c/c++中排序的使用之—sort

sort

sort是STL中提供的算法,頭文件爲#include<algorithm>以及using namespace std; 函數原型如下:
template <class RandomAccessIterator>
  void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

使用第一個版本是對[first,last)進行升序排序,默認操作符爲"<",第二個版本使用comp函數進行排序控制,comp包含兩個在[first,last)中對應的值,如果使用"<"則爲升序排序,如果使用">"則爲降序排序,分別對int、float、char以及結構體排序例子如下:
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;

struct product{
	char name[16];
	float price;
};

int array_int[5]={4,1,2,5,3};
char array_char[5]={'a','c','b','e','d'};
double array_double[5]={1.2,2.3,5.2,4.6,3.5};

//結構比較函數(按照結構中的浮點數值進行排序)
bool compare_struct_float(const product &a,const product &b){
	return a.price<b.price;
}
//結構比較函數(按照結構中的字符串進行排序)
bool compare_struct_str(const product &a,const product &b){
	return string(a.name)<string(b.name);
}
//打印函數
void print_int(const int* a,int length){
	printf("升序排序後的int數組:\n");
	for(int i=0; i<length-1; i++)
		printf("%d ",a[i]);
	printf("%d\n",a[length-1]);
}
void print_char(const char* a,int length){
	printf("升序排序後的char數組:\n");
	for(int i=0; i<length-1; i++)
		printf("%c ",a[i]);
	printf("%c\n",a[length-1]);
}
void print_double(const double* a,int length){
	printf("升序排序後的dobule數組:\n");
	for(int i=0; i<length-1; i++)
		printf("%.2f ",a[i]);
	printf("%.2f\n",a[length-1]);
}
void print_struct_array(struct product *array, int length) 
{ 
    for(int i=0; i<length; i++) 
        printf("[ name: %s \t price: $%.2f ]\n", array[i].name, array[i].price); 
    puts("--");
}
void main()
{
	struct product structs[] = {{"mp3 player", 299.0f}, {"plasma tv", 2200.0f}, 
                              {"notebook", 1300.0f}, {"smartphone", 499.99f}, 
                              {"dvd player", 150.0f}, {"matches", 0.2f }};
	//整數排序
	sort(array_int,array_int+5);
	print_int(array_int,5);
	//字符排序
	sort(array_char,array_char+5);
	print_char(array_char,5);
	//浮點排序
	sort(array_double,array_double+5);
	print_double(array_double,5);
	//結構中浮點排序
	int len = sizeof(structs)/sizeof(struct product);
	sort(structs,structs+len,compare_struct_float);
	printf("按結構中float升序排序後的struct數組:\n");
	print_struct_array(structs, len); 
	//結構中字符串排序
	sort(structs,structs+len,compare_struct_str);
	printf("按結構中字符串升序排序後的struct數組:\n");
	print_struct_array(structs, len); 
}

在編寫compare函數時,可以使用標準庫中的equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。對於這個問題來說,greater和less就足夠了,直接拿過來用:升序:sort(begin,end,less<data-type>());降序:sort(begin,end,greater<data-type>()),使用時需要加入#include<functional>頭文件,例如上面的int排序如果要變成降序排列,可以改成:

sort(array_int,array_int+5,greater<int>());

 

以下轉自:http://www.cppblog.com/mzty/archive/2005/12/15/1770.html

在STL中還有其他排序的函數,分別是:

partion :使得符合某個條件的元素放在前面

stable_partition :相對穩定的使得符合某個條件的元素放在前面

nth_element :找出給定區間的某個位置對應的元素

partial_sort :對給定區間所有元素部分排序

sort :對給定區間所有元素進行排序

stable_sort :對給定區間的所有元素進行穩定排序

若需對vector, string, deque, 或 array容器進行全排序,你可選擇sort或stable_sort; 若只需對vector, string, deque, 或 array容器中取得top n的元素,部分排序partial_sort是首選. 若對於vector, string, deque, 或array容器,你需要找到第n個位置的元素或者你需要得到top n且不關係top n中的內部順序,nth_element是最理想的;若你需要從標準序列容器或者array中把滿足某個條件或者不滿足某個條件的元素分開,你最好使用partition或stable_partition; 若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排序。若你需要得到partial_sort或nth_element的排序效果,你必須間接使用。正如上面介紹的有幾種方式可以選擇。

 

參考:

 http://blog.csdn.net/ajioy/article/details/6976945

http://www.cplusplus.com/reference/algorithm/sort/

http://blog.csdn.net/hqwang4/article/details/5623795

http://apps.hi.baidu.com/share/detail/2056555

http://hi.baidu.com/posinfo/item/dc3e73584c535cc9d2e10c27

http://www.cnitblog.com/luckydmz/archive/2006/05/24/11003.html

http://www.cplusplus.com/reference/std/functional/less/

http://hi.baidu.com/gzyyan/item/c64b5adeb199611de0f46fd4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章