Jump to content
  • Advertisement
Sign in to follow this  
clapton

STL vector::erase()

This topic is 5133 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

Hello! I'd like to know how to remove an arbitrary element from a vector. Is this piece of code correct ?
int* a, b, c;

a = new int;
b = new int;
c = new int;

*a = 1;
*b = 2;
*c = 3;

vector <int*> pVecInt;

pVecInt.push_back(a);
pVecInt.push_back(b);
pVecInt.push_back(c);

delete pVecInt[1];
pVecInt.erase( pVecInt.begin() + 1 );

Thanks ! :)

Share this post


Link to post
Share on other sites
Advertisement
You shouldn't/needn't be storing pointers to integers for what your trying to do, only store pointers in containers when necessary, this isn't it.


std::vector<int> vec_of_ints;

vec_of_ints.push_back(1);
vec_of_ints.push_back(2);
vec_of_ints.push_back(3);

vec_of_ints.erase(vec_of_ints.begin() + 1);

Share this post


Link to post
Share on other sites
Aside from the point made by snk_kid, "int* a, b, c" means that a is an int*, and b and c are ints. You would want "int *a, *b, *c". It helps to pair * with the name instead of the type so you don't get confused :)

The rest looks correct, but you may want to rewrite things so that they're a little more obvious:


vector<int *>::iterator element = pVecInt.begin() + 1;
delete *element;
pVecInt.erase(element);

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
You shouldn't/needn't be storing pointers to integers for what your trying to do, only store pointers in containers when necessary, this isn't it.


Why is that so? I used ints only to make the example more obvious. Where would I want to use a vector of pointers?

Quote:
Aside from the point made by snk_kid, "int* a, b, c" means that a is an int*, and b and c are ints. You would want "int *a, *b, *c".


Sure thing. :)

Summing up, they way I erase elements from vectors is OK.

Thanks :)

Share this post


Link to post
Share on other sites
Quote:
Original post by clapton
Quote:
Original post by snk_kid
You shouldn't/needn't be storing pointers to integers for what your trying to do, only store pointers in containers when necessary, this isn't it.


Why is that so? I used ints only to make the example more obvious. Where would I want to use a vector of pointers?

Pointers are better for large data structures or classes. When you allocate a pointer, you're taking up space on the heap. When you just declare a normal int or struct/class, you're taking up space on the stack. The heap is a lot bigger than the stack. Therefore it's better to make large structures pointers so you don't run out of stack space.

Bottom line is that there's nothing wrong with declaring an array of ints, it's just that it's almost always unnecessary since they take up meager stack space (unless it's a very big array [smile])

Share this post


Link to post
Share on other sites
Quote:
Original post by Gaiiden
(unless it's a very big array [smile])

In which case you would be storing a vector of ints [grin] instead of a pointer to an array anyways.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!