📄 vector2.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>STL 型式(STL idiom) vector</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++ Gossip: STL 型式(STL idiom)
vector</a></h1>
vector的STL型式,其实就是以物件导向的方式来操作vector(如果您还没接触过物件导向程式设计,这边介绍的可能稍有难度),以物件的方式来
操作vector是比较被鼓励的方式,以下将介绍几个vector的基本操作。<br>
<br>
您可以建构一个元素为空的vector物件:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">vector<int>
ivector;</span><br>
</div>
<br>
如果打算将元素放入vector中,可以使用push_back(),例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(int
i = 0; i < 5; i++) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
ivector.push_back(i);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>
</div>
<br>
如果打算将元素循序取出,则可以begin()与end()方法分别传回起始位置的iterator与结束位置的iterator,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">for(vector<int>::iterator
it = ivector.begin();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
it != ivector.end();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
it++) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
cout << *it << " ";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
<< endl;</span><br>
</div>
<br>
iterator是标准函式库定义类别(Class),它是一个指标,指向iterator物件的真正位址,对它进行++的动作,表示移动至
iterator的下一个元素,对它使用*运算子(Dereferences
operator),表示提取出iterator目前位址的值,如果iterator走访至结束位置的iterator的位址,表示元素走访完毕。<br>
<br>
虽然您可以使用下标运算子[
]来存取vector的元素,但实际上要知道vector与阵列本质上是不相同的,当您如最上头那样宣告一个空的vector物件时,其容量
(capacity)为0,长度(size)也为0,所以此时您不能使用ivector[0]来取得第一个元素值,因为实际上ivector中还没有任何
的元素。<br>
<br>
当使用push_back()将元素加入vector时,vector的长度会自动增长,由于每次增长度都要配置记忆体过于没有效率,所以vector会
自动先增加足够的容量,当元素的长度超过容量时,才会再重新配置新的容量,您可以使用capacity()取得,使用size()取得元素长度,下面这个
程式综合以上的几个介绍作了示范:<br>
<br>
<pre>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> vector<int> ivector;<br> <br> for(int i = 0; i < 10; i++) {<br> ivector.push_back(i);<br> }<br> <br> for(vector<int>::iterator it = ivector.begin();<br> it != ivector.end();<br> it++) {<br> <br> cout << *it << " ";<br> }<br> cout << endl;<br> <br> cout << "capacity: " << ivector.capacity() << endl<br> << "size: " << ivector.size() << endl;<br> <br> return 0; <br>}</pre>
<br>
结果如下:<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">0 1 2 3 4 5 6 7 8 9<br>
capacity: 16<br>
size: 10</span></small></td>
</tr>
</tbody>
</table>
<br>
如果打算对vector进行排序、寻找、反转等操作,可以使用标准函式库中的泛型演算法,要使用泛型演算法必须先含入表头档:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include
<algorithm></span><br>
</div>
<br>
下面这个程式直接示范了排序、寻找、反转等操作:<br>
<br>
<pre>#include <algorithm><br>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> int iarr[] = {30, 12, 55, 31, 98, 11, 41, 80, 66, 21};<br> vector<int> ivector(iarr, iarr + 10);<br> <br> // 排序 <br> sort(ivector.begin(), ivector.end());<br> <br> for(vector<int>::iterator it = ivector.begin();<br> it != ivector.end();<br> it++) {<br> <br> cout << *it << " ";<br> }<br> cout << endl;<br><br> cout << "输入搜寻值:";<br> int search = 0;<br> cin >> search;<br> <br> vector<int>::iterator it = <br> find(ivector.begin(), ivector.end(), search);<br> <br> if(it != ivector.end()) {<br> cout << "找到搜寻值!" << endl;<br> }<br> else {<br> cout << "找不到搜寻值!" << endl;<br> }<br> <br> // 反转 <br> reverse(ivector.begin(), ivector.end());<br> <br> for(vector<int>::iterator it = ivector.begin();<br> it != ivector.end();<br> it++) {<br> <br> cout << *it << " ";<br> }<br> cout << endl;<br> <br> return 0; <br>}</pre>
<br>
执行结果:<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">11 12 21 30 31 41 55 66 80 98<br>
输入搜寻值:41<br>
找到搜寻值!<br>
98 80 66 55 41 31 30 21 12 11</span></small></td>
</tr>
</tbody>
</table>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -