stl list::unique

hum i am wondering if i can use a function to use unique for my class and also i want to count how much each of them is duplicated. i mean i have (420,250,420,66,444,777,250) in my list i would like to know that 420 is duplicated 2 times and also 250.
Is there a way to get this result ???
Sorry for my english it is not my first language

1
2
list<Patient *> ListePatient
ListePatient.unique(g_nas()); // nas is a attribute of the class patient 


Use a map to count the frequencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <list>
#include <map>

struct patient { int nas ; /* ... */ };

int main()
{
    patient a[] = { { 420 /*,...*/ }, { 250 /*,...*/ }, { 420 /*,...*/ }, { 666 /*,...*/ },
                    { 444 /*,...*/ }, { 777 /*,...*/ }, { 250 /*,...*/ }, { 666 /*,...*/ },
                    { 100 /*,...*/ }, { 666 /*,...*/ }, { 250 /*,...*/ }, { 666 /*,...*/ }}  ;
    std::list< patient* > ListePatient ;
    for( patient& p : a ) ListePatient.push_back( &p ) ;

    std::map<int,int> frequency_counter ;
    for( const auto ptr : ListePatient ) if(ptr) ++frequency_counter[ptr->nas] ;

    for( auto pair : frequency_counter )
        std::cout << pair.first << " appears " << pair.second << " time(s).\n" ;
    std::cout << "#unique values: " << frequency_counter.size() << '\n' ;
}

http://coliru.stacked-crooked.com/a/763f3f03e0087a9e
Thanks man all work :)
Topic archived. No new replies allowed.