题解 | #迭代器遍历容器#

迭代器遍历容器

https://www.nowcoder.com/practice/0f7ab22e60ee4574a9d9c81412b26595

#include <iostream>
// write your code here......
#include <vector>
using namespace std;

int main() {

    // write your code here......
    vector<int> arr{};
    int data{},n{5};
    while (n--) {
        std::cin>>data;
        arr.push_back(data);
    }

//只遍历不修改数据,保证数据不变,使用带有const的遍历
//decltype()判定类型只与表达式相关,auto则与赋值相关
    decltype(auto) iter_front{arr.cbegin()};
    decltype(auto) iter_back{arr.crbegin()};
    for(;iter_front!=arr.cend();iter_front++){  
        cout<<*iter_front<<" ";
    }
    cout<<endl;
    for(;iter_back!=arr.crend();iter_back++){
        cout<<*iter_back<<" ";
    }


    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
Steven267:这不喷回去?花钱是大爷,记住这个道理
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务