简体   繁体   中英

sorting vector of pair of vectors

I have data in the following form:

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))

Such that the sorted output appears as:

3,((0,1,2),(1,2))
5,((0,9),(1,2))
1,((3,4,7),(1,2))
10,((1,10,15,16),(1,2))
6, ((0),(8,9))
7,((3,4,6),(8,9))
2,((0,2,10),(8,9))
11,((1,51,9,3,2,4,6),(8,9))

That is I want to sort by the second pair of the vector A. Such that I group together the elements by the second pair of vector of second pair of vector AEg, here I group together all elements containing (1,2) and (8,9). Then I want to sort the first pair of vector of second pair of vector A according to the following (start,end) ranges. That is, the elements whose vectors lie between (0,0) appear before elements whose vectors lie between (0,1). Eg in the example (0,1,2) appears before (0,9) as its elements lie between (0,2) which appears before (0,9) in the said order...similarly for the other elements:

(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)

I am running C++ using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.

I tried to solve this problem by: first I group together the elements like (1,2) and (8,9). Then I store these ranges in another data structure. Then I iterate over these ranges and try to see if a row falls in the said range or not

You may have a particularly complicated data structure with a super specific and arbitrarily ordering that you want to define on it, but no matter. The nice thing about std::sort is that it's completely accommodating:

std::sort(A.begin(), A.end(), MyComparator);

All you need to write is:

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM