Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Supplying an iterator when inserting into vector?? (http://www.velocityreviews.com/forums/t511893-supplying-an-iterator-when-inserting-into-vector.html)

desktop 06-04-2007 03:29 PM

Supplying an iterator when inserting into vector??
 
Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<int> t;
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?

Victor Bazarov 06-04-2007 03:38 PM

Re: Supplying an iterator when inserting into vector??
 
desktop wrote:
> Why does insert only work when specifying an iterator plus the object
> to be inserted:
>
> std::vector<int> t;
> 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



Naresh Rautela 06-04-2007 03:39 PM

Re: Supplying an iterator when inserting into vector??
 
On Jun 4, 8:29 am, desktop <f...@sss.com> wrote:
> Why does insert only work when specifying an iterator plus the object to
> be inserted:
>
> std::vector<int> t;
> 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.


desktop 06-04-2007 03:50 PM

Re: Supplying an iterator when inserting into vector??
 
Naresh Rautela wrote:
> On Jun 4, 8:29 am, desktop <f...@sss.com> wrote:
>> Why does insert only work when specifying an iterator plus the object to
>> be inserted:
>>
>> std::vector<int> t;
>> 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?

Naresh Rautela 06-04-2007 04:15 PM

Re: Supplying an iterator when inserting into vector??
 
On Jun 4, 8:50 am, desktop <f...@sss.com> wrote:
> Naresh Rautela wrote:
> > On Jun 4, 8:29 am, desktop <f...@sss.com> wrote:
> >> Why does insert only work when specifying an iterator plus the object to
> >> be inserted:

>
> >> std::vector<int> t;
> >> 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.


Naresh Rautela 06-04-2007 04:16 PM

Re: Supplying an iterator when inserting into vector??
 
On Jun 4, 8:50 am, desktop <f...@sss.com> wrote:
> Naresh Rautela wrote:
> > On Jun 4, 8:29 am, desktop <f...@sss.com> wrote:
> >> Why does insert only work when specifying an iterator plus the object to
> >> be inserted:

>
> >> std::vector<int> t;
> >> 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();
}


Andre Kostur 06-04-2007 04:18 PM

Re: Supplying an iterator when inserting into vector??
 
desktop <fff@sss.com> wrote in news:f41cc2$oai$1@news.net.uni-c.dk:

> Naresh Rautela wrote:
>> On Jun 4, 8:29 am, desktop <f...@sss.com> wrote:
>>> Why does insert only work when specifying an iterator plus the
>>> object to be inserted:
>>>
>>> std::vector<int> t;
>>> 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.

Juha Nieminen 06-04-2007 04:27 PM

Re: Supplying an iterator when inserting into vector??
 
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.

niklasb@microsoft.com 06-04-2007 04:34 PM

Re: Supplying an iterator when inserting into vector??
 
On Jun 4, 9:15 am, Naresh Rautela <nraut...@gmail.com> wrote:
> On Jun 4, 8:50 am, desktop <f...@sss.com> wrote:
>
> > 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<test> instead 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.


red floyd 06-04-2007 04:56 PM

Re: Supplying an iterator when inserting into vector??
 
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();
> }
>



All times are GMT. The time now is 04:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.