地图矢量在STL? [英] map of vectors in STL?
本文介绍了地图矢量在STL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想有一个向量的地图(但我不想使用内部向量的指针),是否可能?
I want to have a map of vectors, (but I don't want to use pointer for the internal vector), is it possible?
// define my map of vector
map<int, vector<MyClass> > map;
// insert an empty vector for key 10. # Compile Error
map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>));
我知道如果我使用指针作为向量,如下所示,不知道我是否可以避免使用指针并使用上面的数据结构(我不想手动删除)
I know that if I have used pointer for vector, as follows, it would be fine, but I wonder if I can avoid using pointer and use the above data structure (I don't want to manually delete)
// define my map of vector
map<int, vector<MyClass>* > map;
// insert an empty vector for key 10.
map.insert(pair<int, vector<MyClass>* >(10, new vector<MyClass>));
推荐答案
您可能希望 typedef
某些代码使以后的工作更容易:
The first data structure will work. You might want to typedef
some of the code to make future work easier:
typedef std::vector<MyClass> MyClassSet;
typedef std::map<int, MyClassSet> MyClassSetMap;
MyClassSetMap map;
map.insert(MyClassSetMap::value_type(10, MyClassSet()));
或(感谢 quamrana ):
map[10] = MyClassSet();
这篇关于地图矢量在STL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文