📄 all.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>C++ Double-ended Queues</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Double-ended Queues</a> </div> <div class="name-format"> assign </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> void assign( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& val ); void assign( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end );</pre> <p>The assign() function either gives the current dequeue 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 will destroy the previous contents of the dequeue.</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 class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Strings) <a href="../cppstring/assign1.html">assign</a><br> <a href="insert.html">insert</a><br> <a href="push_back.html">push_back</a><br> <a href="push_front.html">push_front</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> at </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ); const <a href="../containers.html">TYPE</a>& at( <strong>size_type</strong> loc ) const;</pre> <p>The at() function returns a reference to the element in the dequeue at index <em>loc</em>. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the dequeue.</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 class="related-name-format"> Related topics: </div> <div class="related-content"> (C++ Multimaps) <a href= "../cppmultimap/multimap_operators.html">Multimap operators</a><br> <a href="container_operators.html">Deque operators</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> back </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> <a href="../containers.html">TYPE</a>& back(); const <a href="../containers.html">TYPE</a>& back() const;</pre> <p>The back() function returns a reference to the last element in the dequeue.</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>The back() function runs in <a href="../complexity.html">constant time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="front.html">front</a><br> <a href="pop_back.html">pop_back</a> </div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> begin </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> iterator begin(); const_iterator begin() const;</pre> <p>The function begin() returns an iterator to the first element of the dequeue. begin() should run in <a href= "../complexity.html">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 class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="end.html">end</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"> clear </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> void clear();</pre> <p>The function clear() deletes all of the elements in the dequeue. clear() runs in <a href="../complexity.html">linear time</a>.</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="erase.html">erase</a> </div> </div> </td> </tr> </table></body></html><hr><div class="name-format"> <a name="Container [] operator">Container [] operator</a></div><div class="syntax-name-format">Syntax:</div> <pre class="syntax-box"> <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;</pre><p> <p> Individual elements of a dequeue can be examined with the [] operator. </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 class="example-code"> <p> The [] operator runs in <a href="../complexity.html">constant time</a>. </p></p><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"> <a name="Container [] operator">Container [] operator</a></div><div class="syntax-name-format">Syntax:</div> <pre class="syntax-box"> <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;</pre><p> <p> Individual elements of a dequeue can be examined with the [] operator. </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 class="example-code"> <p> The [] operator runs in <a href="../complexity.html">constant time</a>. </p></p><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"> <a name="Container constructors & destructors">Container constructors & destructors</a></div><div class="syntax-name-format">Syntax:</div> <pre class="syntax-box"> container(); container( const container& c ); ~container();</pre><p> <p> Every dequeue has a default constructor, copy constructor, and destructor. </p> <p> The default constructor takes no arguments, creates a new instance of that dequeue, and runs in <a href="../complexity.html">constant time</a>. The default copy constructor runs in <a href="../complexity.html">linear time</a> and can be used to create a new dequeue that is a copy of the given dequeue <em>c</em>. </p> <p> The default destructor is called when the dequeue should be destroyed. </p> <p> For example, the following code creates a pointer to a vector of integers and then uses the default dequeue constructor to allocate a memory for a new vector: </p> <pre class="example-code"> vector<int>* v; v = new vector<int>(); </pre class="example-code"></p><div class="related-name-format">Related topics:</div><div class="related-content"><a href="container_constructors2.html">Special container constructors</a>, <a href="resize.html">resize</a></div> </div> </td> </tr> </table></body></html><hr> <div class="name-format"> Container constructors </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <deque> container(); container( const container& c ); container( <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>& val = <a href="../containers.html">TYPE</a>() ); container( <a href="../iterators.html">input_iterator</a> start, <a href="../iterators.html">input_iterator</a> end ); ~container();</pre> <p>The default dequeue constructor takes no arguments, creates a new instance of that dequeue.</p> <p>The second constructor is a default copy constructor that can be used to create a new dequeue that is a copy of the given dequeue <em>c</em>.</p> <p>The third constructor creates a dequeue 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -