插入不会永久更改地图?
insert doesn't permanently change map?
我有一个 set<pair<string,map<string,int> > > setLabelsWords;
的成员变量,它有点令人费解,但却伴随着我。在同一类的成员功能中,我有以下代码:
pair<map<string,int>::iterator,bool> ret;
for (auto j:setLabelsWords) {
if (j.first == label) {
for (auto k:words) {
ret = j.second.insert(make_pair(k,1));
if (ret.second == false) {
j.second[k]++;
}
}
}
}
"单词"是一组字符串,"标签"是一个字符串。因此,基本上应该插入" k"(字符串(,如果k已经在地图中,则它将int递增。问题是它有效,直到循环最外面的循环结束为止。如果我在最后一个支架之前打印出J.Second的大小,它将给我一个我期望13的尺寸,但是在最后一个括号之外,大小又回到了7个,这是地图的大小最初是在此代码之前的。块运行。我非常困惑为什么会发生这种情况,并感谢任何帮助。
for (auto j:setLabelsWords) {
通过Value ,通过容器迭代。这与您一样:
class Whatever { /* something in here */ };
void changeWhatever(Whatever w);
// ...
{
Whatever w;
changewhatever(w);
}
changewhatever
对w
做的任何修改,无论是对w
的副本做出的,因为它按值逐渐值传递到函数。
为了正确更新您的容器,您必须通过参考迭代:
for (auto &j:setLabelsWords) {
for (auto j:setLabelsWords) {
这将创建每个元素的复制。您在j
上执行的所有操作都会影响该副本,而不是setLabelsWords
中的原始元素。
通常,您只需使用参考:
for (auto&& j:setLabelsWords) {
但是,由于std::set
的性质,这不会使您走远,因为std::set
的元素无法通过迭代器或对元素的引用自由修改,因为这将使您可以创建具有重复项的集合它。编译器将不允许。
这是一种务实的解决方案:使用std::vector
代替std::set
:
std::vector<std::pair<std::string, std::map<std::string,int>>> setLabelsWords;
您将能够使用上面说明的参考方法。
如果您以后需要std::set
的独特性和排序功能,则可以在std::vector
上应用std::sort
和/或std::unique
,或在std::vector
的元素中创建新的std::set
。
相关文章:
- 将一对插入地图并增加计数?
- 当我将其插入无序地图时,矢量的容量为 0
- 如何将元素插入到标准::地图的共享指针中?
- 无法插入到标准地图
- 为什么不插入地图?
- 为什么emplace_hint比插入地图慢?
- 地图插入(const_iterator提示,值)用法
- 无法将智能指针插入地图
- 插入到地图中,以对作为键并列出作为值
- 地图插入不明确
- VS 2017 错误 C2664 地图插入尝试
- 为什么通用参考概念不适用于函数指针的地图插入
- 地图插入将替换以前输入的值
- 地图插入密钥,但没有值
- 如何在CPU和内存中优化C 中的重型地图插入
- 我如何将MPL地图插入
- 地图::插入正在中断
- 我不明白地图插入上的此错误
- C++ 多重地图插入功能
- 地图插入返回错误答案的地图