insert question..

M

Mattias B

Hello!

I have a question about how stl's insert works on containers and on
vector in particular.

The question is: is it OK to insert a value before end() with something
like:

vector<int> v;
v.insert(v.end(), 1);

In SGI's stl documentation it says that the iterator passed to insert
must be a valid iterator in v. Is end() a valid operator or must an
iterator be derefernceable in order to be valid?

Regards,
Mattias
 
V

Victor Bazarov

Mattias B said:
I have a question about how stl's insert works on containers and on
vector in particular.

The question is: is it OK to insert a value before end() with something
like:

vector<int> v;
v.insert(v.end(), 1);

Yes, it is OK. BTW, that's "push_back"s operational semantics
for 'vector', 'list', and 'deque'.
In SGI's stl documentation it says that the iterator passed to insert
must be a valid iterator in v. Is end() a valid operator or must an
iterator be derefernceable in order to be valid?

No, to be valid an iterator has to be either _to_ an element or
"one past the end". That makes 'end()' a valid iterator, AFAIK.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top