Sorting vector values

I am using a vector which has struct type of elements in it. The struct is something like this:

1
2
3
4
5
6
struct block
{
    std::string id1;
    std::string id2;
    std::string last_line;
};


The example value of id1 of first vector element is: id1 = 0001VVYF
The example value of id2 of first vector element is: id2 = 0001VW1S

AND

The example value of id1 of second vector element is: id1 = 0001VW1S
The example value of id2 of second vector element is: id2 = 0001VVYF

i-e

1) the value of id1 of first vector element is equal to the value of id2 of second vector element

2) the value of id2 of first vector element is equal to the value of id1 of second vector element.

In the program I have to find out whether the IDs of one vector element match the IDs of another vector element. I am not concerned about the id1 and id2. I am concerned about the values they hold in any order. It would be easier to compare if similar values appear against the same id e-g

The example value of id1 of first vector element is: id1 = 0001VVYF
The example value of id2 of first vector element is: id2 = 0001VW1S

AND

The example value of id1 of second vector element is: id2 = 0001VVYF
The example value of id2 of second vector element is: id1 = 0001VW1S

Is it possible to sort the vector this way i-e similar values appear against similar IDs?
I'd suggest:

a) changing your struct to a class
b) implementing a < operator for your class
c) using the std::sort function.
Thanks Pax for your reply :)

let me try to explain.

There is a struct and it has got two member variables id1 and id2 and each vector element has these two struct members. I want to find out if the two vector elements contain both these ids (id1 and id2) but in any order. e-g

vector element-1
------------------------
id1 = 0001VVYF
id2 = 0001VW1S

AND

vector element-2
------------------------
id1 = 0001VW1S
id2 = 0001VVYF

both the above vector elements have similar values, both contain 0001VVYF and 0001VW1S but not necessarily against the same variable (id1 or id2) but still the two vector elements would be considered duplicates.

Is there any way I can compare the two vector elements in this way for finding out duplicates. I am just concerned about the values of these two IDs. i-e

if (vect1.id1 == vect2.id1 OR vect1.id1 == vect2.id2) && (vect1.id2 == vect2.id1 OR vect1.id2 == vect2.id2)

then the two vector elements are duplicates of each other.

Many Thanks once again
Yes you can do this. You should simply overload the equality operator of the class (struct) which contains those two data members. i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class foo
{
public:
	int id_1;
	int id_2;
	const bool operator == (const foo & arg)
	{
		if((this->id_1 == arg.id_1 || this->id_1 == arg.id_2) && (this->id_2 == arg.id_1 || this->id_2 == arg.id_2))
		{
			return true;
		}
		return false;
	}
};
It worked :D

BIG THANX Pax :)
Topic archived. No new replies allowed.