C++中循环的冒号
colon in for loop in C++
在下面的代码中,我试图编辑一个无序映射的内容:
class A {
public:
A() {std::cout << "Constructor called, address=" << this << std::endl;val=1;}
int val;
};
void main(void) {
std::unordered_map<int,A> um;
for (int i=0;i<3;i++)
std::cout << "um[" << i << "]=" << &(um[i]) << ", val=" << um[i].val << std::endl;
for (auto it : um)
std::cout << "&it.second=" << &(it.second) << ", val=" << it.second.val << std::endl;
int index = 1;
for (auto um_it : um)
um_it.second.val = index++;
for (int i=0;i<3;i++)
std::cout << "um[" << i << "]=" << &(um[i]) << ", val=" << um[i].val << std::endl;
for (auto it : um)
std::cout << "&it.second=" << &(it.second) << ", val=" << it.second.val << std::endl;
}
上述输出为:
Constructor called, address=0x8dcb2c
um[0]=0x8dcb2c, val=1
Constructor called, address=0x8dc7ac
um[1]=0x8dc7ac, val=1
Constructor called, address=0x8dc42c
um[2]=0x8dc42c, val=1
&it.second=0x7ffc62f24484, val=1
&it.second=0x7ffc62f24484, val=1
&it.second=0x7ffc62f24484, val=1
um[0]=0x8dcb2c, val=1
um[1]=0x8dc7ac, val=1
um[2]=0x8dc42c, val=1
&it.second=0x7ffc62f24484, val=1
&it.second=0x7ffc62f24484, val=1
&it.second=0x7ffc62f24484, val=1
当我用替换编辑代码时
int index = 1;
for (auto um_it = um.begin(); um_it != um.end(); ++um_it)
um_it->second.val = index++;
输出为:
Constructor called, address=0x9d8b2c
um[0]=0x9d8b2c, val=1
Constructor called, address=0x9d87ac
um[1]=0x9d87ac, val=1
Constructor called, address=0x9d842c
um[2]=0x9d842c, val=1
&it.second=0x7fffd2201c34, val=1
&it.second=0x7fffd2201c34, val=1
&it.second=0x7fffd2201c34, val=1
um[0]=0x9d8b2c, val=3
um[1]=0x9d87ac, val=2
um[2]=0x9d842c, val=1
&it.second=0x7fffd2201c34, val=1
&it.second=0x7fffd2201c34, val=2
&it.second=0x7fffd2201c34, val=3
我从结果中了解到,在第一个版本中,代码会影响对象的副本,但这似乎很奇怪。我还期望调用复制构造函数,但没有。如果有人能解释幕后发生了什么,以及在不制作冗余副本的情况下迭代未定义的映射的最佳方法是什么,我会很高兴。
在行中:
for (auto um_it : um)
um_it
不是迭代器。它是映射中某个值的副本。然后修改循环体中的副本,使贴图保持不变。这将为每个迭代调用A
的复制构造函数。
相反,您可以参考操作:
for ( auto& um_item : um )
当CCD_ 3引用映射中的条目时。(使用基于范围的for循环时,不能访问底层迭代器,只能访问每个元素或其副本)。
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 在循环C++中指定字符串之后,不会打印该字符串