锘??xml version="1.0" encoding="utf-8" standalone="yes"?>美日韩精品免费,免费观看国产成人,欧美美女bbbbhttp://m.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://m.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://m.shnenglu.com/keyws/archive/2006/08/13/11194.html</guid><wfw:comment>http://m.shnenglu.com/keyws/comments/11194.html</wfw:comment><comments>http://m.shnenglu.com/keyws/archive/2006/08/13/11194.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/keyws/comments/commentRss/11194.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/keyws/services/trackbacks/11194.html</trackback:ping><description><![CDATA[ <table> <tbody> <tr> <td> <div id="hvzpftn" class="body-content"> <div id="hvzpftn" class="header-box"> <a >www.cppreference.com</a> <br /> <br />assign </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">back </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">at </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format"> <hr /> </div> <div id="hvzpftn" class="name-format">capacity </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">begin </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format"> <hr /> </div> <div id="hvzpftn" class="name-format">max_size </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format"> <hr /> </div> <div id="hvzpftn" class="name-format">clear </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format"> <hr /> </div> <div id="hvzpftn" class="name-format">empty </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format"> <hr /> </div> <div id="hvzpftn" class="name-format">end </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">erase </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">front </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">insert </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">Vector constructors </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">pop_back </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">push_back </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">rbegin </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content"> <a >begin</a> <br /> <a >end</a> <br /> <a >rend</a> </div> <div> </div> <hr /> <div id="hvzpftn" class="name-format">rend </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content"> <a >begin</a> <br /> <a >end</a> <br /> <a >rbegin</a> </div> <div> </div> <hr /> <div id="hvzpftn" class="name-format">reserve </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content"> <a >capacity</a> </div> <div> </div> <hr /> <div id="hvzpftn" class="name-format">resize </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content"> <a >Vector constructors & destructors</a> <br /> <a >capacity</a> <br /> <a >size</a> </div> <div> </div> <hr /> <div id="hvzpftn" class="name-format">size </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" 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="hvzpftn" class="name-format">swap </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content">(C++ Lists) <a >splice</a></div> <div> </div> <hr /> <div id="hvzpftn" class="name-format">Vector operators </div> <div id="hvzpftn" 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="hvzpftn" class="related-name-format">Related topics: </div> <div id="hvzpftn" class="related-content"> <a >at</a> </div> <div> </div> <hr /> <img src ="http://m.shnenglu.com/keyws/aggbug/11194.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:52 <a href="http://m.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://m.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://m.shnenglu.com/keyws/archive/2006/08/13/11193.html</guid><wfw:comment>http://m.shnenglu.com/keyws/comments/11193.html</wfw:comment><comments>http://m.shnenglu.com/keyws/archive/2006/08/13/11193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/keyws/comments/commentRss/11193.html</wfw:commentRss><trackback:ping>http://m.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="hvzpftn" 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://m.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://m.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://m.shnenglu.com/keyws/aggbug/11193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/keyws/" target="_blank">keyws</a> 2006-08-13 19:36 <a href="http://m.shnenglu.com/keyws/archive/2006/08/13/11193.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>銆愯漿銆慽teratorshttp://m.shnenglu.com/keyws/archive/2006/08/13/11187.htmlkeywskeywsSun, 13 Aug 2006 11:04:00 GMThttp://m.shnenglu.com/keyws/archive/2006/08/13/11187.htmlhttp://m.shnenglu.com/keyws/comments/11187.htmlhttp://m.shnenglu.com/keyws/archive/2006/08/13/11187.html#Feedback0http://m.shnenglu.com/keyws/comments/commentRss/11187.htmlhttp://m.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 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲欧洲综合| 在线一区二区日韩| 欧美第一黄色网| 麻豆国产精品va在线观看不卡| 99在线热播精品免费99热| 亚洲永久免费视频| 国产在线观看一区| 国产视频久久| 樱花yy私人影院亚洲| 亚洲高清在线视频| 一区二区三区波多野结衣在线观看| 日韩视频一区二区三区| 亚洲一区二区三区久久| 久久都是精品| 欧美国产日本| 一二美女精品欧洲| 欧美亚洲在线| 欧美高清视频免费观看| 欧美日韩国产一级| 激情成人在线视频| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 日韩天堂在线视频| 亚洲欧美激情一区| 久久久亚洲人| 欧美亚男人的天堂| 在线成人激情黄色| 亚洲欧美在线播放| 欧美激情网友自拍| 亚洲尤物视频在线| 欧美福利电影在线观看| 国产亚洲欧美日韩一区二区| 最新中文字幕亚洲| 久久综合狠狠综合久久综合88| 亚洲国产婷婷| 欧美在线观看视频| 国产精品日韩在线播放| 亚洲欧洲日韩在线| 久热成人在线视频| 亚洲欧美国产77777| 欧美区亚洲区| 狠狠综合久久av一区二区老牛| 亚洲午夜在线观看| 亚洲激情成人| 这里只有视频精品| 欧美怡红院视频一区二区三区| 亚洲欧美国产制服动漫| 久久久久亚洲综合| 午夜免费日韩视频| 欧美日韩国产va另类| 亚洲激情在线视频| 久久久一本精品99久久精品66| 国产精品久久久久久久久久妞妞| 国产精品欧美日韩| 亚洲一区二区三区中文字幕在线 | 亚洲一区在线播放| 亚洲精品免费在线| 蜜桃久久精品一区二区| 亚洲第一精品福利| 欧美大片91| 欧美 日韩 国产在线| 亚洲国产欧美一区二区三区同亚洲 | 亚洲另类视频| 欧美激情一二三区| 欧美电影在线播放| 亚洲伦伦在线| 一区二区三区国产精品| 国产精品久久久久9999吃药| 亚洲午夜伦理| 亚洲制服丝袜在线| 国产日本欧美一区二区三区| 久久久国产精彩视频美女艺术照福利| 亚洲综合日韩在线| 黄色资源网久久资源365| 久久尤物视频| 久久久欧美精品| 久久不射网站| 亚洲日本免费| 亚洲天堂久久| 国外成人在线视频网站| 激情久久婷婷| 亚洲成在线观看| 欧美日韩在线第一页| 亚洲欧美在线一区二区| 香港久久久电影| 亚洲激情视频网| 亚洲一区二区三区三| 国产精品婷婷| 久久青青草综合| 久久国产视频网站| 国产精品久久一区主播| 一区二区三区四区蜜桃| 亚洲线精品一区二区三区八戒| 久久视频精品在线| 一区二区三区鲁丝不卡| 欧美日韩高清不卡| 亚洲欧美日韩国产综合| 香蕉久久国产| 欧美激情在线狂野欧美精品| 美女主播一区| 久久亚洲私人国产精品va媚药| 欧美成人精品在线观看| 久久精品一区| 欧美日韩一区二区三区在线看 | 亚洲欧美制服中文字幕| 浪潮色综合久久天堂| 欧美电影免费观看| 午夜免费在线观看精品视频| 亚洲国产日本| 欧美精品97| 亚洲无毛电影| 9色精品在线| 国产欧美日韩视频一区二区| 亚洲精品久久久久| 国产精品欧美经典| 亚洲精品国产精品乱码不99| 亚洲大胆av| 久久免费精品视频| 久久蜜桃资源一区二区老牛| 欧美连裤袜在线视频| 久久久久久久久一区二区| 久久丁香综合五月国产三级网站| 在线观看一区视频| 亚洲性夜色噜噜噜7777| 亚洲女同同性videoxma| 欧美日本在线视频| 久久影视精品| 亚洲乱码国产乱码精品精| 久久夜色精品亚洲噜噜国产mv| 久久中文字幕一区| 国产色综合久久| 亚洲欧美日韩系列| 久久在线精品| 久久久精品国产免大香伊| 亚洲午夜成aⅴ人片| 亚洲黄色在线视频| 欧美成年人网站| 欧美成人dvd在线视频| 国产欧美91| 午夜综合激情| 久久精品天堂| 韩国福利一区| 可以免费看不卡的av网站| 欧美成人自拍视频| 亚洲狼人综合| 欧美日韩专区| 亚洲一区二区三区在线| 欧美资源在线| 一区二区三区在线观看欧美| 久久久久综合| 亚洲国产精品久久人人爱蜜臀| 亚洲精品乱码久久久久久蜜桃91| 麻豆成人在线播放| 亚洲精品欧洲| 午夜精品视频在线观看| 国产欧美综合一区二区三区| 欧美一区二区三区久久精品| 久久夜色精品国产亚洲aⅴ| 在线看日韩欧美| 欧美经典一区二区三区| 亚洲尤物视频网| 欧美高清日韩| 亚洲免费伊人电影在线观看av| 国产精品久久久久久久久久免费| 亚洲自拍高清| 欧美国产一区二区在线观看 | 国产精品乱码| 久久精品亚洲乱码伦伦中文 | 亚洲一区二区三区精品动漫| 久久精品日产第一区二区三区| 一卡二卡3卡四卡高清精品视频 | 国产精品久久久久aaaa樱花| 国产噜噜噜噜噜久久久久久久久 | 亚洲免费激情| 亚洲欧美一区二区三区久久| 欧美国产极速在线| 久久全国免费视频| 亚洲最快最全在线视频| 国产精品一区免费在线观看| 久久精品免费看| 一本大道久久精品懂色aⅴ| 久久欧美中文字幕| 亚洲网站在线| 亚洲国产日韩欧美| 国产精品高清免费在线观看| 久久天堂av综合合色| 一本色道久久综合狠狠躁篇怎么玩| 久久大逼视频| 亚洲欧美一区二区三区在线| 亚洲视频一区二区在线观看| 亚洲一区在线观看免费观看电影高清| 亚洲女人天堂av| 亚洲精品国产精品国产自| 国产日韩欧美成人| 欧美日韩一本到| 欧美成人乱码一区二区三区| 欧美伊人久久久久久久久影院| 亚洲视频久久| 日韩亚洲视频在线| 亚洲精品久久久久久久久久久| 久久嫩草精品久久久精品一 |