C++ STL(2)--算法(2)

本文详细介绍了C++STL中的sort、stable_sort、partial_sort、partial_sort_copy、nth_element、partition、stable_partition和is_sorted等排序函数,以及它们的实现原理、用法和特性,特别关注了自定义对象排序的需求和移动构造函数/赋值的重要性。
摘要由CSDN通过智能技术生成
算法(2)----STL里的排序函数。
1. sort: 对容器或普通数组中指定范围内的元素进行排序,默认进行升序排序。

    sort函数是基于快速排序实现的,属于不稳定排序。

    只支持3种容器:array、vector、deque。

    如果容器中存储的是自定义的对象,则该类必须提供移动构造函数和移动赋值运算符。

    代码示例:

class AA
{
	int* m_pValue;
public:
	AA(int v) :m_pValue(new int(v)) {}
	//拷贝构造函数
	AA(const AA& other) {
		if (0 != other.m_pValue) {
			this->m_pValue = new int(*other.m_pValue);
		}
		else {
			this->m_pValue = 0;
		}
	}
	//析构函数
	~AA() {
		delete m_pValue;
	}
	//移动构造函数
	AA(AA&& other) noexcept : m_pValue(other.m_pValue) {
		other.m_pValue = 0;
	}
	//移动赋值操作符
	AA& operator = (AA&& other) noexcept {
		if (this != &other) {
			delete m_pValue;
			m_pValue = other.m_pValue;
			other.m_pValue = 0;
		}
		return *this;
	}
	//比较操作符
	bool operator < (AA& other) {
		if (0 != this->m_pValue && 0 != other.m_pValue) {
			return *this->m_pValue < *other.m_pValue;
		}
		return false;
	}
	void Print() {
		if (0 != this->m_pValue) {
			cout << *this->m_pValue << " ";
		}
		else {
			cout << "null" << " ";
		}
	}
};

int main() {
	std::vector<AA> v1{ 5,6,9,8,3,2,1,4 };
	for_each(v1.begin(), v1.end(), mem_fun_ref(&AA::Print));
	cout << endl;

	sort(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), mem_fun_ref(&AA::Print));
	return 0;
}
2. stable_sort: 排序后保证相等元素的相对位置和排序前是一样的。

    stable_sort函数是基于归并排序实现的,属于稳定排序。用法和sort一样。

3. partial_sort(first, middle, last)
    从指定范围内选出(middle-first)个最小的元素并排序存放在 [first,middle) 区间。

    代码示例:

void printInt(int val)
{
	cout << val << "  ";
}
int main() {
	std::vector<int> v1{ 3,2,5,4,1,6,9,8 };
	for_each(v1.begin(), v1.end(), printInt);
	cout << endl;

	//将v1中最小的 3 个元素移动到开头位置并排好序
	partial_sort(v1.begin(), v1.begin() + 3, v1.end());
	for_each(v1.begin(), v1.end(), printInt);
	return 0;
}
4. partial_sort_copy(first, last, result_first, result_last)
    从指定范围内选出(result_last-result_first)个元素排序后拷贝到另一个容器。

    代码示例:

void printInt(int val)
{
	cout << val << "  ";
}
int main() {
	int target[4] = { 0 };
	std::vector<int> v1{ 3,2,5,4,1,6,9,8 };

	//将v1中前面5个元素排序,然后拷贝3个元素到target
	partial_sort_copy(v1.begin(), v1.begin() + 5, target, target + 3);
	for_each(target, target + 4, printInt);
	return 0;
}
5. nth_element (first, nth, last)

    找到[first, last)范围内按照排序规则(默认升序)位于第nth个位置处的元素,并将其放置到此位

     置。同时使所有比此元素小的元素在左侧,比它大的元素在右侧。

void printInt(int val)
{
	cout << val << "  ";
}
int main() {
	std::vector<int> v1{ 8,1,3,4,5,6,0,2,7,9 };
	//默认升序排序
	nth_element(v1.begin(), v1.begin() + 2, v1.end());
	cout << "nth_element排序" << endl;
	for_each(v1.begin(), v1.end(), printInt);
	return 0;
}
6. partition (first, last, pred)

     根据用户自定义的筛选规则,重新排列指定区域内存储的数据,使其分为 2 组,第一组为符合

     筛选条件的数据,另一组为不符合筛选条件的数据。返回第二组的第一个元素。

     代码示例:

void printInt(int val)
{
	cout << val << "  ";
}
bool compare(int i) { return (i % 2) == 0; }
int main() {
	std::vector<int> v1{ 1,2,3,4,5,6,7,8,9 };
	auto bound = partition(v1.begin(), v1.end(), compare);//按奇偶分组
	cout << "bound = " << *bound<<endl;
	for_each(v1.begin(), v1.end(), printInt);
	
	return 0;
}
7. stable_partition (first, last, pred)

    保证对指定区域内数据完成分组的同时,不改变各组内元素的相对位置。用法和partition一样。

8. is_sorted (first, last, comp)

    此函数专门用于判断某个序列是否为有序序列。

    代码示例:

bool compare(int i, int j) { return i > j; }
int main() {
	std::vector<int> v1{ 9, 8, 7, 6, 2 };

	cout << "v1 is sorted? " << is_sorted(v1.begin(), v1.end(), compare) << endl;

	return 0;
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值