c 2b 2b vector insert time complexity

Solutions on MaxInterview for c 2b 2b vector insert time complexity by the best coders in the world

showing results for - "c 2b 2b vector insert time complexity"
Miguel
17 Jul 2018
1// inserting into a vector
2#include <iostream>
3#include <vector>
4
5int main ()
6{
7  std::vector<int> myvector (3,100);
8  std::vector<int>::iterator it;
9
10  it = myvector.begin();
11  it = myvector.insert ( it , 200 );
12
13  myvector.insert (it,2,300);
14
15  // "it" no longer valid, get a new one:
16  it = myvector.begin();
17
18  std::vector<int> anothervector (2,400);
19  myvector.insert (it+2,anothervector.begin(),anothervector.end());
20
21  int myarray [] = { 501,502,503 };
22  myvector.insert (myvector.begin(), myarray, myarray+3);
23
24  std::cout << "myvector contains:";
25  for (it=myvector.begin(); it<myvector.end(); it++)
26    std::cout << ' ' << *it;
27  std::cout << '\n';
28
29  return 0;
30}