首页 > 编程语言 >C++ 实现argosort

C++ 实现argosort

时间:2022-10-29 20:00:46浏览次数:50  
标签:实现 元素 back C++ int push argosort

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n = 5, t;
    vector<int> a, b;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &t);
        a.push_back(t);
        b.push_back(i);
    }
    sort(b.begin(), b.end(), [&a](int x, int y)
    {
        return a[x] < a[y];
    });
    cout<<"b:";
    for(auto x: b)
        cout<<x<<" ";
    cout<<endl<<"a:";
    for(auto x: a)
        cout<<x<<" ";
}

将b中元素按照a中元素大小排序,b数组中的值应该是0,1,2...n-1,这样在比较的时候可以直接将b中的元素作为a的下标来比较。

标签:实现,元素,back,C++,int,push,argosort
From: https://www.cnblogs.com/hetailang/p/16839514.html

相关文章