📄 container_constructors2.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>Container constructors</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> > <a href= "container_constructors2.html">Container constructors</a> </div> <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> <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -