⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insert.html

📁 从www.CppReference.com打包的C++参考手册
💻 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>insert</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> &gt; <a href=    "index.html">C++ Vectors</a> &gt; <a href="insert.html">insert</a>  </div>  <div class="name-format">    insert  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;vector&gt;  iterator insert( iterator loc, const <a href="../containers.html">TYPE</a>&amp; val );  void insert( iterator loc, <strong>size_type</strong> num, const <a href="../containers.html">TYPE</a>&amp; val );  template&lt;<a href="../containers.html">TYPE</a>&gt; 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>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  href="../complexity.html">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  href="../cpplist/index.html">List</a> or a  <a  href="../cppdeque/index.html">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&lt;char&gt; alphaVector; for( int i=0; i &lt; 10; i++ ) {   alphaVector.push_back( i + 65 ); }               // Insert four C&#39;s into the vector vector&lt;char&gt;::iterator theIterator = alphaVector.begin(); alphaVector.insert( theIterator, 4, &#39;C&#39; );              // Display the vector for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )    {   cout &lt;&lt; *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&lt;int&gt; v1;  v1.push_back( 0 );  v1.push_back( 1 );  v1.push_back( 2 );  v1.push_back( 3 );  vector&lt;int&gt; v2;  v2.push_back( 5 );  v2.push_back( 6 );  v2.push_back( 7 );  v2.push_back( 8 );  cout &lt;&lt; "Before, v2 is: ";  for( int i = 0; i &lt; v2.size(); i++ ) {    cout &lt;&lt; v2[i] &lt;&lt; " ";  }  cout &lt;&lt; endl;  v2.insert( v2.end(), v1.begin(), v1.end() );  cout &lt;&lt; "After, v2 is: ";  for( int i = 0; i &lt; v2.size(); i++ ) {    cout &lt;&lt; v2[i] &lt;&lt; " ";  }  cout &lt;&lt; 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 class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="assign.html">assign</a><br>    <a href="erase.html">erase</a><br>    <a href="push_back.html">push_back</a><br>    (C++ Lists) <a href="../cpplist/merge.html">merge</a><br>    (C++ Lists) <a href="../cpplist/push_front.html">push_front</a><br>    (C++ Lists) <a href="../cpplist/splice.html">splice</a>  </div>  </div>  </td>    </tr>  </table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -