提问者:小点点

如何在C++中对向量数组进行排序?


我创建了一个向量数组。 在接受输入后,我必须对它们进行排序,我想我需要使用一个比较器函数,但我不明白如何使用它。

int main()
{
    
    int  n, k;
    cin >> n >> k;
    vector<long long> times[4];
  
    for (int i = 0; i < n; ++i)
    {
        ll t, a, b;
        cin >> t >> a >> b;
        times[a * 2 + b].push_back(t);
    }

    cout << times[0].size();
    ***//i need to sort the whole of times[0] after this***
}

共1个答案

匿名用户

这取决于你需要分类什么。

如果要对内部数组进行排序,请使用sort this way。 它按[0]次排序。 第二个版本带有自定义比较器

std::sort(times[0].begin(), times[0].end());
std::sort(times[0].begin(), times[0].end(), [](ll a, ll b){ return a < b; });

如果您需要对“Times”数组进行排序,那么使用自己的比较器,您可以这样编写它:

std::sort(std::begin(times), std::end(times), 
    [](const std::vector<ll>& a, const std::vector<ll>& b)
    {
        return a.size() < b.size();
    }
);

根据注释编辑:

如果词典的排序顺序已经足够,那么您可以跳过自定义比较器:

std::sort(std::begin(times), std::end(times));

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|c++|中|向量|数组|排序)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?