锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久亚洲精品成人无码网站,婷婷伊人久久大香线蕉AV,国产叼嘿久久精品久久http://www.shnenglu.com/keyws/category/2437.htmlC++ && keyWordSpottingzh-cnMon, 19 May 2008 20:24:02 GMTMon, 19 May 2008 20:24:02 GMT60銆愯漿銆憊ector 錛堜簩錛?/title><link>http://www.shnenglu.com/keyws/archive/2006/08/13/11194.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 13 Aug 2006 11:52:00 GMT</pubDate><guid>http://www.shnenglu.com/keyws/archive/2006/08/13/11194.html</guid><wfw:comment>http://www.shnenglu.com/keyws/comments/11194.html</wfw:comment><comments>http://www.shnenglu.com/keyws/archive/2006/08/13/11194.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/keyws/comments/commentRss/11194.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/keyws/services/trackbacks/11194.html</trackback:ping><description><![CDATA[ <table> <tbody> <tr> <td> <div id="9b97979" class="body-content"> <div id="1lntxp9" class="header-box"> <a >www.cppreference.com</a> <br /> <br />assign </div> <div id="79dh79l" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">2</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽assign(聽size_type聽num,聽</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽TYPE</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">聽val聽);<br /></span><span style="COLOR: #008080">3</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽assign(聽input_iterator聽start,聽input_iterator聽end聽);</span></div> </pre> <p>The assign() function either gives the current vector the values from <em>start</em> to <em>end</em>, or gives it <em>num</em> copies of <em>val</em>.</p> <p>This function <font color="#ff3333">will destroy the previous contents of the vector.</font></p> <p>For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:</p> <pre class="example-code"> vector<int> v; v.assign( 10, 42 ); for( int i = 0; i < v.size(); i++ ) { cout << v[i] << " "; } cout << endl; </pre> <p>The above code displays the following output:</p> <pre class="example-code"> 42 42 42 42 42 42 42 42 42 42 </pre> <p>The next example shows how assign() can be used to copy one vector to another:</p> <pre class="example-code"> vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } vector<int> v2; v2.assign( v1.begin(), v1.end() ); for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; </pre> <p>When run, the above code displays the following output:</p> <pre class="example-code"> 0 1 2 3 4 5 6 7 8 9</pre> </div> </td> </tr> </tbody> </table> <hr /> <div id="x1bl791" class="name-format">back </div> <div id="zfdfx91" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">2</span>聽<span style="COLOR: #000000">TYPE</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">聽back();<br /></span><span style="COLOR: #008080">3</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽TYPE</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">聽back()聽</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;</span></div> </pre> <p>The back() function <font color="#ff3300">returns a reference to the last element in the vector</font>.</p> <p>For example:</p> <pre class="example-code"> vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } cout << "The first element is " << v.front() << " and the last element is " << v.back() << endl; </pre> <p>This code produces the following output:</p> <pre class="example-code"> The first element is 0 and the last element is 4 </pre> <p> </p> <hr /> <p> </p> <div id="99d99jd" class="name-format">at </div> <div id="p7rhlfr" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <a >TYPE</a>& at( <strong>size_type</strong> loc ); const <a >TYPE</a>& at( <strong>size_type</strong> loc ) const; </pre> <p>The at() function<font color="#ff3300"> returns a reference to the element in the vector at index <em>loc</em></font>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.</p> <p>For example, consider the following code:</p> <pre class="example-code"> vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v[i] << endl; } </pre> <p>This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:</p> <pre class="example-code"> vector<int> v( 5, 1 ); for( int i = 0; i < 10; i++ ) { cout << "Element " << i << " is " << v.at(i) << endl; } </pre> <p>Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.</p> <div id="9799j9b" class="related-name-format"> <hr /> </div> <div id="h9rnh99" class="name-format">capacity </div> <div id="jxzbxh9" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <strong>size_type</strong> capacity() const; </pre> <p>The capacity() function <font color="#ff3333">returns the number of elements that the vector can hold before it will need to allocate more space</font>.</p> <p>For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:</p> <pre class="example-code"> vector<int> v1(10); cout << "The capacity of v1 is " << v1.capacity() << endl; vector<int> v2; v2.reserve(20); cout << "The capacity of v2 is " << v2.capacity() << endl; </pre> <p>When run, the above code produces the following output:</p> <pre class="example-code"> The capacity of v1 is 10 The capacity of v2 is 20 </pre> <p>C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the <a >reserve</a>() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.</p> <p>The capacity() function runs in <a >constant time</a>.</p> <div> </div> <hr /> <div id="jdvrd7z" class="name-format">begin </div> <div id="7b7n999" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> iterator begin(); const_iterator begin() const; </pre> <p>The function begin() returns an iterator to the first element of the vector. begin() should run in <a >constant time</a>.</p> <p>For example, the following code uses begin() to initialize an iterator that is used to traverse a list:</p> <pre class="example-code"> // Create a list of characters list<char> charList; for( int i=0; i < 10; i++ ) { charList.push_front( i + 65 ); } // Display the list list<char>::iterator theIterator; for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) { cout << *theIterator; } </pre> <div id="lxz99ff" class="related-name-format"> <hr /> </div> <div id="rdfjvpx" class="name-format">max_size </div> <div id="77l9jb9" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <strong>size_type</strong> max_size() const; </pre> <p>The max_size() function returns the maximum number of elements that the vector can hold. The max_size() function should not be confused with the <a >size</a>() or <a >capacity</a>() functions, which return the number of elements currently in the vector and the the number of elements that the vector will be able to hold before more memory will have to be allocated, respectively.</p> <div id="9d9nbjl" class="related-name-format"> <hr /> </div> <div id="t9tfzfl" class="name-format">clear </div> <div id="7n99pfl" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void clear(); </pre> <p>The function clear() deletes all of the elements in the vector. clear() runs in <a >linear time</a>.</p> <div id="97fj9r9" class="related-name-format"> <hr /> </div> <div id="f97j999" class="name-format">empty </div> <div id="7bz9jh9" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> bool empty() const; </pre> <p>The empty() function returns true if the vector has no elements, false otherwise.</p> <p>For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) <a >while</a> loop to clear a vector and display its contents in reverse order:</p> <pre class="example-code"> vector<int> v; for( int i = 0; i < 5; i++ ) { v.push_back(i); } while( !v.empty() ) { cout << v.back() << endl; v.pop_back(); } </pre> <div id="7rxb9bl" class="related-name-format"> <hr /> </div> <div id="9dr7j9h" class="name-format">end </div> <div id="rztjl77" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> iterator end(); const_iterator end() const; </pre> <p>The end() function returns an iterator just past the end of the vector.</p> <p>Note that before you can access the last element of the vector using an iterator that you get from a call to end(), you'll have to decrement the iterator first. This is because end() doesn't point to the end of the vector; it points <strong>just past the end of the vector</strong>.</p> <p>For example, in the following code, the first "cout" statement will display garbage, whereas the second statement will actually display the last element of the vector:</p> <pre class="example-code"> vector<int> v1; v1.push_back( 0 ); v1.push_back( 1 ); v1.push_back( 2 ); v1.push_back( 3 ); int bad_val = *(v1.end()); cout << "bad_val is " << bad_val << endl; int good_val = *(v1.end() - 1); cout << "good_val is " << good_val << endl; </pre> <p>The next example shows how <a >begin</a>() and end() can be used to iterate through all of the members of a vector:</p> <pre class="example-code"> vector<int> v1( 5, 789 ); vector<int>::iterator it; for( it = v1.begin(); it != v1.end(); it++ ) { cout << *it << endl; } </pre> <p>The iterator is initialized with a call to <a >begin</a>(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the vector, the loop will only stop once all of the elements of the vector have been displayed.</p> <p>end() runs in <a >constant time</a>.<br /><br /></p> <hr /> <div id="r779tfl" class="name-format">erase </div> <div id="hllhjh7" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> iterator erase( iterator loc ); iterator erase( iterator start, iterator end ); </pre> <p>The erase() function either deletes the element at location <em>loc</em>, or deletes the elements between <em>start</em> and <em>end</em> (including <em>start</em> but not including <em>end</em>). The return value is the element after the last element erased.</p> <p>The first version of erase (the version that deletes a single element at location <em>loc</em>) runs in <a >constant time</a> for lists and <a >linear time</a> for vectors, dequeues, and strings. The multiple-element version of erase always takes <a >linear time</a>.</p> <p>For example:</p> <pre class="example-code"> // Create a vector, load it with the first ten characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } int size = alphaVector.size(); vector<char>::iterator startIterator; vector<char>::iterator tempIterator; for( int i=0; i < size; i++ ) { startIterator = alphaVector.begin(); alphaVector.erase( startIterator ); // Display the vector for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) { cout << *tempIterator; } cout << endl; } </pre> <p>That code would display the following output:</p> <pre class="example-code"> BCDEFGHIJ CDEFGHIJ DEFGHIJ EFGHIJ FGHIJ GHIJ HIJ IJ J </pre> <p>In the next example, erase() is called with two iterators to delete a range of elements from a vector:</p> <pre class="example-code"> // create a vector, load it with the first ten characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } // display the complete vector for( int i = 0; i < alphaVector.size(); i++ ) { cout << alphaVector[i]; } cout << endl; // use erase to remove all but the first two and last three elements // of the vector alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 ); // display the modified vector for( int i = 0; i < alphaVector.size(); i++ ) { cout << alphaVector[i]; } cout << endl; </pre> <p>When run, the above code displays:</p> <pre class="example-code"> ABCDEFGHIJ ABHIJ </pre> <div id="rlnxr7j" class="related-name-format">Related topics: </div> <div id="9rf7pn9" class="related-content"> <a >clear</a> <br /> <a >insert</a> <br /> <a >pop_back</a> <br />(C++ Lists) <a >pop_front</a><br />(C++ Lists) <a >remove</a><br />(C++ Lists) <a >remove_if</a></div> <div> </div> <hr /> <div id="ht9h779" class="name-format">front </div> <div id="vzb9pvb" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <a >TYPE</a>& front(); const <a >TYPE</a>& front() const; </pre> <p>The front() function returns a reference to the first element of the vector, and runs in <a >constant time</a>.</p> <div id="9hbnhfd" class="related-name-format">Related topics: </div> <div id="p9tn7t9" class="related-content"> <a >back</a> <br />(C++ Lists) <a >pop_front</a><br />(C++ Lists) <a >push_front</a></div> <div> </div> <hr /> <div id="9dhrvbj" class="name-format">insert </div> <div id="h7jbv79" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> iterator insert( iterator loc, const <a >TYPE</a>& val ); void insert( iterator loc, <strong>size_type</strong> num, const <a >TYPE</a>& val ); template<<a >TYPE</a>> void insert( iterator loc, <a >input_iterator</a> start, <a >input_iterator</a> end ); </pre> <p>The insert() function either:</p> <ul> <li>inserts <em>val</em> before <em>loc</em>, returning an iterator to the element inserted, </li> <li>inserts <em>num</em> copies of <em>val</em> before <em>loc</em>, or </li> <li>inserts the elements from <em>start</em> to <em>end</em> before <em>loc</em>. </li> </ul> <p>Note that inserting elements into a vector can be relatively time-intensive, since the underlying data structure for a vector is an array. In order to insert data into an array, you might need to displace a lot of the elements of that array, and this can take <a >linear time</a>. If you are planning on doing a lot of insertions into your vector and you care about speed, you might be better off using a container that has a linked list as its underlying data structure (such as a <a >List</a> or a <a >Deque</a>).</p> <p>For example, the following code uses the insert() function to splice four copies of the character 'C' into a vector of characters:</p> <pre class="example-code"> // Create a vector, load it with the first 10 characters of the alphabet vector<char> alphaVector; for( int i=0; i < 10; i++ ) { alphaVector.push_back( i + 65 ); } // Insert four C's into the vector vector<char>::iterator theIterator = alphaVector.begin(); alphaVector.insert( theIterator, 4, 'C' ); // Display the vector for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ ) { cout << *theIterator; } </pre> <p>This code would display:</p> <pre class="example-code"> CCCCABCDEFGHIJ </pre> <p>Here is another example of the insert() function. In this code, insert() is used to append the contents of one vector onto the end of another:</p> <pre class="example-code"> vector<int> v1; v1.push_back( 0 ); v1.push_back( 1 ); v1.push_back( 2 ); v1.push_back( 3 ); vector<int> v2; v2.push_back( 5 ); v2.push_back( 6 ); v2.push_back( 7 ); v2.push_back( 8 ); cout << "Before, v2 is: "; for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; v2.insert( v2.end(), v1.begin(), v1.end() ); cout << "After, v2 is: "; for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; </pre> <p>When run, this code displays:</p> <pre class="example-code"> Before, v2 is: 5 6 7 8 After, v2 is: 5 6 7 8 0 1 2 3 </pre> <div id="v9pj99f" class="related-name-format">Related topics: </div> <div id="xbt9p7n" class="related-content"> <a >assign</a> <br /> <a >erase</a> <br /> <a >push_back</a> <br />(C++ Lists) <a >merge</a><br />(C++ Lists) <a >push_front</a><br />(C++ Lists) <a >splice</a></div> <div> </div> <hr /> <div id="9r9djp9" class="name-format">Vector constructors </div> <div id="97jdxf9" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> vector(); vector( const vector& c ); vector( <strong>size_type</strong> num, const <a >TYPE</a>& val = <a >TYPE</a>() ); vector( <a >input_iterator</a> start, <a >input_iterator</a> end ); ~vector(); </pre> <p>The default vector constructor takes no arguments, creates a new instance of that vector.</p> <p>The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector <em>c</em>.</p> <p>The third constructor creates a vector with space for <em>num</em> objects. If <em>val</em> is specified, each of those objects will be given that value. For example, the following code creates a vector consisting of five copies of the integer 42:</p> <pre class="example-code"> vector<int> v1( 5, 42 ); </pre> <p>The last constructor creates a vector that is initialized to contain the elements between <em>start</em> and <em>end</em>. For example:</p> <pre class="example-code"> // create a vector of random integers cout << "original vector: "; vector<int> v; for( int i = 0; i < 10; i++ ) { int num = (int) rand() % 10; cout << num << " "; v.push_back( num ); } cout << endl; // find the first element of v that is even vector<int>::iterator iter1 = v.begin(); while( iter1 != v.end() && *iter1 % 2 != 0 ) { iter1++; } // find the last element of v that is even vector<int>::iterator iter2 = v.end(); do { iter2--; } while( iter2 != v.begin() && *iter2 % 2 != 0 ); cout << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl; cout << "new vector: "; vector<int> v2( iter1, iter2 ); for( int i = 0; i < v2.size(); i++ ) { cout << v2[i] << " "; } cout << endl; </pre> <p>When run, this code displays the following output:</p> <pre class="example-code"> original vector: 1 9 7 9 2 7 2 1 9 8 first even number: 2, last even number: 8 new vector: 2 7 2 1 9 </pre> <p>All of these constructors run in <a >linear time</a> except the first, which runs in <a >constant time</a>.</p> <p>The default destructor is called when the vector should be destroyed.</p> <div> </div> <hr /> <div id="nx7drhn" class="name-format">pop_back </div> <div id="9df77tv" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void pop_back(); </pre> <p>The pop_back() function removes the last element of the vector.</p> <p>pop_back() runs in <a >constant time</a>.</p> <div id="9dxj9hv" class="related-name-format">Related topics: </div> <div id="j77f7vb" class="related-content"> <a >back</a> <br /> <a >erase</a> <br />(C++ Lists) <a >pop_front</a><br /><a >push_back</a></div> <div> </div> <hr /> <div id="7ztx9bz" class="name-format">push_back </div> <div id="9jdhbrf" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void push_back( const <a >TYPE</a>& val ); </pre> <p>The push_back() function appends <em>val</em> to the end of the vector.</p> <p>For example, the following code puts 10 integers into a list:</p> <pre class="example-code"> list<int> the_list; for( int i = 0; i < 10; i++ ) the_list.push_back( i ); </pre> <p>When displayed, the resulting list would look like this:</p> <pre class="example-code"> 0 1 2 3 4 5 6 7 8 9 </pre> <p>push_back() runs in <a >constant time</a>.</p> <div id="vnrl797" class="related-name-format">Related topics: </div> <div id="jbvprzp" class="related-content"> <a >assign</a> <br /> <a >insert</a> <br /> <a >pop_back</a> <br />(C++ Lists) <a >push_front</a></div> <div> </div> <hr /> <div id="x79pxll" class="name-format">rbegin </div> <div id="vrlvb77" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <a >reverse_iterator</a> rbegin(); const_<a >reverse_iterator</a> rbegin() const; </pre> <p>The rbegin() function returns a <a >reverse_iterator</a> to the end of the current vector.</p> <p>rbegin() runs in <a >constant time</a>.</p> <div id="z777jjx" class="related-name-format">Related topics: </div> <div id="rdn799l" class="related-content"> <a >begin</a> <br /> <a >end</a> <br /> <a >rend</a> </div> <div> </div> <hr /> <div id="r7t7nd9" class="name-format">rend </div> <div id="b7n7hhf" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <a >reverse_iterator</a> rend(); const_<a >reverse_iterator</a> rend() const; </pre> <p>The function rend() returns a <a >reverse_iterator</a> to the beginning of the current vector.</p> <p>rend() runs in <a >constant time</a>.</p> <div id="r9797r9" class="related-name-format">Related topics: </div> <div id="7lh7hh9" class="related-content"> <a >begin</a> <br /> <a >end</a> <br /> <a >rbegin</a> </div> <div> </div> <hr /> <div id="prb7z71" class="name-format">reserve </div> <div id="rl7pjbz" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void reserve( <strong>size_type</strong> size ); </pre> <p>The reserve() function sets the capacity of the vector to at least <em>size</em>.</p> <p>reserve() runs in <a >linear time</a>.</p> <div id="v79fpnl" class="related-name-format">Related topics: </div> <div id="lfx7tj7" class="related-content"> <a >capacity</a> </div> <div> </div> <hr /> <div id="dd7jllh" class="name-format">resize </div> <div id="9rjrj9z" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void resize( <strong>size_type</strong> num, const <a >TYPE</a>& val = <a >TYPE</a>() ); </pre> <p>The function resize() changes the size of the vector to <em>size</em>. If <em>val</em> is specified then any newly-created elements will be initialized to have a value of <em>val</em>.</p> <p>This function runs in <a >linear time</a>.</p> <div id="nxj79jh" class="related-name-format">Related topics: </div> <div id="xb9xhz7" class="related-content"> <a >Vector constructors & destructors</a> <br /> <a >capacity</a> <br /> <a >size</a> </div> <div> </div> <hr /> <div id="7r9nffl" class="name-format">size </div> <div id="9ztrlzz" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <strong>size_type</strong> size() const; </pre> <p>The size() function returns the number of elements in the current vector.</p> <div id="hlfz7r7" class="related-name-format">Related topics: </div> <div id="z7b799v" class="related-content"> <a >capacity</a> <br /> <a >empty</a> <br />(C++ Strings) <a >length</a><br /><a >max_size</a><br /><a >resize</a></div> <div> </div> <hr /> <div id="979vp7b" class="name-format">swap </div> <div id="h7btnll" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> void swap( const container& from ); </pre> <p>The swap() function exchanges the elements of the current vector with those of <em>from</em>. This function operates in <a >constant time</a>.</p> <p>For example, the following code uses the swap() function to exchange the values of two strings:</p> <pre class="example-code"> string first( "This comes first" ); string second( "And this is second" ); first.swap( second ); cout << first << endl; cout << second << endl; </pre> <p>The above code displays:</p> <pre class="example-code"> And this is second This comes first </pre> <div id="9rbdf7r" class="related-name-format">Related topics: </div> <div id="j7jtplb" class="related-content">(C++ Lists) <a >splice</a></div> <div> </div> <hr /> <div id="rtnnhfn" class="name-format">Vector operators </div> <div id="f7bx7dt" class="syntax-name-format">Syntax: </div> <pre class="syntax-box"> #include <vector> <a >TYPE</a>& operator[]( <strong>size_type</strong> index ); const <a >TYPE</a>& operator[]( <strong>size_type</strong> index ) const; vector operator=(const vector& c2); bool operator==(const vector& c1, const vector& c2); bool operator!=(const vector& c1, const vector& c2); bool operator<(const vector& c1, const vector& c2); bool operator>(const vector& c1, const vector& c2); bool operator<=(const vector& c1, const vector& c2); bool operator>=(const vector& c1, const vector& c2); </pre> <p>All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a vector can be examined with the [] operator.</p> <p>Performing a comparison or assigning one vector to another takes <a >linear time</a>. The [] operator runs in <a >constant time</a>.</p> <p>Two vectors are equal if:</p> <ol> <li>Their size is the same, and </li> <li>Each member in location i in one vector is equal to the the member in location i in the other vector. </li> </ol> <p>Comparisons among vectors are done lexicographically.</p> <p>For example, the following code uses the [] operator to access all of the elements of a vector:</p> <pre class="example-code"> vector<int> v( 5, 1 ); for( int i = 0; i < v.size(); i++ ) { cout << "Element " << i << " is " << v[i] << endl; } </pre> <div id="fpp9vd7" class="related-name-format">Related topics: </div> <div id="7bv79tr" class="related-content"> <a >at</a> </div> <div> </div> <hr /> <img src ="http://www.shnenglu.com/keyws/aggbug/11194.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:52 <a href="http://www.shnenglu.com/keyws/archive/2006/08/13/11194.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>銆愯漿銆憊ector錛堜竴錛?/title><link>http://www.shnenglu.com/keyws/archive/2006/08/13/11193.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 13 Aug 2006 11:36:00 GMT</pubDate><guid>http://www.shnenglu.com/keyws/archive/2006/08/13/11193.html</guid><wfw:comment>http://www.shnenglu.com/keyws/comments/11193.html</wfw:comment><comments>http://www.shnenglu.com/keyws/archive/2006/08/13/11193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/keyws/comments/commentRss/11193.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/keyws/services/trackbacks/11193.html</trackback:ping><description><![CDATA[ <p align="left"> <strong>(涓)<br /></strong> <br />One of the basic classes implemented by the Standard Template Library is the vector class. A vector is, essentially, a resizeable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory. Fortunately, the memory requirements are equivalent to those of a normal array. The header file for the STL vector library is vector. (Note that when using C++, header files drop the .h; for C header files - e.g. stdlib.h - you should still include the .h.) Moreover, the vector class is part of the std <a ><font color="#c30000">namespace</font></a>, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program.</p> <p>Vectors are more powerful than arrays because the number of functions that are available for accessing and modifying vectors. Unfortunately, the [] operator still does not provide bounds checking. There is an alternative way of accessing the vector, using the function at, which does provide bounds checking at an additional cost. Let's take a look at several functions provided by the vector class:<br /><br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span>聽<span style="COLOR: #000000">unsigned聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽size();聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽Returns聽the聽number聽of聽elements聽in聽a聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">push_back(type聽element);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽Adds聽an聽element聽to聽the聽end聽of聽a聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">3</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">聽empty();聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽Returns聽true聽if聽the聽vector聽is聽empty</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽clear();聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽Erases聽all聽elements聽of聽the聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">type聽at(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽n);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Returns聽the聽element聽at聽index聽n,聽with聽bounds聽checking</span></div> <p>also, there are several basic operators defined for the vector class:</p> <pre class="example">= Assignment replaces a vector's contents with the contents of another == An element by element comparison of two vectors [] Random access to an element of a vector (usage is similar to that of the operator with arrays.) Keep in mind that it does not provide bounds checking.</pre> <p>Let's take a look at an example program using the vector class:</p> <pre class="example"> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">聽std;<br /></span><span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽main()<br /></span><span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000">聽聽聽聽vector聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽example;聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Vector聽to聽store聽integers</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽example.push_back(</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">);聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add聽3聽 onto聽the聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽example.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add聽10聽to聽the聽end</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽example.push_back(</span><span style="COLOR: #000000">33</span><span style="COLOR: #000000">);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Add聽33聽to聽the聽end<br /></span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;聽x</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">example.size();聽x</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)聽<br /></span><span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000">聽聽聽聽{<br /></span><span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">example[x]</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should聽output:聽3聽10聽33</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">14</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽}<br /><br /></span><span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">example.empty())聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Checks聽if聽empty</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">16</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽聽聽聽聽example.clear();聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Clears聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">17</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽vector聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽another_vector;聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Creates聽another聽vector聽to聽store聽integers</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">18</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽another_vector.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Adds聽to聽end聽of聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">19</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽example.push_back(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Same</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">20</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(example</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">another_vector)聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">To聽show聽testing聽equality</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">21</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽{<br /></span><span style="COLOR: #008080">22</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽example.push_back(</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">);聽<br /></span><span style="COLOR: #008080">23</span>聽<span style="COLOR: #000000">聽聽聽聽}<br /></span><span style="COLOR: #008080">24</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽y</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;聽y</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">example.size();聽y</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">25</span>聽<span style="COLOR: #000000">聽聽聽聽{<br /></span><span style="COLOR: #008080">26</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">example[y]</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Should聽output聽10聽20</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">27</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽}<br /></span><span style="COLOR: #008080">28</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">29</span>聽<span style="COLOR: #000000">}</span></div> </pre> <b>Summary of Vector Benefits</b> <p>Vectors are somewhat easier to use than regular arrays. At the very least, they get around having to be resized constantly using new and delete. Furthermore, their immense flexibility - support for any datatype and support for automatic resizing when adding elements - and the other helpful included functions give them clear advantages to arrays.</p> <p align="left">Another argument for using vectors are that they help avoid memory leaks--you don't have to remember to free vectors, or worry about how to handle freeing a vector in the case of an exception. This simplifies program flow and helps you write tighter code. Finally, if you use the at() function to access the vector, you get bounds checking at the cost of a slight performance penalty.<br /><br /><strong>(浜?</strong><br /><br />聽C++ <a class="kLink" oncontextmenu="return false;" id="KonaLink0" onmouseover="adlinkMouseOver(event,this,0);" style="POSITION: relative; TEXT-DECORATION: underline! important" onclick="adlinkMouseClick(event,this,0);" onmouseout="adlinkMouseOut(event,this,0);" target="_top"><font style="FONT-WEIGHT: 400; COLOR: blue; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative" color="blue" size="2"><span id="9rnfjhn" class="kLink" style="FONT-WEIGHT: 400; COLOR: blue; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative">vector</span></font></a> is a container template available with Standard Template Library pack. This C++ vector can be used to store similar typed objects sequentially, which can be accessed at a later point of time. As this is a template, all types of data including user defined data types like struct and class can be stored in this container. </p> <p>This article explains briefly about how to insert, delete, access the data with respect to the C++ vector. The type stl::string is used for the purposes of explaining the sample code. Using stl::string is only for sample purposes. Any other type can be used with the C++ vector. <br /><br />The values can be added to the c++ vector at the end of the sequence. The function push_back should be used for this purpose. The <vector> header file should be included in all the header files in order to access the C++ vector and its functions.<br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">vector</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #000000">#include聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">iostream.h</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽main()<br /></span><span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Declaration聽for聽the聽string聽data</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">聽strData聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">One</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">Declaration聽for聽C++聽vector</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽std::聽vector聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽str_Vector;<br /></span><span style="COLOR: #008080">11</span>聽<span style="COLOR: #000000">聽聽聽聽str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000">聽聽聽聽strData聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Two</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000">聽聽聽聽str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">14</span>聽<span style="COLOR: #000000">聽聽聽聽strData聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Three</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000">聽聽聽聽str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">16</span>聽<span style="COLOR: #000000">聽聽聽聽strData聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Four</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">17</span>聽<span style="COLOR: #000000">聽聽聽聽str_Vector.push_back(strData);<br /></span><span style="COLOR: #008080">18</span>聽<span style="COLOR: #000000">}</span></div> <p align="left">The above code adds 4 strings of std::string type to the str_Vector. This uses std:: vector.push_back function. This function takes 1 parameter of the designated type and adds it to the end of the c++ vector.</p> <h2>Accessing Elements of C++ Vector:</h2> <p>聽聽 The elements after being added can be accessed in two ways. One way is our legacy way of accessing the data with vector.at(position_in_integer) function. The position of the data element is passed as the single parameter to access the data.</p> <h3>Using our normal logic for accessing elements stored in C++ Vector:</h3> <p>If we want to access all the data, we can use a for loop and display them as follows.<br /><br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span>聽<span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">聽str_Vector.size();聽i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">2</span>聽<span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">3</span>聽<span style="COLOR: #000000">聽聽聽聽std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">聽strd聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽str_Vector.at(i);<br /></span><span style="COLOR: #008080">4</span>聽<span style="COLOR: #000000">聽聽聽聽cout</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">strd.c_str()</span><span style="COLOR: #000000"><<</span><span style="COLOR: #000000">endl;<br /></span><span style="COLOR: #008080">5</span>聽<span style="COLOR: #000000">}</span></div> <p>The std:: vector .size() function returns the number of elements stored in the vector.</p> <h3>Using C++ vector iterator provided by STL:</h3> <p>The next way is to use the iterator object provided by the STL. These iterators are general purpose pointers allowing c++ programmers to forget the intricacies of object types and access the data of any type.<br /><br /></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span>聽<span style="COLOR: #000000">std::vector</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">::iterator聽itVectorData;<br /></span><span style="COLOR: #008080">2</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(itVectorData聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽str_Vector.begin();聽聽itVectorData聽</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">聽str_Vector.end();聽itVectorData</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">3</span>聽<span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">4</span>聽<span style="COLOR: #000000">聽聽聽聽std::</span><span style="COLOR: #0000ff">string</span><span style="COLOR: #000000">聽strD聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(itVectorData);<br /></span><span style="COLOR: #008080">5</span>聽<span style="COLOR: #000000">}</span></div> <h2>Removing elements from C++ vector:</h2> <p>Removing elements one by one from specified positions in c++ vector is achieved with the erase function. </p> <p>The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000">str_Vector.erase(str_Vector.begin()</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">1</span> <span style="COLOR: #000000">,str_Vector.begin()</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">2</span> <span style="COLOR: #000000">);</span> </div> <p>聽The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.聽</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080">1</span> <img src="http://www.shnenglu.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000">str_Vector.erase(str_Vector.begin()</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">1</span> <span style="COLOR: #000000">,str_Vector.begin()</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000">3</span> <span style="COLOR: #000000">);</span> </div> <p>If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.</p> <!-- End of text --> <img src ="http://www.shnenglu.com/keyws/aggbug/11193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:36 <a href="http://www.shnenglu.com/keyws/archive/2006/08/13/11193.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>銆愯漿銆慽teratorshttp://www.shnenglu.com/keyws/archive/2006/08/13/11187.htmlkeywskeywsSun, 13 Aug 2006 11:04:00 GMThttp://www.shnenglu.com/keyws/archive/2006/08/13/11187.htmlhttp://www.shnenglu.com/keyws/comments/11187.htmlhttp://www.shnenglu.com/keyws/archive/2006/08/13/11187.html#Feedback0http://www.shnenglu.com/keyws/comments/commentRss/11187.htmlhttp://www.shnenglu.com/keyws/services/trackbacks/11187.html http://www.cprogramming.com/tutorial/stl/iterators.html The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a vector, map, list, etc.

