4.6 - Insert() vector argument specs. Iter or not?
This allows me to refer to the second spot in a vector named 'inventory':
A)
vector<string>::iterator myIterator;
cout << "\nYou found a compass";
inventory.insert((inventory.begin() + 1), "compass");
cout << "\nYour items:\n";
for(iter = inventory.begin(); iter != inventory.end(); ++iter)
cout << *iter << endl;
This does not.
B)
cout << "\nYou found a compass";
inventory.insert(inventory[1], "compass");
cout << "\nYour items:\n";
for(iter = inventory.begin(); iter != inventory.end(); ++iter)
cout << *iter << endl;
I thought maybe because the insert() for vectors requires an iterator as its argument. inventory.begin() works in A) as such. So I tried C), but this didn't work either.
C)
myIterator = inventory[1];
cout << "\nYou found a compass";
inventory.insert(myIterator; "compass");
cout << "\nYour items:\n";
for(iter = inventory.begin(); iter != inventory.end(); ++iter)
cout << *iter << endl;
D)
myIterator = 1;
cout << "\nYou found a compass";
inventory.insert(myIterator; "compass");
cout << "\nYour items:\n";
for(iter = inventory.begin(); iter != inventory.end(); ++iter)
cout << *iter << endl;
Any idea why last 3 examples don't work? B, C, and D?