Inserting the value in the vector .

Hi , my code insert the value in the vector at particular position .Can you please help with other ways to insert the values in the vector .

1
2
3
4
5
6
7
8
9
  int myint[] = {300, 394, 208, 193, 400, 798 , 294 , 87 };
	vector<int> myvect( myint , myint + 8 ) ;	
	
	vector<int>::iterator it  = myvect.begin() + 3; 
	myvect.insert(it , 1, 500);
	for( it = myvect.begin() ; it != myvect.end(); it++)
	{
		cout <<"\n Value = "<<*it;
	}

You want ways other than using the vector insert function?
There is the push_back function that adds the element to the end of the vector.
In C++11 there is also emplace/emplace_back that is similar to insert/push_back with the difference that you pass constructor arguments that will be used to construct the object.
yes Moschops
You want ways other than using the vector insert function?


actually... like i want to insert the value eg 900 at the second position in the myvect . now i cannot do
myvect.insert(2, 900); as it will expect vector eg ..
1
2
vector<int> mm( 1,900);
myvector.insert(  mm ) ; 


or inserting at the back of vector like
myvector.push_back( mm) ;

so , i just wanted to know the right way other the i described above .
it may be any method ..
insert expects an iterator and not a vector. I don't think there is a more easy way than doing myvect.insert(myvect.begin() + 2, 900);
thanks ... Peter87 , and Moschops .. ok . so i assume that this is the only way to insert into vector .
Topic archived. No new replies allowed.