The proposal to split it into two maps is, of course, a little easier, but if you want more flexibility and can use C ++ 11 for functions like std::tuple , you can try something like a form:
#include <iostream> #include <map> #include <tuple> template <typename T1, typename T2, typename T3> class foobar { public: void add(T1 t1, T2 t2, T3 t3) { m1[t1] = std::make_tuple(t1, t2, t3); m2[t2] = std::make_tuple(t1, t2, t3); } std::tuple<T1,T2,T3> getA(T1 t1) { return m1[t1]; } std::tuple<T1,T2,T3> getB(T2 t2) { return m2[t2]; } private: std::map<T1,std::tuple<T1,T2,T3>> m1; std::map<T2,std::tuple<T1,T2,T3>> m2; }; int main() { foobar<int, int, bool> array; // or something along those lines array.add(1, 101, true); array.add(2, 102, false); auto res1 = array.getA(1); // returns first object auto res2 = array.getA(2); // returns second object auto res3 = array.getB(102); // returns second object again std::cout << std::get<0>(res1) << std::endl; std::cout << std::get<1>(res2) << std::endl; std::cout << std::get<2>(res3) << std::endl; return 0; }
A working example gives output 1, 102, 0 (false).
user2530166
source share