我了解您想要地图的问题:key->map<key,>::iterator
所以,这里是一个以映射迭代器为值的结构:
template <
template <class K, class V, class C, class A> class mapImpl,
class K,
class V,
class C=std::less<K>,
class A=std::allocator<std::pair<const K, V> >
>
class value_with_iterator {
public:
typedef typename mapImpl<const K,value_with_iterator,C,A>::iterator value_type;
value_type value;
};
使用上述结构定义的映射:
typedef std::map<size_t, value_with_iterator <std::map, size_t, size_t> > map_size_t_to_itself;
一些插入方法 - 将密钥与自身链接:
map_size_t_to_itself::iterator insert(map_size_t_to_itself& mapRef, size_t value)
{
map_size_t_to_itself::value_type v(value, map_size_t_to_itself::mapped_type());
std::pair<map_size_t_to_itself::iterator, bool> res = mapRef.insert(v);
if (res.second)
res.first->second.value = res.first;
return res.first;
}
和简单的测试:
int main() {
map_size_t_to_itself mapObj;
map_size_t_to_itself::iterator i1 = insert(mapObj, 1);
map_size_t_to_itself::iterator i2 = insert(mapObj, 1);
map_size_t_to_itself::iterator i3 = insert(mapObj, 2);
std::cout << i1->first << ": " << i1->second.value->first << std::endl;
std::cout << i2->first << ": " << i2->second.value->first << std::endl;
std::cout << i3->first << ": " << i3->second.value->first << std::endl;
}
输出:
1: 1
1: 1
2: 2
完整链接:http: //ideone.com/gnEhw