<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[C++中的vector使用范例 ]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 17 Jan 2008 03:07:40 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	#include&lt;stdio.h&gt;<br/>#include&lt;vector&gt;<br/>#include &lt;iostream&gt;<br/><br/>using namespace std;<br/><br/>void main()<br/><br/>{<br/><br/> &nbsp; int i = 0;<br/><br/> &nbsp; &nbsp;vector&lt;int&gt; v;<br/><br/> &nbsp; &nbsp;for( i = 0; i &lt; 10; i++ )<br/><br/> &nbsp; {<br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.push_back( i );//把元素一个一个存入到vector中<br/><br/> &nbsp; }<br/><br/> &nbsp; for( i = 0; i &lt; v.size(); i++ )//v.size() 表示vector存入元素的个数<br/><br/> &nbsp; {<br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cout &lt;&lt; v[ i ] &lt;&lt; &quot; &nbsp;&quot;; //把每个元素显示出来<br/><br/> &nbsp; }<br/><br/> &nbsp; cont &lt;&lt; endl;<br/><br/>} <br/><br/>注：你也可以用v.begin()和v.end() 来得到vector开始的和结束的元素地址的指针位置。你也可以这样做：<br/><br/>vector&lt;int&gt;::iterator iter;<br/><br/>for( iter = v.begin(); iter != v.end(); iter++ ) <br/><br/>{<br/> &nbsp; &nbsp;cout &lt;&lt; *iter &lt;&lt; endl;<br/>}<br/><br/>2. 对于二维vector的定义。<br/><br/>1)定义一个10个vector元素，并对每个vector符值1-10。<br/><br/>#include&lt;stdio.h&gt;<br/>#include&lt;vector&gt;<br/>#include &lt;iostream&gt;<br/><br/>using namespace std;<br/><br/>void main()<br/>{<br/> int i = 0, j = 0;<br/><br/>//定义一个二维的动态数组，有10行，每一行是一个用一个vector存储这一行的数据。<br/><br/>所以每一行的长度是可以变化的。之所以用到vector&lt;int&gt;(0)是对vector初始化，否则不能对vector存入元素。<br/> vector&lt; vector&lt;int&gt; &gt; Array( 10, vector&lt;int&gt;(0) ); <br/><br/>for( j = 0; j &lt; 10; j++ )<br/> {<br/> &nbsp;for ( i = 0; i &lt; 9; i++ )<br/> &nbsp;{<br/> &nbsp; Array[ j ].push_back( i );<br/> &nbsp;}<br/> }<br/><br/> for( j = 0; j &lt; 10; j++ )<br/> {<br/> &nbsp;for( i = 0; i &lt; Array[ j ].size(); i++ )<br/> &nbsp;{<br/> &nbsp; cout &lt;&lt; Array[ j ][ i ] &lt;&lt; &quot; &nbsp;&quot;;<br/> &nbsp;}<br/> &nbsp;cout&lt;&lt; endl;<br/> }<br/>}<br/><br/>2)定义一个行列都是变化的数组。<br/><br/>#include&lt;stdio.h&gt;<br/>#include&lt;vector&gt;<br/>#include &lt;iostream&gt;<br/><br/>using namespace std;<br/><br/>void main()<br/>{<br/> int i = 0, j = 0;<br/><br/> vector&lt; vector&lt;int&gt; &gt; Array;<br/> vector&lt; int &gt; line;<br/> for( j = 0; j &lt; 10; j++ )<br/> {<br/> &nbsp;Array.push_back( line );//要对每一个vector初始化，否则不能存入元素。<br/> &nbsp;for ( i = 0; i &lt; 9; i++ )<br/> &nbsp;{<br/> &nbsp; Array[ j ].push_back( i );<br/> &nbsp;}<br/> }<br/><br/> for( j = 0; j &lt; 10; j++ )<br/> {<br/> &nbsp;for( i = 0; i &lt; Array[ j ].size(); i++ )<br/> &nbsp;{<br/> &nbsp; cout &lt;&lt; Array[ j ][ i ] &lt;&lt; &quot; &nbsp;&quot;;<br/> &nbsp;}<br/> &nbsp;cout&lt;&lt; endl;<br/> }<br/>}<br/><br/>上面就是我对vector使用的总结，更深入的使用，大家查查vector的手册吧。欢迎批评指正。<br/><br/>用std::vector的const_iterator对元素赋值会怎样？ <br/>c++ builder 6中就是改变不了元素的值，不会编译不过，执行也不报错。这玩意儿把我害惨了，害我找了好长时间。<br/>有空测试下vc7.1,vc8和c++ builder 2007,gcc<br/><br/>写了个测试程序vc7.1下居然能改变值：<br/>参考如下代码，就知道为何要这个vector了：<br/>#include &lt;vector&gt;<br/><br/>struct stUpdateItem<br/>{<br/> &nbsp; &nbsp;bool _downloadSucceeded;<br/><br/> &nbsp; &nbsp;stUpdateItem() : _downloadSucceeded(false)<br/> &nbsp; &nbsp;{}<br/>};<br/><br/>struct stDownItem<br/>{<br/> &nbsp; &nbsp;stUpdateItem* _pItem;<br/> &nbsp; &nbsp;bool &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_bPack;<br/><br/> &nbsp; &nbsp;stDownItem(stUpdateItem* item, bool bPack) : _pItem(item),_bPack(bPack)<br/> &nbsp; &nbsp;{}<br/>};<br/><br/>typedef std::vector&lt;stDownItem&gt; tDownItems;<br/><br/>int _tmain(int argc, _TCHAR* argv[])<br/>{<br/> &nbsp; &nbsp;tDownItems downList;<br/><br/> &nbsp; &nbsp;stUpdateItem item1;<br/> &nbsp; &nbsp;stUpdateItem item2;<br/><br/> &nbsp; &nbsp;stDownItem downItem1(&amp;item1,true);<br/> &nbsp; &nbsp;stDownItem downItem2(&amp;item2,false);<br/><br/> &nbsp; &nbsp;downList.push_back(downItem1);<br/> &nbsp; &nbsp;downList.push_back(downItem2);<br/><br/> &nbsp; &nbsp;for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)<br/> &nbsp; &nbsp;{<br/> &nbsp; &nbsp; &nbsp; &nbsp;if(true == it-&gt;_pItem-&gt;_downloadSucceeded)<br/> &nbsp; &nbsp; &nbsp; &nbsp;{<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; &quot;before change, found!&quot; &lt;&lt; std::endl;<br/> &nbsp; &nbsp; &nbsp; &nbsp;}<br/> &nbsp; &nbsp;}<br/><br/> &nbsp; &nbsp;for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)<br/> &nbsp; &nbsp;{<br/> &nbsp; &nbsp; &nbsp; &nbsp;it-&gt;_pItem-&gt;_downloadSucceeded = true;<br/> &nbsp; &nbsp;}<br/><br/> &nbsp; &nbsp;for (tDownItems::const_iterator it = downList.begin(); it != downList.end(); ++it)<br/> &nbsp; &nbsp;{<br/> &nbsp; &nbsp; &nbsp; &nbsp;if(true == it-&gt;_pItem-&gt;_downloadSucceeded)<br/> &nbsp; &nbsp; &nbsp; &nbsp;{<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;std::cout &lt;&lt; &quot;after change, found!&quot; &lt;&lt; std::endl;<br/> &nbsp; &nbsp; &nbsp; &nbsp;}<br/> &nbsp; &nbsp;}<br/> &nbsp; &nbsp;return 0;<br/>}<br/><br/>参考：http://stl.winterxy.com/html/item_26.html<br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] C++中的vector使用范例 ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>