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.