Archived

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

zip7000

delete an element of a vector

Recommended Posts

Hello, I have several questions about vector data structure from STL If I have a vector V of 10 elements, I can access to an element by : 1 -V[3] //access to the fourth element of the vector V. Right? 2 - How can I delete the third element from V ?(how to use the erase method?) 3 - If I delete the fourth element(V[3]) is all the others element which are after will be shifted? otherwise what happened if I try to acces to v[3]? thank you zip7000

Share this post


Link to post
Share on other sites
You call remove first, which moves the elements to the end of the container, and then call erase with the iterator returned by removed.

For instance, this removes all the 0's from a vector of int's

      
std::vector<int> test;
test.erase(std::remove(test.begin(), test.end(), 0));


Yes they'll be shifted.
I just noticed that you wanted to remove a particular position, not a value. For that you can just call erase:


  
std::vector<int> test;
test.erase(&test[3]);



There's also remove_if which takes a functor to determine what to remove (which needs to be followed by a call to erase as above).

[edit: fixed the code bug]

[edited by - Magmai Kai Holmlor on August 13, 2002 6:24:47 PM]

Share this post


Link to post
Share on other sites
Thank you for your answer. I tried to do what you said but it doesn''t seem to work. Here that I wrote:

1 - I create a vector of nest called m_Nests
std::vector m_nests;

2 - I create a remove function
void Garden::RemoveNest(int posX, int posY)
{
int ind = GetIndexFromPosition(posX,posY);
m_nests.erase(m_nests[ind]);
}

but m_nests.erase(m_nests[ind]) gives me the error message:

H:\antSim2\Garden.cpp(45) : error C2664: ''class Nest *__thiscall std::vector >::erase(class Nest *)'' : cannot convert parameter 1 from ''class Nest'' to ''class Nest *''

morever I was wondering if I could use the remove method to erase all the elements(=the nests objects) which have their x an y position equal to given values. I could write something like:

m_nests.erase(std::remove(m_nests.begin(), m_nests.end(), posX, poY);

where posX and posY are two given values.

any suggestion?

zip7000

Share this post


Link to post
Share on other sites
You can remove elements from an STL vector using its erase() function.

-----
std::vector vecInt;

for (int i = 0; i < 10; ++i;
vecInt.push_back(i);

// Remove element at index 3 to 8
// Note the end index is not included
// erase() will return the position of the next element

vecInt.erase(3, 9);
-----

Kuphryn

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just call erase() with the offset of the element you want to erase:

myvector.erase(5);

do NOT do this while iterating through the vector because it will change end()

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
I just noticed that you wanted to remove a particular position, not a value. For that you can just call erase:

std::vector< int > test;
test.erase(test[3]);


This doesn''t work IMO. erase expects an iterator.

And this:
quote:
By AP
myvector.erase(5);


doesn''t work on my compiler either, for the same reasons.

I would write:

test.erase(test.begin() + 3);

Cédric

Share this post


Link to post
Share on other sites
oops,
test.erase(&test[3]);

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ok. test(v.begin() +3) seems to work. however I still have some difficulties to use the vector. For instance what''s wrong with that?
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);

when I start the debugger I can only see 10 in the member ''first''. The members ''last'' and ''end'' don''t store the value 20 and 30. Why???????

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
oops,
test.erase(&test[3]);


Ah, better, but still, that wouldn''t work with deques, would it? I prefer begin() + 3.

quote:
By AP
when I start the debugger I can only see 10 in the member ''first''. The members ''last'' and ''end'' don''t store the value 20 and 30. Why???????


This behavior is correct. You''re doing the right thing. It''s just that the debugger is not very useful with standard containers, IMO.

Cédric

Share this post


Link to post
Share on other sites