STL like container typedef shortcut?

后端 未结 7 1121
有刺的猬
有刺的猬 2021-01-26 22:56

A common pattern with STL containers is this:

map map;
for(map::iterator iter = map.begin(); iter != map.end(); ++iter)
{
  .         


        
相关标签:
7条回答
  • 2021-01-26 23:42

    C++0x will also offer ranged-based for loop, which is similar to iterative for looping in other languages.

    Unfortunately, GCC does not yet implement range-based for (but does implement auto).

    Edit: In the meanwhile, also consider typedefing the iterator. It doesn't get around the one-use typedef (unless you put that in a header, which is always an option), but it makes the resulting code shorter by one ::iterator.

    0 讨论(0)
提交回复
热议问题