Question 2 of vector sorting in C ++

Source: Internet
Author: User
C ++ vector sorting

Recently, I want to output the content in the vector. The results show that some files are opened alternately and repeatedly. So I want to sort the elements in the vector. In the study, I found that the following article is a good summary, share it with me ~

In C ++, when the data type in the vector is of the basic type, we call the STD: sort function to easily implement the ascending and descending sorting of data members in the vector, however, when the data type in a vector is a custom struct type, how can we sort data in ascending and descending order? There are two methods: Method 1:
Let's look at the Code directly. It's relatively simple and easy to understand:
# Include "stdafx. H"
# Include <vector>
# Include <algorithm>
# Include <functional>

Using namespace STD;
Struct assesstypeinfo
{
Unsigned int m_uitype; // type ID
Char m_szname [64]; // Type name
Unsigned int m_uitotal; // total score

Bool operator < (Const assesstypeinfo & RHs) Const // the function that must be written when sorting in ascending order
{
Return m_uitype <RHS. m_uitype;
}
Bool operator> (Const assesstypeinfo & RHs) Const // the function that must be written in descending order
{
Return m_uitype> RHS. m_uitype;
}
}
Int main ()
{
Vector <assesstypeinfo> CTN;

Assesstypeinfo A1;
A1.m _ uitype = 1;
Assesstypeinfo A2;
A2.m _ uitype = 2;

Assesstypeinfo A3;
A3.m _ uitype = 3;

CTN. push_back (A1 );
CTN. push_back (A2 );
CTN. push_back (A3 );
// Sort in ascending order
Sort (CTN. Begin (), cTn. End (), less <assesstypeinfo> (); // or sort (CTN. Begin (), cTn. End ()) The default value is ascending.

For (int I = 0; I <3; I ++)
Printf ("% d \ n", cTn [I]. m_uitype );

// Sort in descending order
Sort (CTN. Begin (), cTn. End (), greater <assesstypeinfo> ());

For (int I = 0; I <3; I ++)
Printf ("% d \ n", cTn [I]. m_uitype );

Return 0 ;
}
The above method can be sorted in ascending order and the output result is 1. 2 3 
Descending Order result 3 2 1.
Method 2: Without modifying the struct or class definition, we use function objects to implement:
# Include "stdafx. H"
# Include <vector>
# Include <algorithm>
# Include <functional>

Using namespace STD;
Struct assesstypeinfo
{
Unsigned int m_uitype; // type ID
Char m_szname [64]; // Type name
Unsigned int m_uitotal; // total score
};

Bool lessmark (const assesstypeinfo & S1, const assesstypeinfo & S2)
{
Return s1.m _ uitype <s2.m _ uitype;
}
Bool greatermark (const assesstypeinfo & S1, const assesstypeinfo & S2)
{
Return s1.m _ uitype> s2.m _ uitype;
}
Int main ()
{
Vector <assesstypeinfo> CTN;

Assesstypeinfo A1;
A1.m _ uitype = 1;
Assesstypeinfo A2;
A2.m _ uitype = 2;

Assesstypeinfo A3;
A3.m _ uitype = 3;

CTN. push_back (A1 );
CTN. push_back (A2 );
CTN. push_back (A3 );

Sort (CTN. Begin (), cTn. End (), lessmark); // sort in ascending order

For (int I = 0; I <3; I ++)
Printf ("% d \ n", cTn [I]. m_uitype );

Sort (CTN. Begin (), cTn. End (), greatermark); // sort in descending order

Return 0 ;
}

The above method can be sorted in ascending order and the output result is 1. 2 3 
Descending Order result 3 2 1.
Method 2 is a simple method.
The above two methods can be selected based on your own needs, and the above two methods are compiled in the VC ++ 6.0 environment, which is also your summary in the practice process, if anything is wrong, you can point out that, for the reason for this use, please refer to the sort section of STL algorithm.

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.