为什么迭代器在与map.begin()一起使用时预先增加了
Why is the iterator preincremented when using with map.begin()?
http://www.cplusplus.com/reference/map/map/begin/
// map::begin/end
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << 'n';
return 0;
}
为什么迭代器是在for循环中预先增加的?
BoBTFish的评论是正确的:使用预加密是因为您是这样写的。在我的其余回答中,我将解释为什么此选项是首选,即,这是推荐的做法。
在表达式的点上没有使用增量之前的迭代器的值。表达式只想增加迭代器,而不使用它以前的值。
在这种情况下,预注册操作员是正确的,应该是首选。它保存了在增量之前存储值的要求,这对于后增量运算符来说总是存在的。
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 有效地使用std::unordered_map来插入或增加键的值
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 为什么迭代器在与map.begin()一起使用时预先增加了