Sort STL vector by multi-Field Values

Source: Internet
Author: User

In the previous article, we talked about how to sort keys of STL map by multiple values. In the final conclusion, we said, "Generally we do not use the sort function in STL algorithm to sort a map, but the sort function can be used to sort the elements of a vector."

 

The following is a complete sample code for sorting a vector by multiple field values:

# Include <iostream> # include <vector> # include <string> # include <algorithm> using namespace STD; class student {PRIVATE: int ID; // student ID string name; // float eyesight; // float height; // float Chinese; // float English; // float math; // public: student (int id, string name, float eyesight, float height, float Chinese, float English, float math) {This-> id = ID; this-> name = Name; this-> eyesight = E Yesight; this-> Height = height; this-> Chinese = Chinese; this-> English = English; this-> math = math;} int get_id () {return ID ;} string get_name () {return name;} float get_eyesight () {return eyesight;} float get_height () {return height;} float get_chinese () {return Chinese;} float get_english () {return English ;}float get_math () {return math ;}}; // compare the size of a function (predicate) bool comparer (student & stu_a, student & stu_ B) {// by eyesight Ascending + height ascending order if (stu_a.get_eyesight ()! = Stu_ B .get_eyesight () Return (stu_a.get_eyesight () <stu_ B .get_eyesight (); elsereturn (stu_a.get_height () <stu_ B .get_height ()); // sort by eyesight in descending order ° + height in descending order // If (stu_a.get_eyesight ()! = Stu_ B .get_eyesight () // return (stu_a.get_eyesight ()> stu_ B .get_eyesight (); // else // return (stu_a.get_height ()> stu_ B .get_height ()); // sort by eyesight in ascending order + height in descending order // If (stu_a.get_eyesight ()! = Stu_ B .get_eyesight () // return (stu_a.get_eyesight () <stu_ B .get_eyesight (); // else // return (stu_a.get_height ()> stu_ B .get_height ()); // sort by eyesight in descending order + height in ascending order // If (stu_a.get_eyesight ()! = Stu_ B .get_eyesight () // return (stu_a.get_eyesight ()> stu_ B .get_eyesight (); // else // return (stu_a.get_height () <stu_ B .get_height ());} int main (INT argc, char ** argv) {vector <student> VEC; Vec. push_back (student (4, "Dudley", 1.1f, 170.2f, 90.5f, 89.5f, 93.0); Vec. push_back (student (3, "Chris", 1.1f, 163.4f, 93.5f, 90.0f, 83.5f); Vec. push_back (student (2, "Bob", 1.5f, 166.6f, 86.0f, 98.5f, 85.0f); Vec. P Ush_back (student (1, "Andrew", 1.5f, 173.2f, 98.5f, 100366f, 100.f)); // call the sort function in STL. The third parameter is defined previously, the sort (VEC. begin (), VEC. end (), comparer); vector <student>: iterator ITER; For (iter = Vec. begin (); iter! = Vec. end (); ++ ITER) {cout <(* ITER ). get_eyesight () <"\ t" <(* ITER ). get_height () <Endl;} return 0 ;}

The output result is:

1.1 163.4

1.1 170.2

1.5 166.6

1.5 173.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.