我有一系列类似的结构,例如,
map<key1, attr1>; map<key2, attr2>; ...,
同时,对应一系列数组,
// JUST TO SHOW THE IDEA, NOT SYNTAX CORRECT
class {key_1; attr_1;} array [#]; class {key_2; attr_2;} array [#]; ...,
并且 keyX 和 key_X 具有相似的结构以及 attr,例如
struct key1 {int k1;}; class key_1 {int k_1;};
struct key2 {int k1; int k2;}; class key_2 {int k_1; int k_2;};
...
struct attr1 {int a1; int a2;}; class attr_1 {int a_1; int a_2;};
我需要编写一个函数,例如重载赋值运算符,将数据从map_series转换为另一个,反之亦然。
因此,除了逐个映射、逐个键和逐个整数,是否有一些基于模板的方法可以这样做,这样代码就通用了,还是节省了更多行代码?做到这一点的明智方法是什么?
编辑1:
不幸的是,由于我们的遗留系统,结构之间的类型转换存在一个限制,例如key1、key_1 不能作为 C++ 原语使用,因此还必须提供转换函数。
编辑2:
灵感来自 J.N.回答,是否有可能有类似的东西:
template <class KeyMap, class ValueMap, class KeyArr, class ValueArr> void convert (map<KeyMap, ValueMap>, class {KeyArr, ValueArr} array[]){};
如何通用化key和attr的转换?就像
template <class KeyMap, class KeyArr> void convert_key(KeyMap, KeyArr){}
最佳答案
好吧,假设我们有这个:
map<KeyTypeM, ValueTypeM> m1;
struct Type1 {
KeyTypeS Key;
ValueTypeS Value;
};
我们先定义转换函数:
Type1 ConvType1(const KeyTypeM& key, const ValueTypeM& value)
{
Type1 result;
result.Key = f(key); // user dependent
result.Value = f(value); // user dependent
return result;
}
然后,在 C++ 中,您无法返回数组。实际上,您通常不使用数组,人们通常更喜欢 std::vector
。 (如果您确实需要一个数组,那么您需要分配它并返回指向它的指针或将其包装在智能指针中)
然后浏览 map 中的值并将它们推送到 vector 中就足够了:
vector<Type1> ConvertType1(map<KeyTypeM, ValueTypeM>& input)
{
vector<Type1> result;
for (auto& pair : input) // assumes C++11
result.push_back( ConvType1(pair.first, pair.second) );
return result;
}
注意:如果您没有 C++11 编译器,请使用:
for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it)
result.push_back( ConvType1(it->first, it->second) );
这将完成一张 map 和一种类型的工作。现在我们可以使用模板来概括转换函数:
template <class OutType, class KeyType, class ValueType, class Converter>
vector<OutType> Convert(map<KeyType, ValueType>& input, Converter conv)
{
vector<OutType> result;
for (auto& pair : input)
result.push_back( conv(pair.first, pair.second) );
return result;
}
并像这样使用它:
auto v1 = Convert<Type1>(m1, &ConvertType1);
auto v2 = Convert<Type2>(m2, &ConvertType2);
...
您可以进一步创建要使用 boost::mpl
进行转换的类型列表,但这应该是另一个问题。
编辑:概括类型的转换
正如您所提到的,没有通用的方法来转换类型,我们必须编写所有转换函数。所以我们唯一能做的就是通过实现隐式转换运算符来使其更加优雅,如下所示:
struct KeyTypeM
{
... // normal members
operator KeyTypeS() const
{
// do the conversion here
}
};
// suppose we have the same for ValueTypeM and ValueTypeS
// We can now use a single convert function:
template <class OutType, class KeyTypeM, class ValueType M>
OutType ConvertToStruct(const KeyTypeM& key, const ValueTypeM& value)
{
OutType result;
result.Key = key; // will call the implicit conversion
result.Value = value;
return result;
}
这简化了我们的转换函数(更新以考虑数组)
template <class OutType, class KeyType, class ValueType>
void Convert(map<KeyType, ValueType>& input, OutType out[])
{
// out must have been initialized to a proper size to hold all the elements.
unsigned cursor = 0;
for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it, ++cursor)
out[cursor] = ConvertToStruct<OutType>(it->first, it->second);
}
编辑2:我们可以使用std::pair
来概括数组结构:
template <class OutType, class KeyTypeM, class ValueType M>
OutType ConvertToStruct(const KeyTypeM& key, const ValueTypeM& value)
{
OutType result;
result.first = key; // will call the implicit conversion
result.second = value;
return result;
}
template <class OutKey, class OutValue, class KeyType, class ValueType>
void Convert(map<KeyType, ValueType>& input, std::pair<OutKey, OutValue> out[])
{
// out must have been initialized to a proper size to hold all the elements.
unsigned cursor = 0;
for (map<KeyTypeM, ValueTypeM>::iterator it = m.begin(); it != m.end(); ++it, ++cursor)
out[cursor] = ConvertToStruct<OutType>(it->first, it->second);
}
// Use this way:
std::pair<OutKey, OutValue> arr[m.size()];
Convert(m, arr);
关于c++ - 在 C++ 中复制类似数据结构的任何模板方式、通用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530923/