Solved

STL sorting

Posted on 1998-02-08
1
287 Views
Last Modified: 2013-12-14
I am using the Standard Template Library in a program that involves sorting linked lists.  I have an object defined that contains two variables, int ID and char TYPE.  I also have defined the operator< and operator == which involves the ID variable.  So an STL sort would sort the dequeue by the ID number.  I also want to sort by the TYPE.  For example, If I am sorting by ID number, and two identical number come up, I want to be able to break the tie by choosing the object with the highest TYPE priority.  I would also like to be able to sort by TYPE.  Is there any way to use the STL to sort a dequeue of object by more than one object variable?  
            Thank you
            Chris Blue
0
Comment
Question by:nexguy
1 Comment
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1181541
The standard allows you to pass a comparator to list::sort() as a parameter. However, most compilers do not yet support template member functions, and therefore they do not support this function.

The global sort, however, does not require this feature. It works on a deque, vector and built-in arrays. Here is an example:

#include <vector>
#include <algorithm>

struct MyStruct {
    int i;
    char c;
};

bool Compare_I(const MyStruct s1, const MyStruct s2)
{return (s1.i > s2.i);}

bool Compare_C(const MyStruct s1, const MyStruct s2)
{return (s1.c > s2.c);}

bool Compare_Both(const MyStruct s1, const MyStruct s2)
{
    if (s1.i == s2.i) return (s1.c > s2.c);
    else return (s1.i > s2.i);
}

int main ()
{
    using std::vector;
    using std::sort;

    vector<MyStruct> v;

    // put stuff into v

    sort(v.begin(), v.end(), Compare_I); // sort by first field
    sort(v.begin(), v.end(), Compare_C); // sort by second field
    sort(v.begin(), v.end(), Compare_Both); // sort by both

    return 0;
}


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now