C++ sort() / partial_sort() / partial_sort_copy排序函数用法

本文仅仅发布在CSDN我的博客:点击打开链接

http://blog.csdn.net/pythontojava?viewmode=contents

转载请注明出处。

我用的IDE是VS2013.。代码在vc++6.0不能编译,要把int _tmain(int argc, _TCHAR* argv[]) 改成 int main() 。

====分割线=========

一、sort()排序函数的头文件是<algorithm>,函数对元素进行升序排序。

用法:

1.sort (RandomAccessIterator first, RandomAccessIterator last);

2.sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

更多资料点这里:http://www.cplusplus.com/reference/algorithm/sort/

// sort.cpp : 定义控制台应用程序的入口点。
//IDE是vs2013
#include "stdafx.h"
#include <iostream>
#include <algorithm>    // sort()头文件
#include <vector>
bool compare(int i, int j)//自定义排序规则递减
{
	return(i > j);
}
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int myints[] = { 32, 71, 12, 45, 26, 80, 53, 33 };
	vector<int>v(myints, myints + 8);
	sort(v.begin(), v.begin() + 8);//默认递增排序
	auto i = v.begin();
	for (; i != v.end(); i++)
	{
		cout << *i << ' ';
	}
	cout << endl;
	sort(v.begin(), v.begin() + 8,compare);//递减排序
	for (auto value : v)
	{
		cout << value << ' ';
	}
	system("pause");
	return 0;
}
运行结果:

========分割线========

二、partial_sort()函数,进行部分排序。

用法:

1.partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);

2.partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);

更多资料点这里:http://www.cplusplus.com/reference/algorithm/partial_sort/

// partial_sort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool compare(int a, int b)
{
	return(a > b);//降序
}
int _tmain(int argc, _TCHAR* argv[])
{
	int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	vector<int>vec(myints, myints + 9);
	partial_sort(vec.begin(), vec.begin() + 5, vec.end());//对第0~5个元素进行排序
	for (auto i : vec)
	{
		cout << i << ' ';
	}
	system("pause");
	return 0;
}
运行结果:


========分割线=========

三、partial_sort_copy(),对部分元素进行排序,并复制到另一个容器里。

用法:

1.partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first,RandomAccessIterator result_last);

2.partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first,RandomAccessIterator result_last, Compare comp);

(待排序的容器首指针,尾指针,接收元素的容器的首指针,尾指针)

// partial_sort_copy.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>     // std::cout
#include <algorithm>    // std::partial_sort_copy
#include <vector>       // std::vector
using namespace std;
bool compare(int i, int j)
{
	return (i>j);
}
int _tmain(int argc, _TCHAR* argv[])
{
	int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	vector<int> vec(5);
	partial_sort_copy(myints, myints + 9, vec.begin(), vec.end());
	cout << "vec contains:";
	for (auto it : vec)
	{
		std::cout << ' ' << it;
	}
	std::cout << '\n';
	system("pause");
	return 0;
}
运行结果:

=======分割线==========

四、is_sorted(),判断一系列元素是否有序

// is_sorted.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<array>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	array<int, 5> ar = { 1, 2, 3, 4, 5 };
	cout << is_sorted(ar.begin(), ar.end());//判断一系列元素是否有序,是就返回1
	cout << endl;
	system("pause");
	return 0;
}
运行结果:

=======分割线==========

五、is_sorted_until(),返回有序元素后面无序的第一个元素的指针

用法:

1.is_sorted_until (ForwardIterator first, ForwardIterator last);

2.is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp); //Compare comp自定义比较规则

#include "stdafx.h"
#include <iostream>     // std::cout
#include <algorithm>    // std::is_sorted_until, std::prev_permutation
#include <array>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	array<int, 4> foo={2,3,-5,0};//2、3有序,-5无序
	array<int, 4>::iterator it;
	it =is_sorted_until(foo.begin(), foo.end());//返回指向-5的指针
	cout << *it;
	cout << endl;
	system("pause");
	return 0;
}
运行结果:


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值