How do I insert into vector<int>?

Hi community lm trying to insert an element in a specific position then displace all elements in a vector<>. l found there's a method wich l cannot understand how to use it. The following valid arguments are:

1
2
   vector<int> myVec;
   myVec.Insert (int * __position, const int & __x); // how l use this method? 


for *__position l tried to create a pointer wich points to the element position, wich l want to insert the new element. It didnt work. lm suspecting *__position is an array or iterable object. Thanks in advance.
Last edited on
The __notation is something internal to the compiler. If that's what IntelliSense or something like that is giving you, probably ignore it (although it isn't exactly wrong, just misleading because it's showing internal stuff that shouldn't usually be seen).

Instead, read the actual library documentation.
http://www.cplusplus.com/reference/vector/vector/insert/

The page above gives a description of different ways to call insert (lowercase, by the way), and code examples.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// inserting into a vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 ); // this is the call you are referring to in your post

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
Last edited on
Thanks for anwer it help me very well, sir.
 
   vector.insert(it+n, 777); // this is what l was looking for 

Topic archived. No new replies allowed.