A C + + function template for sorting

Source: Internet
Author: User
Tags arrays comparison sort sorts

When you write an MFC program some time ago, you need to sort a string collection CStringArray. The Standard Template Library STL provides a function template sort that is powerful but has two inconveniences:

1. Sort uses the enumerator (iterator) mechanism to handle C + + arrays (that is, pointers) and STL objects such as vectors, but the MFC collection Class CArray, CStringArray does not provide an enumerator. Although you can convert a collection to a pointer by using the member function GetData of the collection class and then call sort for processing, this destroys the encapsulation of the object.

2. If you use descending sort, you also need to make a different comparison function.

To this end, I wrote a sort of function template, on the one hand to meet their needs, on the other hand, also consolidate the base of C + +. Although the function template is simpler, it supports both C + + arrays and MFC collection classes, which support ascending and descending order, and are sufficient for general applications.

The

Function code is as follows:
//The function template sorts the collection elements by using bubbling, which describes the parameter:
//Collection collection object, the collection object must provide the [] action.
//element collection elements, which are used only to determine the collection element type, the
//parameter's value is not useful, and it is recommended to take the first element of the collection. The collection
//element must provide replication, assignment, and comparison operations. The number of
//Count collection elements
//Ascend Indicates whether the sort uses ascending (true) or descending (false)
///The function template supports C + + Arrays and MFC collection CStringArray, CArray.
Template <typename Collection_type, typename Element_type>
void Bubblesort (collection_type& Collection, Element_type ELEMENT, int count, bool ascend = true)
{
for (int i = 0; i < count-1; i++)
for (int j = 0; J < Count-1-i; J + +)          
if (Ascend)
{
//ascending
if (Collection[j] > collection[j+1])
{
Element_type temp = collection[j];
Collection[j] = collection[j+1];
Collection[j+1] = temp;        
}
}
Else
{
//Descending
if (Collection[j] < collection[j+1])
{
Element_type temp = CollECTION[J];
Collection[j] = collection[j+1];
Collection[j+1] = temp;  
}
}
}
The following code sorts an integer array in ascending order:
int arrayint[] = {A, n, a, 201, Panax Notoginseng, 187};
Bubblesort (Arrayint, arrayint[0], 7);  
The following code sorts the collection of integers in ascending order:
CArray <int, int> collectionint;
Collectionint.add (45);
Collectionint.add (23);
Collectionint.add (76);
Collectionint.add (91);
Collectionint.add (37);
Collectionint.add (201);
Collectionint.add (187);
Bubblesort (Collectionint, collectionint[0], collectionint.getsize ());  
The following code sorts a string array in descending order:
CString arraystring[] = {"Eagle", "Hawk", "Falcon"};
Bubblesort (arraystring, Arraystring[0], 3, false);

The following code sorts a collection of strings in descending order:

CStringArray collectionstring;
Collectionstring.add ("Eagle");
Collectionstring.add ("Hawk");
Collectionstring.add ("Falcon");
Bubblesort (collectionstring, collectionstring[0], Collectionstring.getsize (), false);

Related Article

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.