Tim*_*Tim 10 c++ iterator naming-conventions
我觉得我在命名和使用迭代器的方式上不专业.我的意思是我"觉得"我应该把它们称为别的东西,但我总是根据"it_"前缀命名它们,过了一会儿,在一个长函数中,名字开始看起来都一样.
另外,我总是想知道我是否以一种"奇怪"的方式做事,因为我不知道更好.例如,如果我在迭代地图以显示其所有键/值对,我会这样做:
map<int, int>::const_iterator it = layout.begin();
for (; it != layout.end(); ++it)
{
cout << it->first << ":\t" << it->second << "\n";
}
我看到有些人称他们的迭代器"iter" - 我看到了其他循环方式.是否有任何超越风格的惯例,只是一种好的做法?
我尝试for尽可能在循环内声明迭代器,以便最小化迭代器标识符的范围。
首先,我将typedef“长”类型名称改为“短”且可重复使用的名称:
typedef std::map< int, int > IntMap;
typedef IntMap::const_iterator IntMapConstIter;
Run Code Online (Sandbox Code Playgroud)
那么,我做
for( IntMapConstIter it = layout.begin(); it != layout.end(); ++it ) {
....
}
Run Code Online (Sandbox Code Playgroud)