how to remove duplicate entries in a vector of Pair?

A

Allerdyce.John

I have a vector of Pair of int:

typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector

I would like to remove entries if their first are equal, or if their
value is swap ( first of first pair equals to seconds of second pair):

bool equals(MyPair src, MyPair dest ) {
if (( src.first == dest.first) && ( src.second == dest.second))
return true;

if (( src.first == dest.second) && ( src.second == dest.first))
return true;

return false;
}

I think I want to use the 'unique' algorithm and use the above 'equals'
function as the binary predicate.
But before I use the 'unique' algorithm, I need to sort my list. But
for my purpose, I am not sure how to sort my list. base on the 'first'
value? or base on the second value?

Thank you for any pointers.
 
M

Mark P

I have a vector of Pair of int:

typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector

I would like to remove entries if their first are equal, or if their
value is swap ( first of first pair equals to seconds of second pair):

What you say above is not equivalent to what you write below, but I'll
assume that what you mean is what you've implemented below.
bool equals(MyPair src, MyPair dest ) {
if (( src.first == dest.first) && ( src.second == dest.second))
return true;

if (( src.first == dest.second) && ( src.second == dest.first))
return true;

return false;
}

I think I want to use the 'unique' algorithm and use the above 'equals'
function as the binary predicate.
But before I use the 'unique' algorithm, I need to sort my list. But
for my purpose, I am not sure how to sort my list. base on the 'first'
value? or base on the second value?

Sort by the smaller of the two pair elements, resolving ties by the
larger of the two pair elements.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top