基于一行排序3d矢量(c ++,std :: sort)

时间:2014-11-14 20:37:20

标签: c++ stdvector

我想对3d矢量进行排序,例如

3 2 1 4 5
1 2 3 4 5
5 4 3 2 1

一行。基于第一行排序的结果应为:

1 2 3 4 5
3 2 1 4 5
3 4 5 2 1

我认为通过在std :: sort中使用正确的比较函数非常容易,但是如何?

谢谢!

2 个答案:

答案 0 :(得分:2)

std::vector<std::vector<int>> myvect = 
     { { 3, 2, 1 } 
     , { 1, 2, 3 }
     , { 5, 4, 3 }
     , { 0, 1, 3 }
     , { 7, 2, 5 }
     };

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

答案 1 :(得分:0)

首先

3 2 1 4 5
1 2 3 4 5
5 4 3 2 1

不构成一个3d,而是一个应该像这样定义的二维矢量

std::vector<std::vector<int>> myvect = 
     { { 3, 2, 1, 4, 5 } 
     , { 1, 2, 3, 4, 5 }
     , { 5, 4, 3, 2, 1 }
     };

没有为任意std::vector<>提供的内在 compare 函数,您必须实现一个,并将其传递给std::sort()函数。

相关问题