Archived

This topic is now archived and is closed to further replies.

Removing an element from a vector

This topic is 5143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

quote:
Original post by Cedric
And, AFAIK, it is equivalent to &v.front() == v.begin() since vector iterators are pointers.

Cédric


hmm wait a min, how can they be the same? If we were doing this to actual pointers using pointer by itself would give us the address of the pointee while using &pointer gives us the address of the pointer. At least that''s how I understand it.

Can you clarify a bit on this part?

Thanks





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by Greatwolf
hmm wait a min, how can they be the same? If we were doing this to actual pointers using pointer by itself would give us the address of the pointee while using &pointer gives us the address of the pointer. At least that''s how I understand it.
IIRC, here is how it goes.

v.front() returns a reference to the first element of the vector. Hence, &v.front() is a pointer to the first element of the vector.

v.begin() returns an iterator to the first element of the vector. For speed and for simplicity, vector iterators are always implemented as pointers, although I''m not sure if it''s a requirement of the Standard or not. So v.begin() is a pointer to the first element of the vector. Just like &v.front(); there is no difference between the two.

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by Cedric
And, AFAIK, it is equivalent to &v.front() == v.begin() since vector iterators are pointers.


Sorry but you can not, and should not, count on that. The iterator could hold more information than just the pointer to the current element.

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

Share this post


Link to post
Share on other sites
quote:
Original post by Cedric
For speed and for simplicity, vector iterators are always implemented as pointers, although I'm not sure if it's a requirement of the Standard or not. So v.begin() is a pointer to the first element of the vector. Just like &v.front(); there is no difference between the two.

Cédric


It is not in the standard and not always implemented - run this on g++ for example:


#include <iostream>
#include <vector>

using namespace std;

template<typename T>
struct trait
{
trait() { cout << "An object\n"; }
};

template<typename T>
struct trait<T*>
{
trait() { cout << "A pointer\n"; }
};

int main()
{
trait<vector<int>::iterator> vt;
}




[edited by - KennithTheMenith on November 11, 2003 7:48:41 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by emileej
I need to remove element #idx from vector v. How is this done? Like this? v.erase(&v.front()+idx);

"On a long enough timeline the survival rate of everyone drops to zero"
- Fight Club



It works but you should never use it!!! Instead use this

erase(begin()+i)

the iterator accepts ''+'' because it''s a random access iterator...

Share this post


Link to post
Share on other sites