【问题标题】:I have a list of STL vectors and I want to sort them by the first element of each vector我有一个 STL 向量列表,我想按每个向量的第一个元素对它们进行排序
【发布时间】:2016-12-19 10:25:07
【问题描述】:

我有一个来自 STL 的向量列表。它们有不同的长度,我想按每个向量的第一个元素对它们进行排序。例如,我有向量:4 4 5 6 10, 1 8, 2 2 3 and 3 1 7 9. 第一个数字是每个向量的大小,然后是向量的元素。我想在文件中显示向量,按第一个元素排序。在此示例中,我的向量应按以下顺序显示:3 1 7 9, 2 2 3, 4 4 5 6 10 和 1 8。

这是我的代码:

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”是向量的数量。 “排序”函数用于对每个向量进行排序,我还需要另一个排序来对刚才描述的向量进行排序。

【问题讨论】:

  • 可以在这个例子中添加变量sol的声明吗?
  • 可能是sort(sol.begin(), sol.end())?
  • 这里是声明: unsigned int i, j;向量 sol[50001];无符号整数 T;
  • vector &lt;unsigned int&gt; sol[50001] 你想创建一个包含 50001 个向量的 cstyle 数组吗?
  • 您是否将向量的大小存储为向量的第一个元素?如果是,为什么?

标签: c++ algorithm sorting vector stl


【解决方案1】:

对其他答案的小说明:std::vector&lt;T&gt; 已经有 operator&lt;,其行为与您描述的完全相同 (cplusplus description)。

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

说明:

小于比较 (operator

所以,你可以写

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

UP:在 cmets 中写到您有一个 std::vector 数组,而不是 std::list&lt;std::vector&gt;。该数组没有方法begin()end(),所以这个确切的代码不起作用。解决办法是

sort(sol, sol + SIZE);

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

【讨论】:

  • 问题是它不起作用。这是错误:“在 'sol' 中请求成员 'begin',它是非类类型 'std::vector [50001]'”。
  • 或者更好:sort(std::begin(sol), std::end(sol)).
【解决方案2】:

根据这个link回答:

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

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

并调用:

sol.sort(compare_vec);

【讨论】:

  • 发生错误:“请求'sol'中的成员'sort',它是非类类型'std::vector [50001]'”。
【解决方案3】:

查看this 的帖子。

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 列表进行排序。

【讨论】:

  • 这将是一个昂贵的比较/排序,我建议你接受 const-refs 到你的“myfunction”中的向量
【解决方案4】:

你可以按照这个算法:

// 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;
  }

【讨论】:

    【解决方案5】:
    std::sort(MyVector.begin(), MyVector.end(), [](auto a, auto b) { return a.front() < b.front(); } );
    

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

    【讨论】:

      【解决方案6】:

      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&lt;int&gt; v[50001] 如 cmets 中所示。应该将其重写为:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        • 2022-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多