使用可变模板的多键映射 我试图用C++中的可变模板实现一个具有不同访问键的映射。我想得到的是使这种语法起作用: MultikeyMap map1;//int和double是键,float是值类型 map1[2]=3.5; map1[5.7]=22; MultikeyMap map2;//更多键,int是值类型 map2[100000000000ULL]=56; //等等。。。
我现在看到的是:使用可变模板的多键映射 我试图用C++中的可变模板实现一个具有不同访问键的映射。我想得到的是使这种语法起作用: MultikeyMap map1;//int和double是键,float是值类型 map1[2]=3.5; map1[5.7]=22; MultikeyMap map2;//更多键,int是值类型 map2[100000000000ULL]=56; //等等。。。,c++,templates,c++11,variadic-templates,variadic,C++,Templates,C++11,Variadic Templates,Variadic,我现在看到的是: template<class V, class... Krest> class MultikeyMap; template<class V, class K, class... Krest> class MultikeyMap<V, K, Krest...> : protected std::map<K, V>, protected MultikeyMap&
template<class V, class... Krest>
class MultikeyMap;
template<class V, class K, class... Krest>
class MultikeyMap<V, K, Krest...> : protected std::map<K, V>,
protected MultikeyMap<V, Krest...>
{
public:
template<class T>
void insert( const T& t, const V& v )
{
std::map<T, V>::insert( make_pair( t, v ));
}
template<class T>
const V* find( const T& k )
{
typedef std::map<T,V> currentMap;
currentMap::const_iterator it = currentMap::find( k );
return it == currentMap::end() ? 0 : &it->second;
}
};
template<class V>
class MultikeyMap<V>
{};
模板
类多键映射;
模板
类MultikeyMap:protected std::map,
受保护的多密钥映射
{
公众:
模板
无效插入(常数T&T、常数V&V)
{
std::map::insert(make_对(t,v));
}
模板
常数V*查找(常数T&k)
{
typedef std::map currentMap;
currentMap::const_迭代器it=currentMap::find(k);
返回它==currentMap::end()?0:&it->second;
}
};
模板
类多键映射
{};
我没有在insert和find中返回迭代器来简化代码
我认为这个解决方案有两个主要缺陷
首先,值类型位于模板参数列表的第一位。起初我试着写作
template<class K, class... Krest, class V>
class MultikeyMap<K, Krest..., V>
模板
类多键映射
但编译器坚持认为,“如果类模板部分专门化的参数是包扩展,那么它应该是最后一个参数”
Second是std::maps的受保护继承。我真的想用合成来代替它,但在这种情况下,我看不到访问存储地图的方法。如果有一个静态\u如果,我会写
template<class V, class K, class... Krest>
class MultikeyMap<V, K, Krest...> : protected MultikeyMap<V, Krest...>
{
public:
template<class T>
void insert( const T& t, const V& v )
{
static if( is_same<T,K>::value )
m_map.insert( make_pair( t, v ));
else
MultikeyMap<V, Krest...>::insert( t, v );
}
private:
std::map<K,V> m_map;
};
模板
类MultikeyMap:受保护的MultikeyMap
{
公众:
模板
无效插入(常数T&T、常数V&V)
{
静态if(相同::值)
m_图.插入(使_对(t,v));
其他的
多键映射::插入(t,v);
}
私人:
std::map mu map;
};
请就我提到的问题提出建议。如果有更好的方法,我很乐意学习
感谢阅读。更简单但不完全相同的方法可能是或 前者是一个映射,其中键可以查找值,反之亦然,而后者则更一般:它是一个具有任意数量索引的容器,允许顺序(“类似列表”)、随机访问(“类似向量”)、关联(“类似映射”)和散列访问 您可以尝试围绕Boost.MultiIndex包装您的可变模板,然后至少不必重新实现所有插入/擦除逻辑(但只需精简包装)
注意:Boost.MultiIndex不需要类型的可变序列,您还可以使用成员函数的可变序列提取用户定义类的各种数据成员作为主要数据类型。我将这样做:
template<class V, class K, class... Krest>
class MultikeyMap : MultikeyMap<V, Krest...>,
MultikeyMap<V, K>
{
using ParentMM = MultikeyMap<V, Krest...>;
using Parent = MultikeyMap<V, K>;
public:
using ParentMM::insert;
using Parent::insert;
using ParentMM::find;
using Parent::find;
using ParentMM::operator[];
using Parent::operator[];
};
template<class V, class K>
class MultikeyMap<V, K>
{
std::map<K, V> k_map;
public:
void insert(const K& k, const V& v)
{
k_map.insert(std::make_pair(k, v));
}
const V* find( const K& k ) const
{
auto it = k_map.find(k);
if (it != k_map.end())
return &it->second;
return nullptr;
}
V& operator[](const K& k)
{
return k_map[k];
}
};
MultikeyMap
:int
被引用为两次键,这是真的吗?您将如何知道用户是在第一个还是第二个int
?而且,我也不清楚你想要实现什么;是否应在所有地图中同时显示一个值?因为如果它只存在于一个映射中,那么拥有一个大的公共容器就没有意义了。@MatthieuM.:感谢这一点,应该有一个int。该值现在应该存在于一个映射中,但稍后我将为已经存储的值添加另一个键的功能。
template<class Head, class... Tail>
struct Helper : Helper<Tail...> {
using Last = typename Helper<Tail...>::Last;
};
template<class T>
struct Helper<T> {
using Last = T;
};
template<class K, class... Rest>
class MultikeyMap : MultikeyMap<Rest...>,
MultikeyMap<K, typename Helper<Rest...>::Last>
{
using ParentMM = MultikeyMap<Rest...>;
using Parent = MultikeyMap<K, typename Helper<Rest...>::Last>;
public:
using ParentMM::insert;
using Parent::insert;
using ParentMM::find;
using Parent::find;
using ParentMM::operator[];
using Parent::operator[];
};
template<class K, class V>
class MultikeyMap<K, V>
{
std::map<K, V> k_map;
public:
void insert(const K& k, const V& v)
{
k_map.insert(std::make_pair(k, v));
}
const V* find( const K& k ) const
{
auto it = k_map.find(k);
if (it != k_map.end())
return &it->second;
return nullptr;
}
V& operator[](const K& k)
{
return k_map[k];
}
};