Jump to content
  • Advertisement
Sign in to follow this  
donjonson

vectors (SOLVED)

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

I am using C++ is there an existing method which is part of the std::vector library that will remove a specific element and change the size of the vector accordingly? I could write a function to do this but why do that if it already exists? [Edited by - donjonson on June 15, 2005 2:13:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think you should specify what implementation are you talking about, but supposing C arrays (and any other kind I know) the only way is to take all the elements after the one you want to delete and then writing them starting from this very element.

i.e

2 5 1 8 7 9 0
you want to remove the 8 so you take 7 9 0 and write them starting where is the 8:
2 5 1 7 9 0

Of course you gave to update the elemets counter, if any.

edit:
the following function should (not tested) do the work on a c array:

bool delete(int* a, int* size, int pos)
{
if(!a || !size) return false;
for(int i = elem+1; i < *size; i++) a[i-1] = a;
*size--;
return true;
}


supposing your array is an int array and the pos is starting from 0. After the function call, size will contain the new size of the array.

Share this post


Link to post
Share on other sites
vector.erase(iterator) will remove an element, and I think it re-sizes [but not re-capacity] the vector.

I believe you can do some trickery with the iterator [like doing begin()+5 to go to vector[5] ], but don't quote me on that.

Share this post


Link to post
Share on other sites
Thanks for all the help

your reply lead me to the answer which is the following:

Removing elements from C++ vector:
Removing elements one by one from specified positions in c++ vector is achieved with the erase function.

The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.

str_Vector.erase(str_Vector.begin()+1,str_Vector.begin()+2);

The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.

str_Vector.erase(str_Vector.begin()+1,str_Vector.begin()+3);

If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.



found at
http://www.codersource.net/c++_vector_stl.html

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
vector.erase(iterator) will remove an element, and I think it re-sizes [but not re-capacity] the vector.


typically wont resize the allocated memory, what you can typically expect to happen for std::vector::erase is destroy an element then copies the rest down e.g.:


_____
|__~__|<----- end of storage, capactiy = end of storage - start
|__~__|
|__42_|<----- finish, size = finish - size
|__43_|
|__65_|
|__5__|
|__32_|<----- start

remove element at position 3 using std::vector::erase:

step 1.
_____
|__~__|<----- end of storage, capactiy = end of storage - start
|__~__|
|__42_|<----- finish, size = finish - size
|__43_|
|__~__|<----- element is destroyed (not deallocated)
|__5__|
|__32_|<----- start

step 2., copies elements past the destroyed element downwards.

_____
|__~__|<----- end of storage, capactiy = end of storage - start
|__~__|
|__~__|
|__42_|<----- finish, size = finish - size
|__43_|
|__5__|
|__32_|<----- start


Quote:
Original post by Telastyn
I believe you can do some trickery with the iterator [like doing begin()+5 to go to vector[5] ], but don't quote me on that.


Its not trickery, std::vector's iterators are random accessible iterators so its completely valid.

Share this post


Link to post
Share on other sites
Thats ok I really dont care if it is deallocated or not so long as the size of the vector is changed. Ill worry about that problem when I am writing games with enough data for that problem to make a difference

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!