Does C++ have anything which behaves like std::map<T1, T2>, but permitting not only looking for a T1 value to find its associated T2 value, but also to look for a particular T2 value to find its associated T1 value? (Which would mean values on both sides need to be unique.)
The C++ standard library doesn't have one, but Boost library's boost::bimap is a data structure that behaves exactly the way you describe.
map of type boost::bimap<T1,T2>:
map.left provides an interface that closely matches std::map<T1,T2>, including all T1-based lookupmap.right provides an interface that closely matches std::map<T2,T1>, including all T2-based lookupconst. You can add and remove, but you can not modify in-place.You could do this with boost::bimap from the boost library. Eg.
boost::bimap<int, std::string> map;
map.insert({1, "one"});
map.insert({2, "two"});
std::cout << map.left.at(1) << "\n"; // "one"
std::cout << map.right.at("two") << "\n"; // 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With