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

迭代器遍历容器

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;
}

全部评论

相关推荐

01-15 13:52
已编辑
河南大学 Java
六年要多久:标准头像,不吃香菜😂
点赞 评论 收藏
分享
MingoTree:看不出你你的技术栈,想找什么工作,然后课设项目别写上去了,自我评价删了,前后端你想好你要干啥,这种简历投上去秒挂的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务