📄 vector1.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>阵列型式(Array 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: 阵列型式(Array
idiom) vector </a></h1>
<br>
您可以使用vector来替代阵列,并使用阵列型式(Array
idiom)的方式来操作vector,要使用vector,必须含入vector表头档:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include
<vector></span><br>
</div>
<br>
要建立vector型态的物件(Object),您必须提供元素型态与长度资讯,例如下例中建立int元素的vector,并拥有10个元素:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">vector<int>
ivector(10);</span><br>
</div>
<br>
您可以模仿阵列中存取元素的方式来存取vector的元素,使用下标(Subscript)运算子并指定索引来取得指定的元素,例如:<br>
<br>
<pre>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> vector<int> ivector(10);<br> <br> for(int i = 0; i < ivector.size(); i++) {<br> ivector[i] = i;<br> }<br> <br> for(int i = 0; i < ivector.size(); i++) {<br> cout << ivector[i] << " ";<br> }<br> cout << endl;<br><br> return 0; <br>}</pre>
<br>
执行结果: <br>
<small> </small><small> </small><small> </small><small> </small><small>
</small>
<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</span><span style="color: rgb(255, 255, 255);"></span></small></td>
</tr>
</tbody>
</table>
<br>
与阵列型态不同的是,vector记得自己的长度资讯,您可以使用size()查询vector的元素长度,或是使用empty()测试长度是否为0。<br>
<br>
当您宣告vector物件时,所有的元素值会有与型态相应的初始值,例如算术相关型态会被初始为0,指标型态初始为0(表示不指向任何位址),如果您打算
将所有元素初始为指定的值,则可以如下:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">vector<int>
ivector(10, 5);</span><br>
</div>
<br>
上例中将宣告有10个元素的vector,并将所有的元素值初始为5。<br>
<br>
如果您愿意,您可以使用一个阵列来作为建构vector的引数(Argument),如此建构出来的vector会拥有阵列所指定的元素初值,在这之前,
您要先知道,宣告一个阵列时,阵列名称所储存的是第一个元素在记忆体中的位址值,对该位址值加一,表示移至下一个元素,当使用阵列来建构vector时,
要指定的是元素的起始位址与最后一个元素的下一个位址,例如以整个阵列来建构vector的话,可以这么撰写:<br>
<br>
<pre>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> int iarr[] = {1, 2, 3, 4, 5};<br> <br> vector<int> ivector(iarr, iarr + 5);<br> <br> for(int i = 0; i < ivector.size(); i++) {<br> cout << ivector[i] << " ";<br> }<br> cout << endl;<br> <br> return 0; <br>}</pre>
<br>
执行结果: <br>
<small> </small><small> </small><small> </small><small> </small><small>
</small>
<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);">1 2 3 4 5</span><span style="color: rgb(255, 255, 255);"></span></small></td>
</tr>
</tbody>
</table>
<br>
如果打算只使用阵列中的某个范围来建构vector,例如复制索引1到3的元素,则可以如下:<br>
<br>
<pre>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> int iarr[] = {1, 2, 3, 4, 5};<br> <br> vector<int> ivector(iarr + 1, iarr + 4);<br> <br> for(int i = 0; i < ivector.size(); i++) {<br> cout << ivector[i] << " ";<br> }<br> cout << endl;<br><br> return 0; <br>}</pre>
<br>
执行结果: <br>
<small> </small><small> </small><small> </small><small> </small><small>
</small>
<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);">2 3 4</span><span style="color: rgb(255, 255, 255);"></span></small></td>
</tr>
</tbody>
</table>
<br>
vector可以使用另一个vector作为引数来建构,例如以ivector1作为引数来建构ivector2:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">vector<int>
ivector1(5, 1);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">vector<int>
ivector2(ivector1);</span><br>
</div>
<br>
vector可以使用=直接指定给另一个vector,这会将vector的值复制给被指定的vector,例如:<br>
<br>
<pre>#include <iostream> <br>#include <vector><br>using namespace std; <br><br>int main() { <br> vector<int> ivector1(5, 1);<br> vector<int> ivector2; // 定义一个空的vector<br> <br> ivector2 = ivector1;<br> <br> for(int i = 0; i < ivector2.size(); i++) {<br> cout << ivector2[i] << " ";<br> }<br> cout << endl;<br> <br> ivector2[0] = 2;<br><br> for(int i = 0; i < ivector2.size(); i++) {<br> cout << ivector2[i] << " ";<br> }<br> cout << endl;<br> <br> for(int i = 0; i < ivector1.size(); i++) {<br> cout << ivector1[i] << " ";<br> }<br> cout << endl;<br> <br> return 0; <br>}<br></pre>
<br>
由于指定时是将值一一复制加入目标vector,所以您改变目标vector的值,并不会影响原来的vector,
执行结果如下: <br>
<small> </small><small> </small><small> </small><small> </small><small>
</small>
<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);">1 1 1 1 1<br>
2 1 1 1 1<br>
1 1 1 1 1</span><span style="color: rgb(255, 255, 255);"></span></small></td>
</tr>
</tbody>
</table>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -