# remove algorithm

• 01-26-2008
dra
remove algorithm
Okay, my problem is to create a function that emulates the remove algorithm in <algorithm>.

so far this is what I have:

Code:

```template<class X, class T> void remove(X& b, X& e, T test){         while(b != e){                                 if(*b == test)                         ++b;                 else{                         X temp = b;                         while ( temp != test ) ++temp;                         swap(*b, *temp);                         b = temp;                 }         } }```
but it doesn't quite work...

Code:

```vector<int> vec; //input 5 1 5 vec.push_back(5); vec.push_back(1); vec.push_back(5); remove(vec.begin(), vec.end(), 5);```
printing vec gives me: 1 1 5

Also, the real remove function returns a iterator...but that shouldn't be too hard.

Can someone help me show me what my version is doing wrong?
• 01-26-2008
indigo0086
What are your errors and where do they happen, post the compiler log.
• 01-26-2008
dra
I don't get any errors...I just don't get the expected output.

5 1 5 and remove(vec.begin(), vec.end(), 5) should yield 1 5 5, but instead i get 1 1 5;
• 01-26-2008
tabstop
It is not clear what you're trying to do here. Are you trying to swap all the removed ones to the front, or something? It's also not clear what's going to happen if you walk past e in the while loop with temp and test. I mean, what happens if you try to remove 5 from "1 2 5 3 5 3"?
• 01-26-2008
dra
Well my goal is to create a function that works just like the remove function in <algorithm> remove(b, e, t) which moves all values not equal to t to the front of the container...
• 01-26-2008
indigo0086
You should take pointers to T rather than T itself, and iterate through the program.
• 01-26-2008
tabstop
I'm pretty sure you've gotten "front" and "back" backwards, since your algorithm leaves the removed values alone, and takes the nonremoved values and moves them forward. Maybe you should switch your uses of ==test and !=test; this way your code would leave the nonremoved values alone, and move the removed values forward.
• 01-26-2008
CornedBee
Your remove logic is quite broken, too. You lose track of your current position, and you swap to the wrong position. Oh, and you swap from the wrong position, because remove() must be stable.

On the other hand, the content of the elements after the return iterator is unspecified. There's no need to swap anything there.

Here's what I'd do.
Code:

```template <typename FwdIt, typename T> FwdIt remove(FwdIt first, FwdIt last, T death) {   for(; first != last && *first != death; ++first) {}   if(first == last) return first;   FwdIt target = first;   for(++first; first != last; ++first) {     if(*first != death) {       *target = *first;       ++target;     }   }   return target; }```
• 01-26-2008
dra
Thanks for the help...

problems like these always seem so difficult...

Edit:
I went back and I looked over what remove actually does, and it seems I wasn't understanding it correctly. I was under the impression that the elements in the container should be arranged or sorted according to the value given, which is incorrect.