C++头文件algorithm 2——Sorting

本文详细介绍了C++标准库中的排序算法,包括sort、stable_sort、partial_sort等函数的功能及用法,并通过实例展示了如何使用这些函数进行升序排序。

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

函数功能
sort升序排序
stable_sort与sort相似,升序排序,但保留元素的相对位置
partial_sort升序排序部分元素
partial_sort_copy部分排序并复制已排序的序列
is_sorted检查是否升序排序
is_sorted_until找到第一个不满足升序排序的元素
nth_element仅排序第n个元素,在返回结果中只有第n位元素与最终有序序列的第n位一致

sort

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp)
对元素进行排序,默认为升序。first,last表示[first,last)。comp二元函数,为可选参数,设定新的排序规则。

example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

bool myfunction(int i, int j){ return(i < j); }
struct myclass{
    bool operator()(int i, int j){ return (i < j); }
}myobject;
int main()
{
    int myints[] = { 32, 71, 12, 45, 26, 80, 53, 33 };
    vector<int> myvector(myints, myints + 8);
    //using default comparison (operator <)  
    sort(myvector.begin(),myvector.begin()+4);  //对(32,71,12,45)进行部分升序排序
    cout << "default comparison:";
    for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        cout << *it << " ";
    cout << endl;
    //using function as comp
    sort(myvector.begin() + 4, myvector.end(), myfunction);  //对(26,80,53,33)进行部分升序排序
    cout << "function as comp:";
    for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        cout << *it << " ";
    cout << endl;
    //using object as comp
    sort(myvector.begin(), myvector.end(), myobject); //对全部元素进行升序排序
    cout << "object as comp:";
    for (vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        cout << *it << " ";
    cout << endl;
    //using object as comp
    return 0;
}

output
这里写图片描述

stable_sort

void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp )。stable_sort功能与sort类似,但保持元素之间的相对位置。

example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

bool compare_as_ints(double i, double j){
    return (int(i) < int(j));
}
int main()
{
    double mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 };
    vector<double> myvector;
    myvector.assign(mydoubles, mydoubles + 8);
    cout << "default comparison:";
    stable_sort(myvector.begin(), myvector.end());
    for (vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        cout << *it << " ";
    cout << endl;
    myvector.assign(mydoubles, mydoubles + 8);
    cout << "compare_as_ints:";
    stable_sort(myvector.begin(), myvector.end(), compare_as_ints);
    for (vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        cout << *it << " ";
    cout << endl;
    return 0;
}

output
这里写图片描述

partial_sort

void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp)
first表示其实位置
middle表示某个中间位置,排序结果中只有从first到middle是有序的
last表示最终位置
comp表示二元函数,自定义的排序规则,可选参数。

example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool myfunction(int i, int j){ return (i < j); }
int main()
{
    int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, };
    vector<int> myvector(myints, myints + 9);
    //using default comparison(operator<)
    partial_sort(myvector.begin(), myvector.begin() + 5, myvector.end());
    cout << "default comparison:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    //using function as comp
    cout << "function as comp:";
    partial_sort(myvector.begin(), myvector.begin() + 5, myvector.end());
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
}

output
这里写图片描述

partial_sort_copy

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

对[first,last)进行排序,并将结果复制保存到[result_first,result_last)。被复制元素的数目是[result_first,result_last)的长度,除非它的长度大于[first,last)中元素数目。comp表示二元函数,自定义的排序规则,可选参数。

example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool myfunction(int i, int j){ return (i < j); }
int main()
{
    int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, };
    vector<int> myvector(5);
    //using default comparison(operator<)
    partial_sort_copy(myints, myints + 9, myvector.begin(), myvector.end());
    cout << "myvector contains:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    return 0;
}

output
这里写图片描述

is_sorted

bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp)
first,last表示范围;
comp表示排序规则,二元函数,可选参数。

example

#include<iostream>
#include<algorithm>
#include<array>
using namespace std;
int main()
{
    array<int, 4> myarray = { 2, 3, 1, 4 };
    do{
        //尝试一个新的排列
        prev_permutation(myarray.begin(), myarray.end());
        cout << "myarray:";
        for (auto x : myarray)
            cout << x << " ";
        cout << endl;
    } while (!is_sorted(myarray.begin(), myarray.end()));

    cout << "The array is sorted!" << endl;
    return 0;
}

output
这里写图片描述

is_sorted_until

ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp)
函数返回在[first,last)中第一个不满足升序排序元素的迭代器。从first到返回的迭代器之间是有序的。如果所有元素是有序的,函数返回last。

example

#include<iostream>
#include<algorithm>
#include<array>
using namespace std;
int main()
{
    array<int, 4> myarray = { 2, 3, 1, 4 };
    array<int, 4>::iterator it;
    do
    {
        prev_permutation(myarray.begin(), myarray.end());
        cout << "myarray:";
        for (auto x : myarray)
            cout << x << " ";
        it = is_sorted_until(myarray.begin(), myarray.end());
        cout << "【" << (it - myarray.begin()) << " elements sorted】\n";
    } while (it != myarray.end());
    cout << "The array is sorted\n";
    return 0;
}

output
这里写图片描述

nth_element

void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp)
first表示起始位置
nth表示要排序的位置
last表示终止位置
comp自定义排序规则,二元函数,可选参数

example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool myfunction(int i, int j){ return(i < j); }
int main()
{
    vector<int> myvector;
    for (int i = 1; i < 10; ++i)
        myvector.push_back(i);

    random_shuffle(myvector.begin(), myvector.end());
    cout << "myvector contains:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    nth_element(myvector.begin(), myvector.begin() + 5, myvector.end());
    cout << "default comparison:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    random_shuffle(myvector.begin(), myvector.end());
    cout << "myvector contains:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    nth_element(myvector.begin(),myvector.begin()+5, myvector.end(), myfunction);
    cout << "function as comp:";
    for (auto x : myvector)
        cout << x << " ";
    cout << endl;
    return 0;
}

output
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值