How to insert an array into a specific position of a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
 
void printVector(std::vector<int> const &v) {
    for (auto const &n: v) {
        std::cout << n << " ";
    }
}
  
int main()
{
	std::vector<int> v = { 5, 2, 7, 1, 9, 3, 6, 4 };
	int N = 3;
	int arr[] = { 100, 200, 300, 400 };

	v.insert(v.begin() + N, arr, arr + 4);
	
	printVector(v);

	return 0;
}

  
  
  
  
/*
run:
  
5 2 7 100 200 300 400 1 9 3 6 4 
   
*/

 



answered Apr 10, 2020 by avibootz

Related questions

1 answer 186 views
1 answer 235 views
1 answer 166 views
1 answer 195 views
2 answers 255 views
1 answer 197 views
...