c++ std::map class 带通用键
c++ std::map class with generic key
我有一个 类 家族,每个子类都需要一个映射,但键将具有不同的类型,尽管它们都将对映射执行完全相同的操作。此外,两种情况下的值都是字符串。
到目前为止,我的代码类似于下面的示例,我的目标是重用代码,通过
有一个通用密钥。
除了 STL
,不使用任何额外的库
class A{
public:
/*
* More code
*/
};
class subA1 : public A{
public:
void insertValue(long id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<long,std:string> _theMap;
};
class subA2 : public A{
public:
void insertValue(std::string& id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<std::string,std:string> _theMap;
};
写另一个小基数 class 怎么样,比如说 C<T>
,它是一个 template typename T
,只包含一个 map<T, string>
和你的 insert
函数。那么 A 的每个新子 class 也将是 C
的子class。所以你的 subA1
将是 public A, public C<long>
等等
只需将超类 A 设为模板,将 _theMap 和 insertValue() 移动到它,并在子类中使用正确的模板版本。
template <typename KeyT>
class A{
public:
void insertValue(KeyT id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<KeyT, std:string> _theMap;
};
class subA1 : public A<long> {};
class subA2 : public A<std::string> {};
您可以将 subA1
和 subA2
合并为一个模板 class,例如:
class A{
public:
/*
* More code
*/
};
template <typename KeyType>
class subA : public A {
public:
void insertValue(const KeyType &id, const std::string& value) {
if(_theMap.find(id) == _theMap.end()) {
_theMap.insert(std::make_pair(id, value));
}
}
private:
std::map<KeyType, std:string> _theMap;
};
然后您可以根据需要创建 typedef:
typedef subA<long> subA1;
typedef subA<std::string> subA2;
或者,如果您需要实际派生 classes:
class subA1 : public subA<long>
{
...
};
class subA2 : public subA<std::string>
{
...
};
我有一个 类 家族,每个子类都需要一个映射,但键将具有不同的类型,尽管它们都将对映射执行完全相同的操作。此外,两种情况下的值都是字符串。 到目前为止,我的代码类似于下面的示例,我的目标是重用代码,通过 有一个通用密钥。 除了 STL
,不使用任何额外的库class A{
public:
/*
* More code
*/
};
class subA1 : public A{
public:
void insertValue(long id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<long,std:string> _theMap;
};
class subA2 : public A{
public:
void insertValue(std::string& id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<std::string,std:string> _theMap;
};
写另一个小基数 class 怎么样,比如说 C<T>
,它是一个 template typename T
,只包含一个 map<T, string>
和你的 insert
函数。那么 A 的每个新子 class 也将是 C
的子class。所以你的 subA1
将是 public A, public C<long>
等等
只需将超类 A 设为模板,将 _theMap 和 insertValue() 移动到它,并在子类中使用正确的模板版本。
template <typename KeyT>
class A{
public:
void insertValue(KeyT id, std::string& value){
if(_theMap.find(id) == _theMap.end())
{
_theMap[id] = value;
}
}
private:
std::map<KeyT, std:string> _theMap;
};
class subA1 : public A<long> {};
class subA2 : public A<std::string> {};
您可以将 subA1
和 subA2
合并为一个模板 class,例如:
class A{
public:
/*
* More code
*/
};
template <typename KeyType>
class subA : public A {
public:
void insertValue(const KeyType &id, const std::string& value) {
if(_theMap.find(id) == _theMap.end()) {
_theMap.insert(std::make_pair(id, value));
}
}
private:
std::map<KeyType, std:string> _theMap;
};
然后您可以根据需要创建 typedef:
typedef subA<long> subA1;
typedef subA<std::string> subA2;
或者,如果您需要实际派生 classes:
class subA1 : public subA<long>
{
...
};
class subA2 : public subA<std::string>
{
...
};