Public Group

# 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.

## 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 on other sites
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.

DELETED

##### 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 on other sites
Thanks for all the help

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 on other sites
Quote:
 Original post by Telastynvector.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_|<----- startremove 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_|<----- startstep 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 TelastynI 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 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

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632977
• Total Posts
3009673
• ### Who's Online (See full list)

There are no registered users currently online

×