std中稳定排序算法_实战c++中的vector系列--使用sort算法对vector进行排序(对vector排序、使用稳定的排序std::stable_sort())...

本文介绍了C++中对vector排序的方法,包括使用std::sort的不稳定排序和std::stable_sort的稳定排序。通过示例代码展示了如何使用这两种排序,并解释了它们在处理等价元素相对顺序上的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了挺多关于vector的操作了,正好工作中遇到对vector进行排序的问题,这里就讨论一下。

直接使用sort算法,那就先了解一下:

template

void sort (RandomAccessIterator first, RandomAccessIterator last);

template

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Sorts the elements in the range [first,last) into ascending order.

The elements are compared using operator< for the first version, and comp for the second.

Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).

也就是所说的不稳定排序。

直接上代码:

#include // std::cout

#include // std::sort

#include // std::vector

#include

bool myfunction(int i, int j) { return (i

struct myclass {

bool operator() (int i, int j) { return (i

} myobject;

int main() {

int myints[] = { 32,71,12,45,26,80,53,33 };

std::vector myvector(myints, myints + 8);// 32 71 12 45 26 80 53 33

// using default comparison (operator

std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71)26 80 53 33

// using function as comp

std::sort(myvector.begin() + 4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

// using object as comp

std::sort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)

// print out content:

std::cout << "myvector contains:";

for (std::vector::iterator it = myvector.begin(); it != myvector.end(); ++it)

std::cout << ' ' << *it;

std::cout << '\n';

// Sorting the string vector

std::vector<:string> stringVec = { "John", "Bob", "Joe", "Zack", "Randy" };

sort(stringVec.begin(), stringVec.end());

for (std::string &s : stringVec)

std::cout << s << " ";

return 0;

}

//输出:

myvector contains: 12 26 32 33 45 53 71 80

Bob Joe John Randy Zack

这个时候可以使用std::stable_sort()来实现稳定的排序了:

Sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort preserves the relative order of the elements with equivalent values.

#include // std::cout

#include // std::stable_sort

#include // std::vector

bool compare_as_ints(double i, double j)

{

return (int(i)

}

int main() {

double mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 };

std::vector myvector;

myvector.assign(mydoubles, mydoubles + 8);

std::cout << "using default comparison:";

std::stable_sort(myvector.begin(), myvector.end());

for (std::vector::iterator it = myvector.begin(); it != myvector.end(); ++it)

std::cout << ' ' << *it;

std::cout << '\n';

myvector.assign(mydoubles, mydoubles + 8);

std::cout << "using 'compare_as_ints' :";

std::stable_sort(myvector.begin(), myvector.end(), compare_as_ints);

for (std::vector::iterator it = myvector.begin(); it != myvector.end(); ++it)

std::cout << ' ' << *it;

std::cout << '\n';

return 0;

}

//输出:

using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67

using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值