manvscode's blog

By manvscode, history, 7 years ago, In English

How to define triplet vectors. I tried {pair<int,pair<int,int>>} but push_back,sort,begin, end are not working with them. Please help.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +13 Vote: I do not like it

For vector<pair<int, pair<int, int>>> all those operations should work.

vector<tuple<int, int, int>> is an alternative.

For further help you need to post your code.

»
7 years ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

May be create custom structure with comparator? pair<int,pair<int,int>> is ugly.

struct my {
    int x, y, z;
    my () {}
    my (int x, int y, int z) : x(x), y(y), z(z) {}

    bool operator < (const my &other) const {
        if (x != other.x) return x < other.x;
        if (y != other.y) return y < other.y;
        return z < other.z;
    }
    // also for ==, > if needed.
};

std::vector<my> a;
a.push_back(my(1,2,3));
a.emplace_back(4, 5, 6); // i. e. a.push_back(my(4, 5, 6)), C++11
sort(a.begin(), a.end());