关于vector迭代器定义和auto直接定义

今天在做题的时候碰到一个很纳闷的题,就是在遍历vector<pair<int,int> >的时候用迭代器标准定义迭代器遍历和auto直接定义迭代器
二者形式不同,并且调用first和second元素的形式也不同!!!

一、正确使用迭代器标准定义形式代码如下:

for(vector<pair<int,int> >::iterator it = add.begin() ; it != add.end() ; it ++){
        int x = find(it->first);
        a[x] += it->second;
    }

错误使用迭代器标准定义形式代码如下:
1.

for(vector<pair<int,int> >::iterator it **:** add){
        int x = find(it->first);
        a[x] += it->second;
    }
  2.
for(vector<pair<int,int> >::iterator it = add.begin() ; it != add.end() ; it ++){
        int x = find(it**.**first);
        a[x] += it**.**second;
    }

二、正确使用auto定义迭代器

for(auto it **:** add){
        int x = find(it**.**first);
        a[x] += it**.**second;
    }

真的是匪夷所思,楼主先把问题扔这,接着查资料去了!!!

原文地址:https://www.cnblogs.com/ZhaoHaoFei/p/13762193.html