转换成二维数组到std ::地图? [英] Convert 2D array to std::map?
问题描述
数组可以转换成的std ::矢量
轻松有效地:
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));
}
有没有类似的方式二维数组转换成的std ::地图
没有遍历成员?这看起来像一个不寻常的函数签名,但在我的特定的情况,在这些地图的键和值将是相同的类型。
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]) {
// ...?
}
下面是测试code我放在一起了这个问题。它将编译和运行原样;我们的目标是得到它与块注释编译主
注释。
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;
*/
}
同样,我不是在寻找与code的答案是在阵列中的每个成员进行迭代。我可以写我自己。如果不能以更好的方式来完成,我会接受,作为一个答案。
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.
推荐答案
以下工作正常,我:
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;
}
如果你有C ++ 03你可以使用
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;
}
完整演示 http://liveworkspace.org/$c$c/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;
}
这篇关于转换成二维数组到std ::地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!