0

多次元ベクトルをソートしようとしていますが、このコードを使用すると途中でいくつかの問題が発生します最後の次元 (vec[ ][ ][0] から vec[ ][ ][5]) に基づいてベクトル vec を注文する方法はありますか?

int main(){
    vector<vector<vector<int> > > vec (148995,vector<vector<int> >(7,vector <int>(6,0))); 
    order();
    order2();
}
bool comparison_function(const std::vector<std::vector<int> >& v1,
                     const std::vector<std::vector<int> >& v2) {

    return v1[5][0] > v2[5][0];
}

void order(){
     std::sort(vec.begin(), vec.end(),comparison_function);
}
bool comparison_function2(const std::vector<std::vector<int> >& v1,
                     const std::vector<std::vector<int> >& v2) {

    return v1[5][2] > v2[5][2];
}

void order2(){
     std::sort(vec.begin(), vec.end(),comparison_function2);
}

現在の動作方法は、order() を呼び出してから order2() を呼び出すと、order() のすべての順序が失われ、すべてが order2() によって順序付けられます。

最終的には、vec[ ][ ][x] の 3 番目のブラケットを使用して、ベクトルの次元を別々に注文できる 6 つの注文関数が必要です。

助けてくれてありがとう。

4

1 に答える 1

0

2 つの異なる比較関数を使用して同じベクトルを並べ替えることはできません。2 番目の並べ替えは、最初の並べ替えを忘れます。

std sort への呼び出しは 1 回だけ使用する必要がありますが、構成された比較関数 (辞書式順序の場合など) を使用する必要があります。アイデアを提供するための 2 次元ベクトルの例を次に示します。

bool comparison_function(const std::vector<std::vector<int> >& v1,
                 const std::vector<std::vector<int> >& v2) {

    if (v1[5][0] != v2[5][0]) return v1[5][0] > v2[5][0];
    else                      return v1[5][1] > v2[5][1];
}
于 2013-07-17T22:38:00.397 に答える