You can think of an iterator as pointing to an item that is part of a larger container of items. For intance, all containers support a function called begin, which will return an iterator pointing to the beginning of the container (the first element) and function, end, that returns an iterator corresponding to having reached the end of the container. In fact, you can access the element by "dereferencing" the iterator with a *, just as you would dereference a pointer.

To request an iterator appropriate for a particular STL templated class, you use the syntax

1std::class_name<template_parameters>::iterator聽name

where name is the name of the iterator variable you wish to create and the class_name is the name of the STL container you are using, and the template_paramters are the parameters to the template used to declare objects that will work with this iterator. Note that because the STL classes are part of the std namespace, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program.

For instance, if you had an STL vector storing integers, you could create an iterator for it as follows:
1std::vector<int>聽myIntVector;
2std::vector<int>::iterator聽myIntVectorIterator;
Different operations and containers support different types of iterator behavior. In fact, there are several different classes of iterators, each with slightly different properties. First, iterators are distinguished by whether you can use them for reading or writing data in the container. Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time.

Some of the most important are the forward, backward and the bidirectional iterators. Both of these iterators can be used as either input or output iterators, meaning you can use them for either writing or reading. The forward iterator only allows movement one way -- from the front of the container to the back. To move from one element to the next, the increment operator, ++, can be used.

