Reputation: 1692
I am fairly new to STL. So pardon me if the question is naive.
I have a pair like this that is used as a key to a map.
typedef pair <int, int> KeyPair;
My map is as shown below
typedef map <KeyPair, uint32> NvInfoMap;
Now I want to introduce a new integer into the Key part of the map.
Which is the easiest way to do this?
Do I have to make another pair that will take the existing pair as its latter part?
Please note that I am on a restricted environment where boost library is not available.
Thanks for your time.
Upvotes: 2
Views: 2098
Reputation: 254751
If your restrictions allow C++11, then
typedef std::tuple<int, int, int> KeyTriple;
Otherwise, you could define your own type
struct KeyTriple {
int a;
int b;
int c;
};
with an ordering to allow it to be used as a key
bool operator<(KeyTriple const & lhs, KeyTriple const & rhs) {
if (lhs.a < rhs.a) return true;
if (rhs.a < lhs.a) return false;
if (lhs.b < rhs.b) return true;
if (rhs.b < lhs.b) return false;
if (lhs.c < rhs.c) return true;
return false;
// Alternatively, if you can use C++11 but don't want a tuple for a key
return std::tie(lhs.a, lhs.b, lhs.c) < std::tie(rhs.a, rhs.b, rhs.c);
}
or, as you suggest, you could use a nested pair
typedef std::pair<int, std::pair<int, int>>;
with the advantage that it defines the necessary comparison operator for you, but the disadvantage that creating one and accessing its elements is a bit of a faff.
Upvotes: 6