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?

Edited 6 Years Ago by crapgarden: n/a

in B and C you when you say inventory[1] , it is a string there is no conversion from string to type iterator. And I'm not too sure what you are trying to accomplish in D by setting iterator to 1 ?

You need to pass it an iterator!!!!!!!! Even you said it.

But when you use the operator[], on a vector you are de-referencing it! Which means
you access the elements stored, in your case its a string. So basically, now you
are left with trying to turn a string into a vector iterator, which is not possible.

@AGNI
I was hoping to point the iterator to position 1. Can you tell me why it's wrong?

@FirstPerson
You mean in B) right? I didn't realize [] de-referenced it. That makes sense now.
How about C and D though. Aren't those iterators? Why wouldn't they work then.

Thank you for the help btw. I'm sure it's hard to tell but this is all really helping a lot. This is definitely the hardest part of C++ to wrap my head around so far and Each answer is helping a lot of other questions or shaky areas make a ton more sense. Thanks for all the help!

C is exactly same as B so it is also wrong for the same reason. and in D '1' is an integer and you are trying to initialize an iterator with an int. btw when you compile these, the compiler must be giving some meaningful error messages, you should read them carefully.

In D, isn't that telling the integer to point to position 1 of the container? Or I guess that's assigning it to be a 1 int.

No it isn't. I would suggest that you read some tutorial on vectors, iterators to understand these in detail or else you will face these problems every now and then.

First of all, inventory should be initialized to contain at least one element for case A (otherwise inventory.begin() is pointing nowhere) and at least two elements for case B,C,D.

Then, you have to understand that an iterator is essentially a pointer (not exactly, but it behaves like one), so setting it to one, if it was allowed by the compiler, which it most certainly does not, would make your iterator point to memory location 1 which is completely meaningless. So you must understand that an iterator is not an index in the array but more like a pointer to an element in the array.

Finally, since iterators are not pointers because they can have more complicated behavior, although in the case of "vector" it is pretty much just a normal pointer, they cannot be assigned to a pointer, so even sending "&inventory[1]" to the insert function will not work because you cannot convert a pointer to an iterator.

You should be more careful when using iterators and arrays and stuff. Look at the documentation (www.cplusplus.com or somewhere else) about each function you use and make no assumptions (like that "myIterator = 1;" is correct!) before you double checked it in the standard documentation. A lot of the above errors will not be caught by the compiler, but not all, and those that are not caught will cause "undefined behavior" which is an expression to remember and look out for when programming in C++ or any other language for that matter. One such problem that would not be caught at compile-time is if you try case A on an empty "inventory" (that will lead to a run-time "segmentation fault" or worse).

This article has been dead for over six months. Start a new discussion instead.