Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there prettier syntax for a c++ iterator?

Is there a prettier / less-verbose way to use iterators in C++? From the tutorials I've seen, I either set up typedefs everywhere (which gets tedious to do for a lot of one-off for-loops):

typedef std::vector<std:pair<int, int> >::iterator BlahIterator;

or have verbose-looking for loops like:

for (std::vector<std:pair<int, int> >::iterator it = ... ) ...

Is there a better way?

like image 657
bd1 Avatar asked Aug 16 '11 02:08

bd1


1 Answers

In C++11 you can use the range-based for loop combined with the auto keyword:

for (auto& it : v) ...
like image 60
kalev Avatar answered Oct 13 '22 00:10

kalev