• Advertisement

Archived

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

vectors, how to remove parts of the array?

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

say ive got:
vector <int> number;
number.push_back(0);
number.push_back(1);
number.push_back(2);
number.push_back(3);
number.push_back(4);
how would i remove number 2? that is number[2] Thanks,

Share this post


Link to post
Share on other sites
Advertisement
if there is only one thing in the array do i have to use clear, as it doesn''t seem to work if i have only one element in the array.

Share this post


Link to post
Share on other sites
If you only have one element in the array then removing the element at index 2 doesn''t make very much sense.

Share this post


Link to post
Share on other sites
If you routinely remove elements from the front, and don''t need contiguous storage, use a deque instead of a vector, and call pop_front().

Share this post


Link to post
Share on other sites
"Doesn''t seem to work" is rather vague. What''s giving you the impression that it''s not working?

Share this post


Link to post
Share on other sites
when you erase the element do cout << number.size();

and see what it tells you.

Share this post


Link to post
Share on other sites
quote:
Original post by Nervo
when you erase the element do cout << number.size();

and see what it tells you.
Learn to use your debugger instead of littering your code with cout statements. (Yes, you can''t display the contents of Standard Library containers in the debugger in VC6 - natively; expand them and look at their internal data structures.)

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
"Doesn''t seem to work" is rather vague.


You should be used to that answer by now.

Share this post


Link to post
Share on other sites
i''m outputting the size,

when i have 2 elements in the array and use erase, the size goes down to 1.

when i have 1 element in the array and use erase, the size stays at 1.

Share this post


Link to post
Share on other sites

  • Advertisement