For instance, if you want to access the elements of an STL vector, it's best to use an iterator instead of the traditional C-style code. The strategy is fairly straightforward: call the container's begin function to get an iterator, use ++ to step through the objects in the container, access each object with the * operator ("*iterator") similar to the way you would access an object by dereferencing a pointer, and stop iterating when the iterator equals the container's end iterator. You can compare iterators using != to check for inequality, == to check for equality. (This only works for one twhen the iterators are operating on the same container!)

The old approach (avoid)
聽1usingnamespace聽std;
聽2
聽3vector<int>聽myIntVector;
聽4
聽5//聽Add聽some聽elements聽to聽myIntVector
聽6myIntVector.push_back(1);
聽7myIntVector.push_back(4);
聽8myIntVector.push_back(8);
聽9
10for(int聽y=0;聽y<myIntVector.size();聽y++)
11{
12聽聽聽聽cout<<myIntVector[y]<<"";聽聽//Should聽output聽1聽4聽8
13}
The STL approach (use this)
聽1usingnamespace聽std;
聽2
聽3vector<int>聽myIntVector;
聽4vector<int>::iterator聽myIntVectorIterator;
聽5
聽6//聽Add聽some聽elements聽to聽myIntVector
聽7myIntVector.push_back(1);
聽8myIntVector.push_back(4);
聽9myIntVector.push_back(8);
10
11for(myIntVectorIterator聽=聽myIntVector.begin();聽
12聽聽聽聽聽聽聽聽myIntVectorIterator聽!=聽myIntVector.end();
13聽聽聽聽聽聽聽聽myIntVectorIterator++)
14{
15聽聽聽聽cout<<*myIntVectorIterator<<"";聽聽聽聽//Should聽output聽1聽4聽8
16}
17
As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator.

