By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

STL vector insert/erase question

P: n/a
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<intv;

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
Dec 23 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Dec 23, 5:29 pm, Boltar <boltar2...@yahoo.co.ukwrote:
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<intv;

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<intv;

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.
Dec 23 '07 #2

P: n/a
On Dec 23, 5:38 pm, johanatan <johana...@gmail.comwrote:
On Dec 23, 5:29 pm, Boltar <boltar2...@yahoo.co.ukwrote:
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<intv;
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<intv;

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

Dec 23 '07 #3

P: n/a
Boltar wrote:
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<intv;

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.
--
Jim Langston
ta*******@rocketmail.com
Dec 23 '07 #4

P: n/a
Boltar <bo********@yahoo.co.ukwrote:
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<intv;

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<intv;

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

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

v.erase(v.begin() + 1);
Dec 23 '07 #5

P: n/a
Boltar <bo********@yahoo.co.ukwrote in news:4b6c3d17-cf41-4860-bee0-
b9**********@i12g2000prf.googlegroups.com:
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<intv;

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

Jan 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.