Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 二维向量c+中的两列以上排序+;_C++_Sorting_Std_Multiple Columns_2d Vector - Fatal编程技术网

C++ 二维向量c+中的两列以上排序+;

C++ 二维向量c+中的两列以上排序+;,c++,sorting,std,multiple-columns,2d-vector,C++,Sorting,Std,Multiple Columns,2d Vector,我已经为二维向量上的排序编写了代码,该排序依赖于两列。例如,如果这是2D列的数据 香蕉自行车2 |苹果车1 |橙色自行车5 |香蕉车2 |苹果 自行车3 然后我的排序将更改此数据,如下所示: 苹果车3 |苹果车1 |香蕉车2 |香蕉车2 |橙色 周期5 我在下面给出了我的编码 class StringListCompare { public: explicit StringListCompare(int column, int column2) : m_column(column), m_c

我已经为二维向量上的排序编写了代码,该排序依赖于两列。例如,如果这是2D列的数据

香蕉自行车2 |苹果车1 |橙色自行车5 |香蕉车2 |苹果 自行车3

然后我的排序将更改此数据,如下所示:

苹果车3 |苹果车1 |香蕉车2 |香蕉车2 |橙色 周期5

我在下面给出了我的编码

class StringListCompare
{
public:
  explicit StringListCompare(int column, int column2) : m_column(column), m_column2(column2) {}

 bool operator()(const vector<string>& lhs, const vector<string>& rhs)
  {
        if (lhs[m_column] == rhs[m_column])
        {
            return lhs[m_column2] < rhs[m_column2];
        }   
        else
        {   
            return lhs[m_column] > rhs[m_column];
        }
  }
private:
  int m_column;
  int m_column2;
};

在我的第二个节目中

这里有一些伪代码可能会对您有所帮助:

bool compare(lhs, rhs) {
    //compare lhs and rhs, which you know is different at this point
}

bool operator()(lhs, rhs) {
for i := 0 to noCol
    if lhs[i] != rhs[i]
        return compare(lhs, rhs)

//We know now that lhs and rhs are equal
return true;
}
if (lhs[m_column] == rhs[m_column])
bool compare(lhs, rhs) {
    //compare lhs and rhs, which you know is different at this point
}

bool operator()(lhs, rhs) {
for i := 0 to noCol
    if lhs[i] != rhs[i]
        return compare(lhs, rhs)

//We know now that lhs and rhs are equal
return true;
}