sparrowwf 2019-04-09 23:21 采纳率: 100%
浏览 712
已采纳

C++ for循环遍历, 遍历加和不加&,为何结果差异这么大:for (auto p : ps) 与for (auto &p : ps)

问题:
见下面的代码和结果
1.加过地址打印,两种方式打印的地址全部完全相同,猜测是for遍历中始终使用一个临时中间遍历
2.不加引用符表征的是复制操作,更改不会同步;加了是别名引用,更改会同步?
3.如果2猜测是对的.那么&(p.name)地址值应该不一样.然后结果还是一样
4.那么该如何解释背后原理?

代码:
#include
#include
#include
#include "fact.h"
using namespace std;

class People {
public:
string name;
People(string n) {
name = n;
}
};

int main(int argc, char** argv) {
cout << "main test start" << endl;
vector ps;
ps.push_back(People("zhangshan"));
ps.push_back(People("lisi"));
ps.push_back(People("wangwu"));

for (auto p : ps){
    p.name += "123";
}
for (const auto p : ps){
    cout << p.name << endl;
}
cout << "================" << endl;
for (auto &p : ps){
    p.name += "123";
}
for (const auto p : ps){
    cout << p.name << endl;
}   
return 0;

}
结果:
#include
#include
#include
#include "fact.h"
using namespace std;

class People {
public:
string name;
People(string n) {
name = n;
}
};

int main(int argc, char** argv) {
cout << "main test start" << endl;
vector ps;
ps.push_back(People("zhangshan"));
ps.push_back(People("lisi"));
ps.push_back(People("wangwu"));

for (auto p : ps){
    p.name += "123";
}
for (const auto p : ps){
    cout << p.name << endl;
}
cout << "================" << endl;
for (auto &p : ps){
    p.name += "123";
}
for (const auto p : ps){
    cout << p.name << endl;
}   
return 0;

}

  • 写回答

1条回答 默认 最新

  • sparrowwf 2019-04-09 23:24
    关注

    抱歉,如上猜测.是log打印加错地方了.
    int main(int argc, char** argv) {
    cout << "main test start" << endl;
    vector ps;
    ps.push_back(People("zhangshan"));
    ps.push_back(People("lisi"));
    ps.push_back(People("wangwu"));

    for (auto p : ps){
        p.name += "123";
        cout << &(p.name) << endl;
    }
    for (const auto p : ps){
        cout << p.name << endl;
    }
    cout << "================" << endl;
    for (auto &p : ps){
        p.name += "123";
        cout << &(p.name) << endl;
    }
    for (const auto p : ps){
        cout << p.name << endl;
    }   
    return 0;
    

    }

    int main(int argc, char** argv) {
    cout << "main test start" << endl;
    vector ps;
    ps.push_back(People("zhangshan"));
    ps.push_back(People("lisi"));
    ps.push_back(People("wangwu"));

    for (auto p : ps){
        p.name += "123";
        cout << &(p.name) << endl;
    }
    for (const auto p : ps){
        cout << p.name << endl;
    }
    cout << "================" << endl;
    for (auto &p : ps){
        p.name += "123";
        cout << &(p.name) << endl;
    }
    for (const auto p : ps){
        cout << p.name << endl;
    }   
    return 0;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误