What is the normal convention for naming and using iterators in C ++?

It seems to me that I'm unprofessional in the way I name and use iterators. I mean, I "feel" as if I should call them something else, but I always call them based on the prefix "it_", and after a while in a long function the names begin to look the same.

In addition, I am always curious that I do things in a “weird” way, which I only found out because I didn't know better. For example, if I iterated through a map to display all its key / value pairs, I would do the following:

  map <int, int> :: const_iterator it = layout.begin ();
 for (; it! = layout.end (); ++ it)
 {
 cout << it-> first << ": \ t" << it-> second << "\ n";
 } 

I see that some people call their iterators "iterators" - I see other ways of looping. Is there any agreement that goes beyond style and is just good practice?

+6
c ++ iterator naming-conventions
source share
6 answers

I am trying to declare iterators inside a for loop as much as possible, so that the scope of the iterator identifier is minimized.

First, I typedef names "long" of type "shorter" and reusable:

 typedef std::map< int, int > IntMap; typedef IntMap::const_iterator IntMapConstIter; 

Then i do

 for( IntMapConstIter it = layout.begin(); it != layout.end(); ++it ) { .... } 
+4
source share

I do it as follows

 for (map<int, int>::const_iterator it = layout.begin(); it != layout.end(); ++it) { cout << it->first << ":\t" << it->second << "\n"; } 

Thanks to this, the iterator is limited only within the block, which makes it safer to give it a shorter name.

+2
source share

I am sure that no convention is beyond the scope of this issue. I believe that the most important part of naming an iterator is in the second part (given that you decide to add a prefix if it is “he” or “iter_” or something else). Carefully select the second part, as for any other variable, and everything will be fine.

+2
source share

Would it be completely unreasonable to suggest using BOOST_FOREACH and for you (mostly) forget about iterators?

+1
source share

I have not seen a single canonical cycle.

The cost of creating an iterator is indicated as O(1) (based on the size of the container), but it can be worth it, especially if certain debugging options are activated.

Therefore, calling end at each iteration of the loop is wasteful.

Thus, the canonical way of writing a for loop is to compute both it and end in the first statement.

 typedef std::map<int,int> LayoutType; for (LayoutType::const_iterator it = layout.begin(), end = layout.end(); it != end; ++it) { // ... } 

I usually type in my container, but honestly, it makes no sense to specify an iterator, it is already available in the container and introduces too many synonyms, it does not help.

It could also be a personal quirk, but I prefer to specify a function rather than a type when I enter my typedef. The type may change during subsequent refactoring, the function will not (at least it will not be the same if it happens, so it will require a complete rewrite).

For example, what if you suddenly prefer: typedef std::unordered_map<int,int> LayoutType ? It still fits your needs, and you can possibly opt out of it without any rewriting costs (assuming you used a typedef).

+1
source share

The name is the least important thing for me, as long as it is understood and chosen accordingly.

It gets into endless naming disputes

  • m_ for members
  • final underscore for members
  • etc...
0
source share

All Articles