Convert 2D array to std::map?

匿名 (未验证) 提交于 2019-12-03 08:50:26

问题:

An array can be converted into a std::vector easily and efficiently:

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 into a std::map without iterating over the members? This looks like an unusual function signature, but in my particular situation, the keys and values in these maps will be of the same type.

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

Here's the test code I put together for this question. It will compile and run as-is; the goal is to get it to compile with the block comment in main uncommented.

#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 iterates over each member of the array... I could write that myself. If it can't be done in a better way, I'll accept that as an answer.

回答1:

The following works fine 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 could 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; }


转载请标明出处:Convert 2D array to std::map?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!