c++ 将2D数组转换为std::map?

11dmarpk  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(162)

数组可以简单有效地转换为std::vector

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

有没有类似的方法可以将二维数组转换为std::map,而无需迭代成员?这看起来像一个不寻常的函数签名,但在我的特定情况下,这些Map中的键和值将是相同的类型。

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

下面是我为这个问题编写的测试代码。它将按原样编译和运行;目标是让它在main中的块注解未注解的情况下编译。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. template <typename T, int N>
  7. vector<T> array_to_vector(T(& a)[N]) {
  8. return vector<T>(a, a + sizeof(a) / sizeof(T));
  9. }
  10. template <typename T, int N>
  11. map<T, T> array_to_map(T(& a)[N][2]) {
  12. // This doesn't work; members won't convert to pair
  13. return map<T, T>(a, a + sizeof(a) / sizeof(T));
  14. }
  15. int main() {
  16. int a[] = { 12, 23, 34 };
  17. vector<int> v = array_to_vector(a);
  18. cout << v[1] << endl;
  19. /*
  20. string b[][2] = {
  21. {"one", "check 1"},
  22. {"two", "check 2"}
  23. };
  24. map<string, string> m = array_to_map(b);
  25. cout << m["two"] << endl;
  26. */
  27. }

再次声明,我并不是用遍历数组中每个成员的代码来寻找答案……我可以自己写。如果不能用更好的方法来解决,我会接受这个答案。

1l5u6lss

1l5u6lss1#

下面的代码对我来说很好:

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

如果你有C++03,你可以使用

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

完整demo

Live On Coliru

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <algorithm>
  6. #include <iterator>
  7. using namespace std;
  8. template <typename T, int N>
  9. vector<T> array_to_vector(T const(& a)[N]) {
  10. return vector<T>(a, a + sizeof(a) / sizeof(T));
  11. }
  12. template <typename T>
  13. static std::pair<T, T> as_pair(T const(&p)[2])
  14. {
  15. return std::make_pair(p[0], p[1]);
  16. }
  17. template <typename T, int N>
  18. map<T, T> array_to_map(T const(& a)[N][2])
  19. {
  20. map<T, T> result;
  21. // C++03: std::transform(a, a+N, std::inserter(result, result.begin()), as_pair<T>);
  22. std::transform(
  23. a, a+N, std::inserter(result, result.begin()),
  24. [] (T const(&p)[2]) { return std::make_pair(p[0], p[1]); }
  25. );
  26. return result;
  27. }
  28. int main() {
  29. int a[] = { 12, 23, 34 };
  30. vector<int> v = array_to_vector(a);
  31. cout << v[1] << endl;
  32. const string b[][2] = {
  33. {"one", "check 1"},
  34. {"two", "check 2"}
  35. };
  36. map<string, string> m = array_to_map(b);
  37. cout << m["two"] << endl;
  38. }
展开查看全部

相关问题