4 Replies - 9366 Views - Last Post: 25 October 2011 - 05:28 PM Rate Topic: -----

#1 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,827
  • Joined: 08-August 08

Sort a vector of structs by field name.

Posted 25 October 2011 - 03:50 PM

I'd like to sort a vector of structs using the sort function but I'm not seeing how to do that. I'd like to be able to select which field to sort on, and so I thought about using iterators but I'm not seeing how to use them here.

Here's my test code, which fails on the sort call.
using namespace std;
#include <iostream>
#include <vector>
struct mystruct {
	double A, B, C;
	void show() {
		cout << A << " " << B << " " << C << endl;
	}
	bool operator() (double i, double j) {return (i < j); }
};
vector <mystruct> test;

bool mysort(double i, double j) { return (i<j);}

int main (int argc, char * const argv[]) {
	mystruct temp;
	vector <mystruct>::iterator it;
	int i;
	for (i = 0; i < 10; i++) {
		test.push_back(temp);
		test[i].A = rand() % 1000;
		test[i].B = rand() % 1000;
		test[i].C = rand() % 1000;
		test[i].show();
	}
	cout << endl << endl;
	sort(test.begin(), test.end());
	for (i = 0; i < 10; i++) {
		test[i].show();
	}
	
  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sort a vector of structs by field name.

#2 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Sort a vector of structs by field name.

Posted 25 October 2011 - 04:00 PM

sort uses the < operator, so you could overload that in your struct. Or you could change your compare function to take in two of your structs, and then return true or false depending on what your conditions are.
So for example
struct Example
{
   int x, y;
   bool operator<(const Example & other)
   {
        return (one.x + one.y > two.x + two.y);
   }
};
bool compare(Example one, Example two)
{
    return (one.x + one.y < two.x + two.y);
}

//...

int main()
{
    vector<Example> examples;
    for(int i = 0; i < 10; ++i)
    {
        Example example;
        example.x = rand() % 1000;
        example.y = rand() % 1200;
        examples.push_back(example);
    }
    sort(examples.begin(), examples.end()); // uses the '<' operator that we overloaded
    sort(examples.begin(), examples.end(), compare);
}


This post has been edited by sk1v3r: 25 October 2011 - 04:03 PM

Was This Post Helpful? 1
  • +
  • -

#3 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Sort a vector of structs by field name.

Posted 25 October 2011 - 05:04 PM

Heh, just answered that question 10 minutes ago, 4 posts above this one.

http://www.dreaminco...ir-total-sales/
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,827
  • Joined: 08-August 08

Re: Sort a vector of structs by field name.

Posted 25 October 2011 - 05:27 PM

Thanks, but that gives me the errors:
'one' was not declared in this scope.
'two' was not declared in this scope.

The compare works though.
Was This Post Helpful? 0
  • +
  • -

#5 sk1v3r   User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Sort a vector of structs by field name.

Posted 25 October 2011 - 05:28 PM

Oh yeah sorry about that :stupid:
bool operator<(const Example & other)
{
    return (x + y > other.x + other.y);
}


I was using the variables from the other function.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1