Get Unique points of vector of type Point

I have a vector list of type Point. like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Point
{
   int x , int y;
};

vector< Point> myPoints;

int main()
{
//now let suppose we have stored points like this 

a1(2,4);
a2(2,4);
a3(5,4);
a4(0,1);

//How can i remove duplicates points 

}


I should have to get only result this

a1(0,1)
a2(2,4)
a3(5,4)

How can get unique points ?
sort | unique

http://www.cplusplus.com/reference/algorithm/sort/
http://www.cplusplus.com/reference/algorithm/unique/

for sort to work you need to provide a comparison function (you could overload operator<)
(or simply typedef valarray<int> Point;)
Last edited on
Hye @ne555, Could you explain me via sample code for that.
thansk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>

int main ()
{
    // for non-predicate version of std::sort, point needs an operator <
    // for non-predicate version of std::unique, point needs an operator ==
    // for brevity, using a std::pair<> which has both
    using point = std::pair<int,int> ;

    std::vector< point > my_points { {2,4}, {5,4}, {2,4}, {0,1}, {5,4}, } ;
    std::sort( my_points.begin(), my_points.end() ) ;
    my_points.erase( std::unique( my_points.begin(), my_points.end() ), my_points.end() ) ;

    for( point p :  my_points ) std::cout << '(' << p.first << ',' << p.second << ") " ;
    std::cout << '\n' ;
}

http://ideone.com/eo2uuf
Topic archived. No new replies allowed.