我有一个STL向量的列表,我想按照每个向量的第一个元素对它们进行排序

I have a list of STL vectors and I want to sort them by the first element of each vector

本文关键字:向量 元素 排序 第一个 列表 STL 有一个      更新时间:2023-10-16

我有一个来自STL的向量列表。它们有不同的长度,我想根据每个向量的第一个元素对它们进行排序。例如,我有向量:445 6 10、18、223和317 9。第一个数字是每个向量的大小,后面跟着向量的元素。我想在文件中显示矢量,按第一个元素排序。在本例中,我的矢量应按以下顺序显示:317 9、223、445 6 10和18

这是我的代码:

ofstream fout ("retele.out");
fout << T << 'n';
for (i=1; i<=T; i++)
{
    fout << sol[i].size() << ' ';
    sort(sol[i].begin(),sol[i].end());
    for (j=0; j<sol[i].size(); j++)
        fout << sol[i][j] << ' ';
    fout << 'n';
}
fout.close();

"T"是矢量的数量。"排序"函数用于对每个向量进行排序,我还需要另一个排序来对向量进行排序。

其他答案的一个小说明:std::vector<T>已经有了与您描述的行为完全相同的operator<(cplusplus描述(。

template < class T, class Alloc >
bool operator < (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);

描述:

小于比较(运算符<以对等的方式(即检查a<b和b<a(并且在第一次出现时停止。

所以,你可以写

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

UP:注释中写道,您有一个std::vector数组,而不是std::list<std::vector>数组。该数组没有方法begin()end(),因此这个确切的代码将不起作用。解决方案是

sort(sol, sol + SIZE);

其中SIZE是要排序的数组部分的大小。

基于此链接的答案:

你可以创建这样的比较方法:

bool compare_vec (const std::vector<int>& first, const std::vector<int>& second)
{
  return first[1]<second[1];
}

并呼叫:

sol.sort(compare_vec);

查看这篇文章。

bool myfunction (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
struct myclass {
  bool operator() (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
} myobject;

...
ofstream fout ("retele.out");
fout << T << 'n';
for (i=1; i<=T; i++)
{
    fout << sol[i].size() << ' ';
    sort(sol[i].begin(),sol[i].end(), myfunction);
    for (j=0; j<sol[i].size(); j++)
        fout << sol[i][j] << ' ';
    fout << 'n';
}
fout.close();

您必须实现自己的比较器,稍后将使用它对vectors列表进行排序。

您可以遵循以下算法:

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
 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};
    std::vector<int> 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<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
      std::cout << ' ' << *it;
     std::cout << 'n';
    return 0;
  }
std::sort(MyVector.begin(), MyVector.end(), [](auto a, auto b) { return a.front() < b.front(); } );

我希望这能对你的向量进行排序

STL函数std::sort包含一个可选谓词。在这种情况下,我想你会想写你的排序:
std::sort(sol.begin(), sol.end(), [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });

请注意,如果列表中包含空向量,则此代码将崩溃。

如果sol不是std::list,尽管注释中指出了类似于c样式的数组std::vector<int> v[50001]。应该重写为:

std::sort(sol[0], sol[nofElementsFilledIn], [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });