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

迭代器遍历容器

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

#include <iostream>
#include <vector>

using namespace std;

int main() {

    // 使用正向迭代器和反向迭代器分别遍历输出 vector 中的元素
    vector<int> v;
    int n;
    //输入整数保存在容器v中
    while(cin >> n){
        v.push_back(n);
    }
    //设置it指针(正向迭代器)
    vector<int>::iterator it = v.begin();
    for(; it != v.end(); it++){
        cout << *it << ' ';
    }
    cout << endl;
    //设置i指针(反向迭代器)
    vector<int>::reverse_iterator i = v.rbegin();
    for (; i != v.rend(); i++){
        cout << *i << ' ';
    }
    cout << endl;

    return 0;
}

#你的秋招进展怎么样了##我的求职思考##零基础学习C++#
全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:20
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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