我想在c++中使用Map的 vector 。 我有映射值列表,我想将其存储为 vector 中的映射。 我的要求是这样的: 1. 对于 N 列,我有一个值的映射。 2.我想维护N中每个i的映射。
我正在尝试在 Vector 中维护它>。 请让我知道如何让它发挥作用。
我正在使用以下代码添加映射
void fun()
{
vector< map < string, int> > myVect;
myVect.push_back(new map< string, int> );
myVect.push_back(new map< string, int> );
map< string, int> entry1 = myVect[0];
entry1["ABC"] = 1;
entry1["DEF"] = 2;
map< string, int> entry2 = myVect[1];
entry1["ABC"] = 5;
entry1["RKD"] = 9;
}
为什么不起作用?
最佳答案
在您的示例中,您使用指针(通过 new
)进行推回,而不是通过 const 引用或值。您的“ map vector ”声明很好,但插入会导致编译器错误。
要将 map 插入 vector ,请使用以下形式:
std::map<std::string,int> m;
// populate m if needed
x.push_back(m);
或者简单地
x.push_back(std::map<std::string,int>());
如果你想推送一张空 map 。
请注意,此处不需要 new
。
关于c++ - 在c++中实现Map的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804482/