C++ STL中的向量图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1380585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
map of vectors in STL?
提问by chen
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)
我知道如果我使用pointer for vector,如下就可以了,但是我不知道我是否可以避免使用pointer并使用上面的数据结构(我不想手动删除)
// 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>));
回答by fbrereto
The first data structure will work. You might want to typedef
some of the code to make future work easier:
第一个数据结构将起作用。您可能想要typedef
一些代码来使以后的工作更轻松:
typedef std::vector<MyClass> MyClassSet;
typedef std::map<int, MyClassSet> MyClassSetMap;
MyClassSetMap map;
map.insert(MyClassSetMap::value_type(10, MyClassSet()));
or (thanks quamrana):
或(感谢quamrana):
map[10] = MyClassSet();
回答by Steve Jessop
Yes, but your second line should be:
是的,但你的第二行应该是:
map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>()));
This inserts a pair consisting of the integer 10, and an empty vector. Both will be copied, and if you're dealing with large vectors then you'll want to be careful about copies.
这会插入一对由整数 10 和一个空向量组成的对。两者都将被复制,如果您正在处理大型向量,那么您需要小心复制。
Also: don't call variables "map" while using namespace std
. You're scaring me ;-)
另外:不要在 while 中调用变量“map” using namespace std
。你吓到我了 ;-)
回答by quamrana
Using the typedefs from fbrereton you can also do this:
使用 fbrereton 中的 typedef,您也可以这样做:
typedef std::vector<MyClass> MyClassSet;
typedef std::map<int, MyClassSet> MyClassSetMap;
MyClassSetMap map;
map[10]=MyClassSet();
You can use operator[]
instead of insert().
This saves on the line noise a bit.
您可以使用operator[]
而不是insert().
这样可以稍微节省线路噪音。
回答by Kurt Krueckeberg
Use the swap function to efficiently add your vector.
使用交换功能有效地添加您的向量。
map<int, vector<SomeClass> > Map;
vector<SomeClass> vec;
//...add elements to vec
Map[1] = vector<int>();
// swap the empty vector just inserted with your vector.
Map[1].swap(vec);
回答by Kirill V. Lyadvinsky
You should read compile error messages. They usually gives you all information that you need.
Your code gives error 'illegal use of this type as an expression'
in that string. That means that you use type, not an object. To use an object you could just add () for calling constructor with no arguments.
您应该阅读编译错误消息。他们通常会为您提供您需要的所有信息。
您的代码'illegal use of this type as an expression'
在该字符串中给出了错误。这意味着您使用类型,而不是对象。要使用对象,您只需添加 () 即可调用不带参数的构造函数。
map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>()));
By the way you could use std::make_pair to create pairs. It deduces argument types, so no need to explicitly indicate them.
顺便说一句,您可以使用 std::make_pair 来创建对。它推导出参数类型,因此无需明确指出它们。
map.insert( make_pair( 10, vector<MyClass>() ) );
回答by Martin York
You could use the [] operators.
These will insert the value into the map.
您可以使用 [] 运算符。
这些会将值插入到地图中。
map[10]; // create the 10 element if it does not exist
// using the default constructor.
If you are going to use soon after construction then:
如果您要在施工后不久使用,则:
std::vector<MyClass>& v = map[10];
Now its constructed and you have a local reference to the object.
现在它已构建,并且您拥有对该对象的本地引用。
回答by GutiMac
Lets use a little bit c++11 ;)
让我们使用一点 c++11 ;)
typedef std::vector<MyClass> MyClassSet;
typedef std::map<int, MyClassSet> MyClassSetMap;
MyClassSetMap map;
map.emplace(myid, MyClassSet());
To know if this was inserted you can do:
要知道这是否已插入,您可以执行以下操作:
const auto result = map.emplace(myid, MyClassSet());
return (result.second)
? "Is_OK"
: "Maybe "+myid+" exists\n";
And here is the flagshipof c++11 and maps.... how to insert in this map a pair if it doesn't exists and if it exists just insert a new element in the vector....
这是c ++ 11和地图的旗舰......如果它不存在,如何在这个地图中插入一对,如果它存在,只需在向量中插入一个新元素......
const auto result = map.emplace(myid, MyClassSet());
result.first->second.emplace(objSet);
I hope give a useful information!!!
希望给点有用的信息!!!
回答by UncleBens
You are just missing a pair of parenthesis:
你只是缺少一对括号:
map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>()));
Incidentally, there's a helper function std::make_pair which takes care of deducing the template arguments:
顺便说一下,有一个辅助函数 std::make_pair 负责推导模板参数:
map.insert(make_pair(10, vector<MyClass>()));
Considering using a pointer to dynamically allocated vector instead is a rather bad idea, since this will make you responsible for managing the instance. Also, since map should never move its contents around in memory, there is nothing to gain performance-wise neither.
考虑使用指向动态分配的向量的指针是一个相当糟糕的主意,因为这将使您负责管理实例。此外,由于 map 永远不应该在内存中移动其内容,因此在性能方面也没有任何好处。
回答by VIPK
// define my map of vector
map<int, vector<MyClass> > map;
MyClass *ptr = new MyClass();
map[0].push_back(ptr);
pushing objects into vector of type MyClass
将对象推入 MyClass 类型的向量中
//usage; map[0][vector_index]
map[0][0]->MyClassMember;
accessing member functions using vector_index
使用 vector_index 访问成员函数