📄 all.html
字号:
<p>The last constructor creates a dequeue 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 href= "../complexity.html">linear time</a> except the first, which runs in <a href="../complexity.html">constant time</a>.</p> <p>The default destructor is called when the dequeue should be destroyed.</p> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> Container operators </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> <a href="../containers.html">TYPE</a>& operator[]( <strong>size_type</strong> index ); const <a href="../containers.html">TYPE</a>& operator[]( <strong>size_type</strong> index ) const; container operator=(const container& c2); bool operator==(const container& c1, const container& c2); bool operator!=(const container& c1, const container& c2); bool operator<(const container& c1, const container& c2); bool operator>(const container& c1, const container& c2); bool operator<=(const container& c1, const container& c2); bool operator>=(const container& c1, const container& c2);</pre> <p>All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a dequeue can be examined with the [] operator.</p> <p>Performing a comparison or assigning one dequeue to another takes <a href="../complexity.html">linear time</a>. The [] operator runs in <a href="../complexity.html">constant time</a>.</p> <p>Two `containers` are equal if:</p> <ol> <li>Their size is the same, and</li> <li>Each member in location i in one dequeue is equal to the the member in location i in the other dequeue.</li> </ol> <p>Comparisons among dequeues 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 class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="at.html">at</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> empty </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> bool empty() const;</pre> <p>The empty() function returns true if the dequeue has no elements, false otherwise.</p> <p>For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) <a href= "../keywords/while.html">while</a> loop to clear a dequeue 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 class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="size.html">size</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> end </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> iterator end(); const_iterator end() const;</pre> <p>The end() function returns an iterator just past the end of the dequeue.</p> <p>Note that before you can access the last element of the dequeue using an iterator that you get from a call to end(), you'll have to decrement the iterator first.</p> <p>For example, the following code uses <a href= "begin.html">begin</a>() and end() 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 href= "begin.html">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 href="../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="begin.html">begin</a><br> <a href="rbegin.html">rbegin</a><br> <a href="rend.html">rend</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> erase </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> 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 href= "../complexity.html">constant time</a> for lists and <a href= "../complexity.html">linear time</a> for vectors, dequeues, and strings. The multiple-element version of erase always takes <a href= "../complexity.html">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 class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="clear.html">clear</a><br> <a href="insert.html">insert</a><br> <a href="pop_back.html">pop_back</a><br> <a href="pop_front.html">pop_front</a><br> (C++ Lists) <a href="../cpplist/remove.html">remove</a><br> (C++ Lists) <a href="../cpplist/remove_if.html">remove_if</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> front </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> <a href="../containers.html">TYPE</a>& front(); const <a href="../containers.html">TYPE</a>& front() const;</pre> <p>The front() function returns a reference to the first element of the dequeue, and runs in <a href="../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="back.html">back</a><br> <a href="pop_front.html">pop_front</a><br> <a href="push_front.html">push_front</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> insert </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> iterator insert( iterator loc, const <a href="../containers.html">TYPE</a>& val ); void insert( iterator loc, <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& val ); template<<a href="../containers.html">TYPE</a>> void insert( iterator loc, <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">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>For example:</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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -