How to insert N elements at the beginning of a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
 
int main ()
{
    std::vector<int> v = { 5, 2, 7, 1, 9, 4 };

    auto it = v.begin();
    
    int N = 3;
    v.insert(it, N, 1000);
    
    for (auto n: v) {
        std::cout << n << " ";
    }
    
    return 0;
}
  
  
  
  
/*
run:

1000 1000 1000 5 2 7 1 9 4 
   
*/

 



answered Dec 6, 2020 by avibootz
...