sort more than one variable in STL

J

jack

Hi there,

Does STL provide some function to sort several variables together by
one of them? Say I have 100 student's names, age and scores, and I
want to sort them by score only.

I know I can modify the quicksort code, but just want something quick
and neat.

Thank you for your help.

jack
 
R

Rob Williscroft

jack wrote in in
comp.lang.c++:
Hi there,

Does STL provide some function to sort several variables together by
one of them? Say I have 100 student's names, age and scores, and I
want to sort them by score only.

I know I can modify the quicksort code, but just want something quick
and neat.

Thank you for your help.

Yes std::sort() can take an optional "Functor" argument, a simple
function that returns true if the `left` is less than the `right`
will do:

#include <string>
#include <vector>
#include <algorithm>

struct student_t
{
std::string name;
int age, score;
};

bool by_score( student_t const &lhs, student_t const &rhs )
{
return lhs.score < rhs.score;
}

int main()
{
std::vector< student_t > students;
// populate students

std::sort( students.begin(), students.end(), by_score );
}

Rob.
 
R

Robbie Hatley

jack said:
Hi there,

Does STL provide some function to sort several variables together by
one of them? Say I have 100 student's names, age and scores, and I
want to sort them by score only.

I know I can modify the quicksort code, but just want something quick
and neat.

Thank you for your help.

jack

You could use a multimap. Something like the following should work for you.
I've used this kind of scheme before to solve this kind of issue:

#include <iostream>
#include <map>
#include <utility>
#include <string>

struct StudentData
{
std::string name;
int age;
int score;
};

int main(void)
{
std::multimap<int, StudentData> StudentMap;
StudentData temp;

temp.name = "Ralph";
temp.age = 7;
temp.score = 86;
StudentMap.insert(std::make_pair(temp.score, temp));

temp.name = "Tom";
temp.age = 9;
temp.score = 52;
StudentMap.insert(std::make_pair(temp.score, temp));

temp.name = "Susan";
temp.age = 8;
temp.score = 94;
StudentMap.insert(std::make_pair(temp.score, temp));

temp.name = "Fred";
temp.age = 10;
temp.score = 86;
StudentMap.insert(std::make_pair(temp.score, temp));

// The above will automatically sort students by score.
// Use lower_bound(), upper_bound(), and equal_range() to get
// iterators to students with particular scores. For example, the
// following will print all the students who scored each possible score
// from 0 to 100:

typedef std::multimap<int, StudentData>::iterator SMI;
std::pair<SMI, SMI> Range;
for (int score = 0; score<=100; ++score)
{
Range = StudentMap.equal_range(score);
if (Range.first == Range.second) continue;
std::cout << "Students with score " << score << ":" << std::endl;
for (SMI iter = Range.first; iter != Range.second; ++iter)
{
std::cout << (*iter).second.name << std::endl;
}
}
}

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
R

Roberto =?ISO-8859-15?Q?D=EDaz?=

Does STL provide some function to sort several variables together by
one of them? Say I have 100 student's names, age and scores, and I
want to sort them by score only.

I am not sure if I do understand what you want to do...

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;


class student
{
friend ostream &operator<<(ostream &o, const student &e);
private:
int score;
int age;
char *name;
public:
student (int s) : score (s) {}
int get_score () const { return score;}
bool operator<(const student &e) const { return score < e.score; }
};

ostream &operator<<(ostream &o, const student &e)
{
return o << e.score;
}

int
main ()
{
vector<student> v;
for (int i = 0; i < 20 ; i++)
v.push_back (student (i));

// add some noise
random_shuffle (v.begin(), v.end());
ostream_iterator<student> outp (cout, " ");
cout << "student scores before sorting: " << endl;
copy (v.begin(), v.end(), outp);
cout << endl;
cout << "now we sort: " << endl;
sort (v.begin(), v.end());
copy (v.begin(), v.end(), outp);
cout << endl;

return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top