如何在地图中添加值,并将向量作为值类型
How to add values in a map with a vector as a value type?
概述:我有一些代码可以遍历字符串(名称)列表以查找每个字符串的最后一个字符。我还有一个myGraph typedef映射,它将结构作为值类型。该结构包含向量节点下一个和向量next_cnt。
要做:每次在映射中插入新字符时,我需要将向量nextwt_vec初始化为空向量。
问题:使用以下代码,我的nextwt_vec保留了前一个字符的旧值。
map<char, vector<int> > nextmap;
for (myGraph::const_iterator j = graph.begin(); j != graph.end(); ++j)
{
vector<int> nextwt_vec;
//populating next map with char and weighted ints
for (int p=0; p< (int) (*j).second->nodenext.size(); ++p)
{
char cn = name[name.length() - 1];
int wt = (*j).second->next_cnt[p];
nextwt_vec.insert(nextwt_vec.begin()+p, wt);
//puts char as key and weighted int as value in nextmap
n->nextmap[cn] = nextwt_vec;
}
输出:我得到的:
char: A vec: 109
char: C vec: 109 vec: 48
我应该得到的输出:
char: A vec: 109
char: C vec: 48
感谢您的帮助!!
Drop vector nextwt_vec; 函数中的变量。直接使用 nextmap[cn].替换以下两行:
nextwt_vec.insert(nextwt_vec.begin()+p, wt);
//puts char as key and weighted int as value in nextmap
n->nextmap[cn] = nextwt_vec;
有了这个:
//puts char as key and weighted int as value in nextmap
n->nextmap[cn].insert(nextwt_vec.begin()+p, wt);;
相关文章:
- C++中函数的向量返回类型引发错误
- 具有 STL 向量类型成员的类的复制内存
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 是否可以有一个模板函数,可以将向量和非向量类型都作为参数
- 具有基元类型的模板向量类型
- 在索引和值类型上参数化的惯用向量类型
- 使用迭代器成员函数是否仅适用于某些向量类型"empty()"?
- 通过glUniform 将 GLM 的向量类型传递给 OpenGL
- 尝试将用户定义的向量类型插入用户定义的类型向量
- 使用类作为向量类型时出错 - "Undeclared identifier"
- 在C++中,当两个向量类型不同时,如何将值存储到向量内部的向量中
- 如何访问和存储向量类型的结构成员
- 按参数中的向量类型重载函数
- boost::variant:具有递归向量类型的奇怪行为
- LLVM 传递计数向量类型指令
- 用异构boost::融合向量类型数据填充std::vector
- 如何定义自定义类的向量类型
- 真正的动态向量类型
- 如何在类的属性/变量中使用组合类作为向量类型
- 更快地删除向量类型