C++ 向量对的排序向量

C++ 向量对的排序向量,c++,C++,我有以下格式的数据: vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > A; 5,((0,9),(1,2)) 6, ((0),(8,9)) 10,((1,10,15,16),(1,2)) 2,((0,2,10),(8,9)) 3,((0,1,2),(1,2)) 1,((3,4,7),(1,2)) 7,((3,4,6),(8,9)) 11,((1,51

我有以下格式的数据:

vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > A;

5,((0,9),(1,2))
6, ((0),(8,9))
10,((1,10,15,16),(1,2))
2,((0,2,10),(8,9))
3,((0,1,2),(1,2))
1,((3,4,7),(1,2))
7,((3,4,6),(8,9))
11,((1,51,9,3,2,4,6),(8,9))
也就是说,我想按第二对向量A排序,这样我就可以按第二对向量A的第二对向量把元素组合在一起。例如,这里我把包含(1,2)和(8,9)的所有元素组合在一起。然后我想根据以下(开始,结束)范围对第二对向量A的第一对向量进行排序。也就是说,向量介于(0,0)之间的元素出现在向量介于(0,1)之间的元素之前。例如,在示例中(0,1,2)出现在(0,9)之前,因为其元素位于(0,2)之间,而(0,2)以所述顺序出现在(0,9)之前……类似地,对于其他元素:

(Start,End)
(0,0),
(0,1),
(1,1),
(0,2),
(1,2),
(2,2),
(0,3),
(1,3),
(2,3),
(3,3),
(0,4),
(1,4),
(2,4),
(3,4),
(4,4),
(0,5),
(1,5),
(2,5),
(3,5),
(4,5),
(5,5),
(0,6),
(1,6),
(2,6),
(3,6),
(4,6),
(5,6),
(6,6),
(0,7),
(1,7),
(2,7),
(3,7),
(4,7),
(5,7),
(6,7),
(7,7),
(0,8),
(1,8),
(2,8),
(3,8),
(4,8),
(5,8),
(6,8),
(7,8),
(8,8),
.
.
(n,n)

我用C++(Ubuntu/LIARARO 4.63-1Ubuntu5)4.63./<


我试图通过以下方式解决这个问题:首先,我将元素(1,2)和(8,9)组合在一起。然后我将这些范围存储在另一个数据结构中。然后我在这些范围内进行迭代,尝试查看一行是否在所述范围内

您可能有一个特别复杂的数据结构,其中包含您想要在其上定义的超特定和任意顺序,但无所谓。它的好处在于它能完全适应:

std::sort(A.begin(), A.end(), MyComparator);
你所需要写的就是:

typedef pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > Elem;

bool MyComparator(const Elem& a, const Elem& b) {
    // return true if a should go before b in the sort

    // step 1 is apparently to do a vector comparison
    for (size_t i = 0; i < a.second.second.size(); ++i) {
        if (i == b.second.second.size()) {
            return false;           
        }
        else if (a[i] < b[i]) {
            return true;
        }
        else if (a[i] > b[i]) {
            return false;
        }
    }
    if (a.second.second.size() < b.second.second.size()) {
        return true;
    }

    // if we got here, the two vectors compare equal
    // so onto the next steps 2, 3, ...
    // etc.
}
typedef对元素;
布尔MyCompariator(常量元素M&a、常量元素M&b){
//如果排序中a应在b之前,则返回true
//第一步显然是做一个向量比较
对于(size_t i=0;ib[i]){
返回false;
}
}
if(a.second.second.size()
事实上,这样的问题是反复出现的:迟钝的数据结构总是导致混乱。你有没有试着写一个程序来解决这个问题?@Matt是的,我有。这是一种蛮力方法,在另一个数据结构中,我首先将元素(1,2)和(8,9)组合在一起。然后我将这些范围存储在另一个数据结构中。然后我在这些范围内进行迭代,并尝试查看一行是否在所述范围内。@Bathsheba这是一个数据结构问题。你能推荐一些我可以使用的其他数据结构吗。我真的很感谢你能用向量和std::sort感谢你的回答。但我不知道为什么它给了我错误的输出:@StegVerner的“etc”。我的意思是“在这里填写您自己的附加逻辑”。就目前而言,如果向量相等,函数将表现出未定义的行为…哦,对不起,但我仍然无法理解。我在这里没有任何附加逻辑。@StegVerner“那么我想根据以下(开始、结束)范围对第二对向量A的第一对向量进行排序。”?@StegVerner注意,这不会按照您的要求进行排序,因为
if(A.second.second.size()
将指示(0、1、2)的范围大于(0,4)的范围。
typedef pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > Elem;

bool MyComparator(const Elem& a, const Elem& b) {
    // return true if a should go before b in the sort

    // step 1 is apparently to do a vector comparison
    for (size_t i = 0; i < a.second.second.size(); ++i) {
        if (i == b.second.second.size()) {
            return false;           
        }
        else if (a[i] < b[i]) {
            return true;
        }
        else if (a[i] > b[i]) {
            return false;
        }
    }
    if (a.second.second.size() < b.second.second.size()) {
        return true;
    }

    // if we got here, the two vectors compare equal
    // so onto the next steps 2, 3, ...
    // etc.
}