Sorting for equal_range()

Hello all

I am using 3 vectors each of which is of type struct "ABC". I am trying to find duplicates in each vector using equal_range() method. In order for equal_range() to work, the vectors must be sorted. The criteria for sorting each vector is different, therefore, I can't write a single sort method inside the struct "ABC". Is there any way to write different methods for sorting, one for each vector?

Many Thanks
Thanks Galik
Could you please modify the program below for me.

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
42
43
44
45
46
47
48
// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct mystruct {
string a;
string b;
string c;
string d;
string e;
};

Sort function-1 (does it not have to have some relevance to struct mystruct?)
{
    On basis of variables a, b and c
}

Sort function-2 (does it not have to have some relevance to struct mystruct?)
{
    On basis of variables a, d and e
}

Sort function-3 (does it not have to have some relevance to struct mystruct?)
{
    On basis of variables b, c and d
}

int main () {
  
  vector< mystruct > myvector1;
  vector< mystruct >::iterator it1;

  sort (myvector1.begin(), myvector1.end(),Sort function-1);

  vector< mystruct > myvector2;
  vector< mystruct >::iterator it2;

  sort (myvector2.begin(), myvector2.end(),Sort function-2);

  vector< mystruct > myvector3;
  vector< mystruct >::iterator it3;

  sort (myvector3.begin(), myvector3.end(),Sort function-3);

  return 0;
}
Last edited on
Is this correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
struct sort_one
{
         bool operator () (const mystruct& lhs , const mystruct& rhs)
         {
               return lhs.a < rhs.a; /* but there are two more variables for sorting myvector1 i-e b and c.
Could you please tell me how do I sort myvector1 on the basis of all three variables? */
         }
};

// likewise for the rest two vectors
// ...

sort (myvector1.begin(), myvector1.end(), sort_one());
1
2
3
4
5
6
7
8
9
10
11
12
13
struct sort_one
{
         bool operator () (const mystruct& lhs , const mystruct& rhs)
         {
               return lhs.a < rhs.a; /* but there are two more variables for sorting myvector1 i-e b and c.
Could you please tell me how do I sort myvector1 on the basis of all three variables? */
         }
} mysort;

// likewise for the rest two vectors
// ...

sort (myvector1.begin(), myvector1.end(), mysort);
Thank you bluecoder for simplifying the code by using mysort, but could anyone please help me sorting the vector on three struct variables a,b,c in order for equal_range to work?
Here is an example using just two member variables as sort criteria:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <ctime>
#include <string>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>

struct mystruct
{
	int id;
	std::string a;
	std::string b;
};

bool sort_by_abc(const mystruct& lhs, const mystruct& rhs)
{
	if(lhs.a == rhs.a)
	{
		return lhs.b < rhs.b;
	}
	return lhs.a < rhs.a;
}

int main()
{
	// Initialise random numbers
	std::srand(std::time(0));

	mystruct m;
	std::vector<mystruct> v;
	std::vector<mystruct>::iterator i;

	// Add random strings
	std::string s = "abc";
	for(int i = 0; i < 20; ++i)
	{
		m.id = i;
		std::random_shuffle(s.begin(), s.end());
		m.a = s;
		std::random_shuffle(s.begin(), s.end());
		m.b = s;
		v.push_back(m);
	}


	std::cout << "Before the sort:\n";
	for(i = v.begin(); i != v.end(); ++i)
	{
		std::cout << '\t' << std::setw(2) << i->id << ": ";
		std::cout << i->a << ", " << i->b << '\n';
	}

	std::sort(v.begin(), v.end(), &sort_by_abc);

	std::cout << "After the sort:\n";
	for(i = v.begin(); i != v.end(); ++i)
	{
		std::cout << '\t' << std::setw(2) << i->id << ": ";
		std::cout << i->a << ", " << i->b << '\n';
	}

  return 0;
}
Before the sort:
	 0: cab, bca
	 1: bca, cab
	 2: acb, bac
	 3: cba, cba
	 4: acb, abc
	 5: cba, cab
	 6: abc, cab
	 7: cab, acb
	 8: bac, acb
	 9: acb, abc
	10: bac, cab
	11: acb, cba
	12: acb, cba
	13: acb, cab
	14: abc, cab
	15: abc, acb
	16: cba, bca
	17: acb, bca
	18: cba, cba
	19: bac, bca
After the sort:
	15: abc, acb
	 6: abc, cab
	14: abc, cab
	 4: acb, abc
	 9: acb, abc
	 2: acb, bac
	17: acb, bca
	13: acb, cab
	11: acb, cba
	12: acb, cba
	 8: bac, acb
	19: bac, bca
	10: bac, cab
	 1: bca, cab
	 7: cab, acb
	 0: cab, bca
	16: cba, bca
	 5: cba, cab
	 3: cba, cba
	18: cba, cba
Thanks Galik for writing a whole program :)

How will it help in finding duplicates in a vector of struct?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 // sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct mystruct {
string a;
string b;
string c;
string d;
string e;
};

Sort function-1
{
   // On basis of variables a, b and c
}

Sort function-2
{
    // On basis of variables a, d and e
}

Sort function-3
{
    // On basis of variables b, c and d
}

int main () {
  
  vector< mystruct > myvector1;
  mystruct obj1;
  obj1.a = "x";
  obj1.b = "g";
  obj1.c  = "m";
  obj1.d = "q";
  obj1.e = "l";

  sort (myvector1.begin(), myvector1.end(),Sort function-1);
  equal_range(); // find duplicates in myvector1 if value of a, b, c == value of any other a ,b, c found in myvector1

  vector< mystruct > myvector2;
  mystruct obj2;
  obj2.a = "k";
  obj2.b = "q";
  obj2.c  = "w";
  obj2.d = "s";
  obj2.e = "r";
  
  sort (myvector2.begin(), myvector2.end(),Sort function-2);
  equal_range(); // find duplicates in myvector2 if value of a, d, e == value of any other a ,d, e found in myvector2


  vector< mystruct > myvector3;
  mystruct obj3;
  obj3.a = "f";
  obj3.b = "h";
  obj3.c  = "i";
  obj3.d = "j";
  obj3.e = "z";

  sort (myvector3.begin(), myvector3.end(),Sort function-3);
  equal_range(); // find duplicates in myvector3 if value of b, c, d == value of any other b , c, d found in myvector3


  return 0;
}

Topic archived. No new replies allowed.