Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
auto end=pointer;
while(beginning!=end){
    duplicate=0;
    next=beginning+1;
    while(next!=end){
        if(function(*next)==function(*beginning)){
           duplicate++; 
        }
        next++;
    }
    if(duplicate>0)
    end=std::remove(beginning,end,function(*next));

    beginning++;
}


What I have tried:

This snippet is part of a function that looks something like this:
auto DeleteEvery(TipElemenata beginning,TipElemenata &pointer)->typename std::remove_reference<decltype(*beginning)>::type*

beginning is a pointer to the first element of the block(vector, array or such)
end is the pointer that points right after the last element of the block
So, I am supposed to remove all elements function returns the same value for. Now I know that function "remove" from library algorithm doesn't actually remove elements, but instead moves them. That's why I'm using the pointer that "remove" returns to indicate the new end of the block. But it seems that it skips some elements. What am I doing wrong?
Posted
Updated 14-Apr-20 20:21pm
Comments
[no name] 14-Apr-20 18:53pm    
Why the hell you expect "next" after the inner while is still valid?
kohi11 14-Apr-20 19:40pm    
Yes, I'm sorry, I should've written the remove function for removing *end in the inner while loop, and added the remove function to delete *beginning after the if(duplicate>0) condition. But it still skips elements.

1 solution

You can always use the erase-remove idiom[^] to remove the elements from vector unless this is a programming school work.

Erase-remove Idiom Revisited[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900