C++迭代器遍历容器

详见:
https://www.nowcoder.com/practice/0f7ab22e60ee4574a9d9c81412b26595?tpId=225&tqId=2193295&ru=/practice/f5e0b2ea13ee40308fcc275c0d06053f&qru=/ta/primary-grammar-cpp/question-ranking
具体做法:
对于迭代器,我们可以看成C++中的指针,它指向容器的某个位置,使用*可以访问该位置的值。

我们输入数据以后,将迭代器指向vector的第一个位置,然后遍历直到迭代器指向vector的末尾(注意:v.begin()这个位置是有值的,v.end()这个位置没有值,即它指向最后一个元素的后一个位置),我们输出其中迭代器经过的值,其中迭代像指针一样可以用++遍历。

从后往前输出是一样的,我们正序已经将迭代器移到了结尾,后面再将其逆向移回去,输出每个值即可,注意一开始的结尾是没有值的,要先自减一位才有值,直到到了vector的开头。

用while

#include <iostream>
// write your code here......
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main() {

    // write your code here......
    //定义容器对象
    vector<int> v;
    int a;
    //键盘输入数据,并放入容器中。这样不限制输入数据个数。
    while(cin >> a){
        v.push_back(a);
    }
    // 声明迭代器指针,指向容器开头位置
    vector<int>::iterator iter = v.begin();
    //正向遍历并输出元素
    while(iter != v.end()){
        cout << *iter << " ";
        iter++;
    }
    //输出空行
    cout << endl;
    //反向遍历并输出元素
    while(iter != v.begin()){
        iter--;
        cout << *iter << " ";
    }

    return 0;
}

用for

  • 正向: vector::iterator it;
    begin()指向第一个元素
    end()指向最后一个元素的后一个位置
    for(it=vc.begin();it!=vc.end();it++)
  • 反向: vector::reverse_iterator itr;
    for(it=vc.rbegin();it!=vc.rend();it++)
    rbegin()指向最后一个元素
    rend()指向第一个元素的前一个位置
#include <iostream>
// write your code here......
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main() {

    // write your code here......
    vector<int> vc(5);
    for(int i=0; i<5; i++){
        cin >> vc[i];
    }
    vector<int>::iterator  iter;
    for(iter=vc.begin(); iter!=vc.end(); iter++){
        cout << *iter << " ";
    }
    cout << endl;
    vector<int>::reverse_iterator riter;
    for(riter=vc.rbegin(); riter!=vc.rend(); riter++){
        cout << *riter << " ";
    }

    return 0;
}

#include <iostream>
// write your code here......
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main() {

    // write your code here......
    vector<int> v;
    int num;
    for(int i=0; i<5; i++){
        cin >> num;
        v.push_back(num);
    }
    for(vector<int>::iterator it = v.begin(); it!=v.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
    for(vector<int>::iterator it = v.end()-1; it != v.begin()-1; it--){ // 
        cout << *it << " ";
    }

    return 0;
}

#include <iostream>
// write your code here......
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main() {

    // write your code here......
    vector<int> v;
    int num;
    for(int i=0; i<5; i++){
        cin >> num;
        v.push_back(num);
    }
    for(vector<int>::iterator it = v.begin(); it!=v.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
    for(vector<int>::iterator it = v.end()-1; it != v.begin()-1; it--){ // 
        cout << *it << " ";
    }

    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用迭代器遍历set容器和map容器。下面是遍历这两种容器的示例代码: 遍历set容器: ```cpp std::set<int> mySet = {1, 2, 3, 4, 5}; // 使用迭代器遍历set容器 for (std::set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it) { int element = *it; // 获取当前元素的值 // 执行操作,例如打印或处理元素 } ``` 遍历map容器: ```cpp std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "cherry"}}; // 使用迭代器遍历map容器 for (std::map<int, std::string>::iterator it = myMap.begin(); it != myMap.end(); ++it) { int key = it->first; // 获取当前键的值 std::string value = it->second; // 获取当前值的值 // 执行操作,例如打印或处理键值对 } ``` 另外,C++11引入了范围-based for循环,也可以用于遍历set容器和map容器遍历set容器: ```cpp std::set<int> mySet = {1, 2, 3, 4, 5}; // 使用范围-based for循环遍历set容器 for (const auto& element : mySet) { // 执行操作,例如打印或处理元素 } ``` 遍历map容器: ```cpp std::map<int, std::string> myMap = {{1, "apple"}, {2, "banana"}, {3, "cherry"}}; // 使用范围-based for循环遍历map容器 for (const auto& pair : myMap) { int key = pair.first; // 获取当前键的值 std::string value = pair.second; // 获取当前值的值 // 执行操作,例如打印或处理键值对 } ``` 请注意,在遍历map容器时,使用`pair.first`获取键,使用`pair.second`获取值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值