using unique on a vectored structure

I am trying to remove duplicates from a vectored structure. I cannot figure it out. My structure contains only two ints. my code is below. Can someone help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct point {int x;int y;
        friend bool operator==(const point &a, const point &b) {               
           
            return ((b.x==a.x)&&(a.y==b.y));
}

int main(int argc, char** argv) {
   vector<point> endPoints;
   //assume that endPoints is populated with values

   vector<point>::iterator it;
   it=unique(endPoints.begin(),endPoints.end); 
   printMatrix(endPoints);
}


I keep getting errors. Any assistance will be greatly appreciated.

std::unique() removes duplicate elements which are next to each other. Sort the sequence first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

struct point : std::pair<int,int>
{
    using base = std::pair<int,int> ;

    using base::base ; // needs compiler support for inherited constructors

    int& x() { return base::first ; }
    const int& x() const { return base::first ; }

    int& y() { return base::second ; }
    const int& y() const { return base::second ; }
};

int main()
{
    const auto print = [] ( const std::vector<point>& seq )
    {
        for( const auto& pt : seq ) std::cout << '(' << pt.x() << ',' << pt.y() << ") " ;
        std::cout << '\n' ;
    };

    std::vector<point> seq { {1,2}, {3,4}, {1,2}, {5,6}, {3,4}, {1,2}, {3,4} };
    print(seq) ;

    std::sort( seq.begin(), seq.end() ) ;
    seq.erase( std::unique( seq.begin(), seq.end() ), seq.end() ) ;
    print(seq) ;
}


If non-unique points are never required,
consider using std::set<> or std::unordered_set<> instead of std::vector<>
Topic archived. No new replies allowed.