sort more than one variable in STL

Discussion in 'C++' started by jack, Jun 4, 2004.

  1. jack

    jack Guest

    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
    jack, Jun 4, 2004
    #1
    1. Advertising

  2. jack wrote in news: 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.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 4, 2004
    #2
    1. Advertising

  3. "jack" <> wrote in message news:...
    > 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






    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Robbie Hatley, Jun 5, 2004
    #3

  4. > 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;
    }
    Roberto =?ISO-8859-15?Q?D=EDaz?=, Jun 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Hamm
    Replies:
    0
    Views:
    422
    Michael Hamm
    Nov 15, 2006
  2. Merciadri Luca
    Replies:
    4
    Views:
    802
  3. Bob Rashkin
    Replies:
    5
    Views:
    66
    Dennis Lee Bieber
    Dec 23, 2013
  4. Steven D'Aprano
    Replies:
    0
    Views:
    81
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    75
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page