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 :对给定区间的所有元素进行稳定排序

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值