How to insert value into vector at specific position in C++

1 Answer

0 votes
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec = { 9, 1, 2, 3, 4, 1, 2, 3 };
	vector<int>::iterator it = vec.begin();

	it += 2;
	vec.insert(it, 1, 8008);

	for (it = vec.begin(); it != vec.end(); it++)
		cout << ' ' << *it;

	cout << endl;

	return 0;
}


/*
run:

9 1 8008 2 3 4 1 2 3

*/

 



answered May 17, 2018 by avibootz
edited May 17, 2018 by avibootz
...