How to remove all consecutive duplicate elements from std::forward_list in C++

1 Answer

0 votes
#include <iostream>
#include <forward_list>
 
int main()
{
    std::forward_list<int> fl = {1, 2, 2, 7, 7, 4, 1, 1, 8, 8, 8, 9};
 
    std::cout << "before:";
    for (auto val : fl)
        std::cout << ' ' << val;
    std::cout << '\n';
 
    fl.unique();
    std::cout << "after:";
    for (auto val : fl)
        std::cout << ' ' << val;
    std::cout << '\n';
}


 
/*
run:
   
before: 1 2 2 7 7 4 1 1 8 8 8 9
after: 1 2 7 4 1 8 9
  
*/


 



answered Dec 14, 2024 by avibootz

Related questions

1 answer 181 views
1 answer 91 views
1 answer 171 views
1 answer 207 views
3 answers 167 views
...