I had found that list::unique()
removes only consecutive elements in a list. I would like to know the reason why the method functions like this.
Remove duplicates from a list explains how to remove the duplicate elements from a list but it doesn't explain why just the consecutive elements are only removed.
The following is my test code:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <utility>
using namespace std;
void print_message( const pair<int,string>& message )
{
cout << message.first << " - " << message.second << endl;
}
int main( )
{
list< pair< int, string > > message;
list< pair< int, string > >::iterator ip;
message.push_back( make_pair( 1, string("Test Foo One")) );
message.push_back( make_pair( 1, string("Test Foo Two")) );
message.push_back( make_pair( 1, string("Test Foo Two")) );
message.push_back( make_pair( 1, string("Test Foo Three" )) );
message.push_back( make_pair( 1, string("Test Foo Two" )));
message.push_back( make_pair( 1, string("Test Foo Two")) );
cout << "ORIGINAL MESSAGES" << endl;
ip = message.begin();
while(ip!=message.end()) {
print_message(*ip);
ip++;
}
message.unique();
cout << "\nSEQUENTIAL DUPLICATES REMOVED" << endl;
ip = message.begin();
while(ip!=message.end()) {
print_message(*ip);
ip++;
}
}
Why is this method list::unique()
designed just to remove consecutive duplicate elements in the list?