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

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