Convert 2D array to std :: map?

An array can be easily and efficiently converted to std::vector :

 template <typename T, int N> vector<T> array_to_vector(T(& a)[N]) { return vector<T>(a, a + sizeof(a) / sizeof(T)); } 

Is there a similar way to convert a two-dimensional array to std::map without repeating elements? It looks like an unusual function signature, but in my particular situation, the keys and values ​​on these cards will have the same type.

 template <typename T, int N> map<T, T> array_to_map(T(& a)[N][2]) { // ...? } 

Here is the test code I compiled for this question. It will compile and run as is; the goal is to get it compiled with a block comment in main without commenting.

 #include <iostream> #include <string> #include <vector> #include <map> using namespace std; template <typename T, int N> vector<T> array_to_vector(T(& a)[N]) { return vector<T>(a, a + sizeof(a) / sizeof(T)); } template <typename T, int N> map<T, T> array_to_map(T(& a)[N][2]) { // This doesn't work; members won't convert to pair return map<T, T>(a, a + sizeof(a) / sizeof(T)); } int main() { int a[] = { 12, 23, 34 }; vector<int> v = array_to_vector(a); cout << v[1] << endl; /* string b[][2] = { {"one", "check 1"}, {"two", "check 2"} }; map<string, string> m = array_to_map(b); cout << m["two"] << endl; */ } 

Again, I'm not looking for answers with code that repeats over each member of the array ... I could write this myself. If this cannot be done better, I will agree with that in return.

+4
source share
1 answer

The following works great for me:

 template <typename T, int N> map<T, T> array_to_map(T(& a)[N][2]) { map<T, T> result; std::transform( a, a+N, std::inserter(result, result.begin()), [] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); } ); return result; } 

If you have C ++ 03, you can use

 template <typename T> static std::pair<T, T> as_pair(T const(&p)[2]) { return std::make_pair(p[0], p[1]); } template <typename T, int N> map<T, T> array_to_map(T(& a)[N][2]) { map<T, T> result; std::transform(a, a+N, std::inserter(result, result.begin()), as_pair<T>); return result; } 

Full demo http://liveworkspace.org/code/c3419ee57fc7aea84fea7932f6a95481

 #include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> #include <iterator> using namespace std; template <typename T, int N> vector<T> array_to_vector(T const(& a)[N]) { return vector<T>(a, a + sizeof(a) / sizeof(T)); } template <typename T> static std::pair<T, T> as_pair(T const(&p)[2]) { return std::make_pair(p[0], p[1]); } template <typename T, int N> map<T, T> array_to_map(T const(& a)[N][2]) { map<T, T> result; // C++03: std::transform(a, a+N, std::inserter(result, result.begin()), as_pair<T>); std::transform( a, a+N, std::inserter(result, result.begin()), [] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); } ); return result; } int main() { int a[] = { 12, 23, 34 }; vector<int> v = array_to_vector(a); cout << v[1] << endl; const string b[][2] = { {"one", "check 1"}, {"two", "check 2"} }; map<string, string> m = array_to_map(b); cout << m["two"] << endl; } 
+5
source

Source: https://habr.com/ru/post/1438295/


All Articles