English 中文(简体)
sort vector by more than 1 field
原标题:

How do I sort the below code by name, age and score... all three fields

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

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

 bool by_more_than_1_field( student_t const &lhs, student_t const &rhs )
 {
      // sort by name, age and score
 }

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

 std::sort( students.begin(), students.end(), by_more_than_1_field );
 }
最佳回答

You have to be carefull that your sorting criteria is transitive: if x y then y !< x. If it is not transitive, the sort operation result depends on the ordering of the array before the call, which you probably don t want.

bool by_more_than_1_field( student_t const &lhs, student_t const &rhs )
{
   if (lhs.name < rhs.name) 
      return true; 
   else if (rhs.name < lhs.name)
      return false;
   else // name equals.
      if (lhs. age  < rhs. age ) 
         return true; 
      else if (rhs. age  < lhs. age )
         return false;
      else // age and names equals
         return lhs.score < rhs.score;
}
问题回答

I would write it like so:

bool by_more_than_1_field( student_t const &lhs, student_t const &rhs )
{
    return lhs.name < rhs.name ||
           lhs.name == rhs.name && lhs.age < rhs.age ||
           lhs.name == rhs.name && lhs.age == rhs.age && lhs.score < rhs.score;
}

This will allow you to sort the records prioritizing by name, age and score in that order. Changing the priorities around should be a simple exercise.

bool by_more_than_1_field( student_t const& lhs, student_t const& rhs )
{
    if( lhs.name < rhs.name )
        return true;
    else if( lhs.age < rhs.age )
        return true;
    else if( lhs.score < rhs.score )
        return true;

    return false;
}

Much easier to maintain than the others, and cleaner too!

#include <string>
#include <vector>
#include <algorithm>
struct student_t
 {
      std::string name;
      int age, score;
 };

bool by_more_than_1_field( student_t const &lhs, student_t const &rhs ){
      if(lhs.name ==rhs.name && lhs.age < rhs.age) return lhs.score<rhs.score;
      if(lhs.name==rhs.name) return lhs.age<rhs.age;
      return lhs.name<rhs.name;

}

Explanation : when you make a vector and apply a sort function pass this function as parameter.
ex. vector<strudent_t> st;
and then for sorting sort(st.begin(),st.end(), by_more_than_1_field)

What this will do is the function accepts two const arguments of class student_t. It accepts const so the object student_t is not modifiable. It then compares as given in function.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签