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

Supplying an iterator when inserting into vector??

P: n/a
Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
Jun 4 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
desktop wrote:
Why does insert only work when specifying an iterator plus the object
to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?
Uh... 'push_back(value)' does 'insert(end(), value)'. How would you
insert into any place except the end if you don't give an iterator?

And if you wanted an overloaded 'insert' without an interator, what
would the difference be between it and 'push_back'? Do you expect it
to only do "insert into beginning"?

Do you not understand that 'insert' is a generic function to place
the value _anywhere_ in the vector?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 4 '07 #2

P: n/a
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Jun 4 '07 #3

P: n/a
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
>Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?

push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.
Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?
Jun 4 '07 #4

P: n/a
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:
std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();
t.insert(it,33);
If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

- Show quoted text -
You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Since there is no element at start+1, you may be getting a
segmentation violation.

Jun 4 '07 #5

P: n/a
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
Naresh Rautela wrote:
On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the object to
be inserted:
std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();
t.insert(it,33);
If I use push_back instead I don't need to supply the iterator. But what
are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class 'test' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

- Show quoted text -
try this

void main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}

Jun 4 '07 #6

P: n/a
desktop <ff*@sss.comwrote in news:f4**********@news.net.uni-c.dk:
Naresh Rautela wrote:
>On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
>>Why does insert only work when specifying an iterator plus the
object to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?

push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);
If you want bounds-checking... if not, then you can also use:

t[0]

Of course, assuming that there is a 0-th object in the vector.
But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
test* tt2 = new test(707);
t2.insert(it2,tt2);
With this insertion, you may or may not have invalidated any and all
iterators into the container.
*it2->getInt()
This may or may not be dereferencing an invalid iterator. If it is, then
it is Undefined Behaviour. Anything can happen. And, IIRC, the
precedence rules say you'd have to write that as:

(*it2)->getInt()

-(member selection) has a higher precedence than * (dereferencing).
But this does not work. Are iterators only used for inserting and not
extracting custom objects?
They're used for both. But you need to understand when iterators become
invalid. For vectors, any and all iterators into the vector may become
invalid upon insertion if the insertion causes a reallocation of the
vector.
Jun 4 '07 #7

P: n/a
Naresh Rautela wrote:
> std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()
You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Nonsense. The right answer is that it2 gets invalidated when items
are inserted in the vector.
Even if it didn't get invalidated (eg. because of a reserve()) it
would still point to the first element of the vector. Inserting
elements in the vector doesn't change any existing iterator.

Your answer may be correct for eg. a std::list where iterators
are not invalidated after insertion.
Jun 4 '07 #8

P: n/a
On Jun 4, 9:15 am, Naresh Rautela <nraut...@gmail.comwrote:
On Jun 4, 8:50 am, desktop <f...@sss.comwrote:
std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()
But this does not work. Are iterators only used for inserting and not
extracting custom objects?- Hide quoted text -

You are storing the start of the vector(it2) and then inserting at the
start of the vector. Then it2 is no longer the start but start + 1.
Actually, after the insertion the iterator it2 is no longer valid.
Inserting into a vector invalidates any pre-existing iterators into
that vector. Try changing the second-to-last line to:

it2 = t2.insert(it2,tt2);

Also, consider using a vector<testinstead of a vector<test*>. As the
code is written now, you (not the vector) are responsible for deleting
all the test objects you instantiate using new. Also, your code will
leak a test instance if vector::insert throws an exception.

Jun 4 '07 #9

P: n/a
Naresh Rautela wrote:
>
void main()
Illformed code. main *ALWAYS* returns int.

int main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();

test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}
Jun 4 '07 #10

P: n/a
On Jun 4, 9:56 am, red floyd <no.s...@here.dudewrote:
Naresh Rautela wrote:
void main()

Illformed code. main *ALWAYS* returns int.

int main()
{
std::vector<test*t2;
std::vector<test*>::iterator it2;
//it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
it2 = t2.begin();
std::cout << (*it2)->getInt();
}- Hide quoted text -

- Show quoted text -
Thanks for pointing these out and help me learn.

Jun 4 '07 #11

P: n/a
Andre Kostur wrote:
desktop <ff*@sss.comwrote in news:f4**********@news.net.uni-c.dk:
>Naresh Rautela wrote:
>>On Jun 4, 8:29 am, desktop <f...@sss.comwrote:
Why does insert only work when specifying an iterator plus the
object to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don't need to supply the iterator. But
what are the reason the insert only works with an iterator?
push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.
Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

If you want bounds-checking... if not, then you can also use:

t[0]

Of course, assuming that there is a 0-th object in the vector.
>But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();

it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
> test* tt2 = new test(707);
t2.insert(it2,tt2);

With this insertion, you may or may not have invalidated any and all
iterators into the container.
> *it2->getInt()

This may or may not be dereferencing an invalid iterator. If it is, then
it is Undefined Behaviour. Anything can happen.
To avoid this I could just update the iterator after insertion with :
it2 = t2.begin() As long as I make sure to update the iterator
afterwards I am sure that it is still valid right?
Jun 4 '07 #12

P: n/a

desktop wrote in message ...
Andre Kostur wrote:
desktop wrote in ....
But if I have a vector containing my own object that I have defined in
a class 'test' (with a getInt() function returning a private integer)
I would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
it2 == t2.begin(). And since the vector is empty, it2 == t2.end() as
well.
test* tt2 = new test(707);
t2.insert(it2,tt2);
With this insertion, you may or may not have invalidated any and all
iterators into the container.
*it2->getInt()
This may or may not be dereferencing an invalid iterator. If it is,
then
it is Undefined Behaviour. Anything can happen.

To avoid this I could just update the iterator after insertion with :
it2 = t2.begin() As long as I make sure to update the iterator
afterwards I am sure that it is still valid right?
Just for kicks, try:

// .... fill the vector with something
it2 = t2.begin(); // update the iterator
// *it2->getInt(); // ?? *( it2->getInt() ) ??
int num = (*it2)->getInt();

--
Bob R
POVrookie
Jun 5 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.