• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
bllovetx
Love You Forever!
博客园    首页    新随笔    联系   管理    订阅  订阅

c++11 auto 与auto& 遍历vector区别

目录
  • 说明
  • c++11 auto 与auto& 遍历区别
      • 今天被这个问题坑了一天,一直以为是算法错了,debug了一天,最后暴力生成数据才发现,测试代码如下:

说明

转载请注明出处:https://www.cnblogs.com/bllovetx/p/11669320.html
欢迎访问My Home Page
--2019.11.21

c++11 auto 与auto& 遍历区别

今天被这个问题坑了一天,一直以为是算法错了,debug了一天,最后暴力生成数据才发现,测试代码如下:

➜  test vim test.cpp
➜  test g++ test.cpp -o test
➜  test ./test              
original: 6610
auto: 6610
auto&: 1234
➜  test cat test.cpp 
#include<bits/stdc++.h>
using namespace std;
struct node{
        int i;
        string w;
};
int main(){
        vector<node>ns;
        ns.clear();
        ns.push_back({1,"6610"});
        cout<<"original: "<<ns[0].w<<endl;
        for(auto it:ns)it.w="1234";
        cout<<"auto: "<<ns[0].w<<endl;
        for(auto &it:ns)it.w="1234";
        cout<<"auto&: "<<ns[0].w<<endl;
}

区别显而易见,可读和可写。

posted @ 2019-10-14 01:20  bllovetx  阅读(5559)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3