Iterators are often handy for specifying a particular range of things to operate on. For instance, the range item.begin(), item.end() is the entire container, but smaller slices can be used. This is particularly easy with one other, extremely general class of iterator, the random access iterator, which is functionally equivalent to a pointer in C or C++ in the sense that you can not only increment or decrement but also move an arbitrary distance in constant time (for instance, jump multiple elements down a vector).

For instance, the iterators associated with vectors are random access iterators so you could use arithmetic of the form
iterator + n
where n is an integer. The result will be the element corresponding to the nth item after the item pointed to be the current iterator. This can be a problem if you happen to exceed the bounds of your iterator by stepping forward (or backward) by too many elements.

The following code demonstrates both the use of random access iterators and exceeding the bounds of the array (don't run it!):
1vector<int>聽myIntVector;
2vector<int>::iterator聽myIntVectorIterator;
3myIntVectorIterator聽=聽myIntVector.begin()聽+2;
You can also use the standard arithmetic shortcuts for addition and subtraction, += and -=, with random access iterators. Moreover, with random access iterators you can use <, >, <=, and >= to compare iterator positions within the container.

Iterators are also useful for some functions that belong to container classes that require operating on a range of values. A simple but useful example is the erase function. The vector template supports this function, which takes a range as specified by two iterators -- every element in the range is erased. For instance, to erase an entire vector:
1vector<int>::iterator聽myIntVectorIterator;
2myIntVector.erase(myIntVectorIterator.begin(),聽myIntVectorIterator.end());
which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use
1myIntVector.erase(myIntVectorIterator.begin(),聽myIntVectorIterator.begin()+2);
Note that various container class support different types of iterators -- the vector class, which has served as our model for iterators, supports a random access iterator, the most general kind. Another container, the list container (to be discussed later), only supports bidirectional iterators.

So why use iterators? First, they're a flexible way to access the data in containers that don't have obvious means of accessing all of the data (for instance, maps [to be discussed later]). They're also quite flexible -- if you change the underlying container, it's easy to change the associated iterator so long as you only use features associated with the iterator supported by both classes. Finally, the STL algorithms defined in <algorithm> (to be discussed later) use iterators.

Summary

The Good
  • The STL provides iterators as a convenient abstraction for accessing many different types of containers.
  • Iterators for templated classes are generated inside the class scope with the syntax
    class_name<parameters>::iterator
    
  • Iterators can be thought of as limited pointers (or, in the case of random access iterators, as nearly equivalent to pointers)
The Gotchas
  • Iterators do not provide bounds checking; it is possible to overstep the bounds of a container, resulting in segmentation faults
  • Different containers support different iterators, so it is not always possible to change the underlying container type without making changes to your code
  • Iterators can be invalidated if the underlying container (the container being iterated over) is changed significantly


keyws 2006-08-13 19:04 鍙戣〃璇勮
]]>
国产精品成人久久久| 久久国产精品久久精品国产| 欧美亚洲日本久久精品| 久久综合色之久久综合| 亚洲午夜久久久影院| 久久综合狠狠色综合伊人| 久久99精品国产99久久6| 久久久亚洲裙底偷窥综合| 久久亚洲精品成人av无码网站| 久久AV无码精品人妻糸列| 国产午夜免费高清久久影院| 久久久黄片| 美女写真久久影院| 久久永久免费人妻精品下载| 合区精品久久久中文字幕一区| 久久精品国产精品亚洲毛片| 日韩中文久久| 999久久久免费国产精品播放| 性做久久久久久久| 亚洲精品无码久久不卡| 国产激情久久久久影院| 久久精品麻豆日日躁夜夜躁| 精品一二三区久久aaa片| 久久久久一本毛久久久| 青青草原综合久久大伊人精品| 精品多毛少妇人妻AV免费久久| 亚洲精品无码久久毛片| 久久久久女教师免费一区| 精品久久久久久久久久久久久久久| 久久久久久毛片免费播放| 色综合久久无码五十路人妻| 伊人久久久AV老熟妇色| 久久久久久久精品妇女99| 一级做a爰片久久毛片毛片| 四虎影视久久久免费观看| 久久综合久久性久99毛片| 久久天天躁狠狠躁夜夜2020老熟妇 | 国内精品久久久久国产盗摄| 99久久综合狠狠综合久久| 99热精品久久只有精品| 久久婷婷五月综合成人D啪|