Sort, find duplicates and remove from vector

Hi,
I'm trying to sort my vector, find duplicate values using unique and erase them. Program can compile successfully but the duplicates are not removed.

output:

1
2
3
4
5
6
7
8
9
10
11
x: 3

y: 2

x: 6

y: 4

x: 3

y: 2


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
34
35
36
37
38
39
40
41
vector<Point2D> p2dvector;

void readData()
{
    cout<< "Please enter filename : ";
    cin >> inputFile;

    fstream fileStream;
        fileStream.open(inputFile.c_str(),fstream::in);
        int records = 0;

    while( fileStream.good() ) 
    {
        string line = "";
        while (getline (fileStream, line))  
        {   
            stringstream ss (line);
            getline (ss, className, ',');

            if (className == "Point2D")
            {   
                int x, y;
                getline (ss, p2dX, '[');
                getline (ss, p2dX, ',');

                getline (ss, p2dY, ' ');
                getline (ss, p2dY, ']');

                istringstream (p2dX) >> x;
                istringstream (p2dY) >> y;

                Point2D p2d (x, y);
                p2dvector.push_back(p2d);

                sort(p2dvector.begin(), p2dvector.end());
                p2dvector.erase(unique(p2dvector.begin(), p2dvector.end()), p2dvector.end());
                
}
}
}
}


I also have operator== in my Point2D class which I understand it is required to use unique for vector.

1
2
3
4
bool operator==(const Point2D& lhs, const Point2D& rhs)
{
	return lhs.x == rhs.y;
}


Can you advise where I'm doing wrong? Any kind of advice is appreciated as I'm stuck.
Thanks!

Last edited on
closed account (Dy7SLyTq)
for the sorting... i havent done too much of those kind of algorithims; but a good one is the bubble sort. its easy to implement and its the only one i remember
Yea, you're right. Bubble sort is so much easier. But it's a requirement in my assignment that I need to use sorting algorithms available from STL and write comparator functions. :(
closed account (Dy7SLyTq)
oh sorry didnt see that. just use std::sort then
umm yea but i'm trying to remove duplicates values from vector. Any idea what's wrong with my code coz it's not doing the job.
can anyone advise please?
Don't sort while you are reading the file. Sort after you have read the file.

You don't need lines 12 or 26.

Hope this helps.
Last edited on
You're comparing the x coordinate of one point with the y coordinate of another to check for equality?

Don't you want:
1
2
3
4
bool operator==(const Point2D& lhs, const Point2D& rhs)
{
	return (lhs.x == rhs.x && lhs.y == rhs.y);
}

Am I missing something here? Why not use a std::set?
@LB: I agree, but TC said that the assignment requires the writing of comparator functions and the use of STL sorting functions, so it might not be allowed.
@Duoas: omg thank you so much! Duplicates are now removed. :D

@norm b: I'm comparing x and y values of Pt1 and Pt2 so you code above is what I need. Thanks :)

@L B and Zhuge: thank you guys for taking the time to comment too.
Topic archived. No new replies allowed.