はい、いいえ (使用しているコンテナによって異なります):
- for(iterator pos = range.begin(); pos != range.end(); ++pos) { /* with a range variable = *pos */ ... } のようなループです。
- 演算子 [] は別のことを行う場合があります (たとえば、キーが存在しない場合、std::map 演算子はキーを検索して新しいエントリを作成します)。
例:
#include <iostream>
#include <map>
int main()
{
typedef std::map<int, int> map;
map m = { { 0, 0 }, { 2, 2 }, { 4, 4 } };
for(const auto& e : m) {
std::cout << e.first << " ";
}
std::cout << std::endl;
for(map::size_type i = 0; i < m.size(); ++i) {
std::cout << m[i] << " ";
}
std::cout << std::endl;
return 0;
}
結果は次のとおりです。
0 2 4
0 0 2 0 4
(2番目の結果は、自分の足での良いショット、または意図したものである可能性があります)