【问题标题】:Sort vector of vectors对向量的排序向量
【发布时间】:2013-01-03 09:06:00
【问题描述】:

我有

    vector<vector<int>> vec 

在我的 C++ 应用程序中。

每个整数向量作为“大”向量的一个元素都有 4 个 INT 值。 我想根据它的 ints 内容向量的第三个值对 vec 进行排序(我的意思是每个“内部”向量第三个元素) - 有可能吗?

编辑

假设我有一个函数

COST(vector<int>)

它根据我的向量值计算出一些值 - 我也可以在比较参数中使用它吗?它会帮助我更多。

【问题讨论】:

  • 只需定义您自己的使用第三个元素的值的比较运算符。详情见这里:stackoverflow.com/questions/1380463/…
  • 请记住,lambda 也是一种选择。
  • 嗯,你内心所有的vectors都包含4个ints,而且每个int都有特殊含义?听起来您更愿意将某个类的对象放在该向量中?
  • 在你的比较器中返回cost(a) &lt; cost(b)

标签: c++ stl vector


【解决方案1】:

确实如此。 std::sort 可以带第三个参数,这是排序时使用的比较函数。例如,您可以使用 lambda 函数:

std::vector<std::vector<int>> vec;
// Fill it

std::sort(vec.begin(), vec.end(),
          [](const std::vector<int>& a, const std::vector<int>& b) {
  return a[2] < b[2];
});

或者,您可以传递任何其他可调用签名bool(const std::vector&lt;int&gt;&amp;, const std::vector&lt;int&gt;&amp;),例如函子或函数指针。


编辑回复:只需将您的COST 函数应用于ab

std::sort(vec.begin(), vec.end(),
          [](const std::vector<int>& a, const std::vector<int>& b) {
  return COST(a) < COST(b);
});

【讨论】:

  • 请注意,第三个参数是严格的弱排序,如果第一个参数应该放在第二个参数之前,则返回 true。这转化为小于(即 a
  • 当向量向量中只有一个元素时出现错误,因为b 将为空并且b[2] 将抛出reference binding to null pointer of type 'int' 任何补救措施,有人吗??
【解决方案2】:

如果你想按成本比较两个向量,试试这个:

bool predicate(const std::vector<int>& a, const std::vector<int>& b)
{
    return COST(a) < COST(b);
}

注意事项:

  • 以上内容也适用于 C++98,我不确定 C++11 的使用范围有多广,以及您是否有兼容的编译器。否则,您当然也可以按照 sftrabbit 的建议使用 lambda 表达式。
  • 您没有说 COST 返回什么,我只是假设一些可排序的值,例如 float 或 long。
  • 我希望您在将向量传递给 COST() 时不要复制向量,那样效率会非常低。
  • COST 建议一个宏,就像所有的大写字母一样。不要使用宏。不要为函数使用宏名称。

【讨论】:

    【解决方案3】:

    sort(vec.begin(), vec.end(), comp);

    其中comp 是:

    static bool comp(const vector<int>& vec1, const vector<int>& vec2){
        return vec1[2] < vec2[2];
    }
    

    【讨论】:

      【解决方案4】:
      #include <vector>
      #include <algorithm>
      #include <cstdlib>
      #include <ctime>
      
      using namespace std;
      
      // This makes the sort be according to column 2 and ascending
      bool sortFunc( const vector<int>& p1,
                 const vector<int>& p2 ) {
       return p1[1] < p2[1];
       }
      
      int main() {
      
        srand(time(NULL));
      
        // Creates and initializes 10 x 4 vector
        vector< vector<int> > vec;
        for( int i=0; i<10; i++ ) {
         vector<int> tmpVec;
         for( int j=0; j<2; j++ ) {
        tmpVec.push_back( rand()%10 );
         }
         vec.push_back( tmpVec );
        }
      
        // Print out the pre-sorted vector
       cout << "Pre-sorting state:" << endl;
        for( int i=0; i<vec.size(); i++ ) {
         for( int j=0; j<vec[i].size(); j++ ) {
        cout << vec[i][j] << " ";
        }
      cout << endl;
      }
        cout << endl;
      
        // Do the sorting according to column 2
        sort(vec.begin(), vec.end(), sortFunc);
      
        // Print out the post-sorted vector
         cout << "Post-sorting state:" << endl;
         for( int i=0; i<vec.size(); i++ ) {
          for( int j=0; j<vec[i].size(); j++ ) {
        cout << vec[i][j] << " ";
          }
         cout << endl;
         }
      
        return 0;
        }
      

      来源:https://shihho.wordpress.com/2012/11/28/sort_with_vectors/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多