STL vector insert/erase question

B

Boltar

Hi

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

eg:

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(1,456);
printf("%d\n",v[1]);

v.erase(1)

So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.

Sorry if this is a dummies question.

B2003
 
J

johanatan

Hi

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

eg:

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(1,456);
printf("%d\n",v[1]);

v.erase(1)

So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.

Sorry if this is a dummies question.

B2003

Why not just do:

vector<int> v;

v.push_back(123);
v.push_back(789);

vector<int>::iterator it = v.begin();
it = v.insert(++it,456);
printf("%d\n",v[1]);

v.erase(it) // previously, your code would have deleted 456 and not
789, if you want to delete 789, then use ++it here instead

Short answer, I don't think there's a way to do what you want unless
there a find (that accepts an index and returns an iterator) as some
of the other STL containers have.
 
J

johanatan

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

vector<int> v;
v.push_back(123);
v.push_back(789);
v.insert(1,456);
printf("%d\n",v[1]);
v.erase(1)

So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.
Sorry if this is a dummies question.

Why not just do:

vector<int> v;

v.push_back(123);
v.push_back(789);

vector<int>::iterator it = v.begin();
it = v.insert(++it,456);
printf("%d\n",v[1]);

v.erase(it) // previously, your code would have deleted 456 and not
789, if you want to delete 789, then use ++it here instead

Short answer, I don't think there's a way to do what you want unless
there a find (that accepts an index and returns an iterator) as some
of the other STL containers have.

P.S. If you want to deal with non-trivial indices with the iterator,
you should also be able to do:

it = v.begin();
it += n; // where n is > 1
 
J

Jim Langston

Boltar said:
Hi

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

eg:

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(1,456);
printf("%d\n",v[1]);

v.erase(1)

So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.

Sorry if this is a dummies question.

B2003

From your simple example and explanation, it sounds like you simply want to
do:

v[1] = 456;

replacing the 789 at index 1.

Otherwise I am not understanding your question.
 
D

Daniel T.

Boltar said:
Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?
Yes.

eg:

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(1,456);
printf("%d\n",v[1]);

v.erase(1)

So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.

Sorry if this is a dummies question.

NP. There is a simple way to do what you want. begin() + index is the
iterator for the index you want. So...

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(v.begin() + 1,456);
printf("%d\n",v[1]);

v.erase(v.begin() + 1);
 
J

Joe Greer

Hi

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

Sure. The general pattern to convert an index to an iterator is to add
begin() to the index. That is....

iterator it = v.begin() + index;

Of course, you have to do your own bounds checking.

eg:

vector<int> v;

v.push_back(123);
v.push_back(789);

v.insert(1,456);

This would be:
v.insert(v.begin() + 1, 456);
printf("%d\n",v[1]);

v.erase(1)
This would be:
v.erase(v.begin() + 1);
So the above would print 456 but 789 would now be at index 2. Then it
would delete 789.

Sorry if this is a dummies question.

B2003

Generally speaking, the index functions of vector are just convenience
functions. The api is really more attuned to iterator use and you
should probably just get used to using iterators instead of indexes for
most things. Having said that, I realize that there are legitimate
needs for index based uses of vectors, but most of the time, iterators
work just as well and the methodology is portable between container
types.

joe
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
says...
Hi

Is there a way of inserting and erasing to/from a vector using an
array index instead of an iterator or alternatively a way to map an
index number to an iterator?

You can't do much directly with an index except look at/modify that item
in the vector. Converting an index to an iterator consists of adding
'begin()' to it.

OTOH, wanting to insert/delete items from particular spots in the vector
tends to suggest that you're trying to maintain some ordering on the
items as you insert and/or delete them. If that's the case, there's a
good chance that a collection intended for that kind of usage (e.g. set
or multiset) will work better for you.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top