• c++迭代器STL中的vector简单概述


    1.c++迭代器的位置

    在这里插入图片描述

    2.STL六大组件

    容器 算法 迭代器 仿函数 适配器 空间配置器

    容器 : 存放数据
    算法 : 操作数据
    迭代器 : 容器和算法的桥梁
    仿函数 : 为算法 提供更多的策略
    适配器 : 为算法提供更多的参数接口
    空间配置器 : 管理容器和算法的空间

    3.STL中for_each的使用

    for_each(容器起始迭代器 , 容器结束迭代器 , 自定义打印函数);

    // STL中for_each的使用
    #include 
    #include 
    #include 
    #include //要使用for_each必须包含此头文件
    using namespace std;
    
    
    class Person
    {
    public:
        string name;
        int age;
        Person(string name, int age)
        {
            this->name = name;
            this->age = age;
        }
    };
    
    void myPrintf(Person &ob)
    {
        cout << "name:" << ob.name << ",age:" << ob.age << endl;
    }
    
    int main()
    {
        Person ob1("哈哈", 1);
        Person ob2("嘿嘿", 2);
        Person ob3("哼哼", 3);
        Person ob4("嘻嘻", 4);
    
        vector<Person> v;
        v.push_back(ob1);
        v.push_back(ob2);
        v.push_back(ob3);
        v.push_back(ob4);
    
        for_each(v.begin(), v.end(), myPrintf);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

    4.STL中容器嵌套容器

    // STL中容器嵌套容器
    #include 
    #include 
    #include 
    #include //要使用for_each必须包含此头文件
    using namespace std;
    
    
    
    int main()
    {
        vector<int>v1;
        vector<int>v2;
        vector<int>v3;
    
        v1.push_back(10);
        v1.push_back(20);
        v1.push_back(30);
        v1.push_back(40);
    
        v2.push_back(100);
        v2.push_back(200);
        v2.push_back(300);
        v2.push_back(400);    
    
        v3.push_back(1000);
        v3.push_back(2000);
        v3.push_back(3000);
        v3.push_back(4000);
    
        //定义一个vector,将v1,v2,v3存放进去
        vector< vector<int> >v;
    
        v.push_back(v1);
        v.push_back(v2);
        v.push_back(v3);
    
        //for循环遍历
        for( vector<vector<int>>::iterator it = v.begin();it!=v.end();it++ ){
    
            //*it == vector v1 v2 v3
            for(vector<int>::iterator mit = (*it).begin();mit!=(*it).end();mit++){
                //*mit == int
                cout<<*mit<<" ";
            }
            cout<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

    5.find算法(需要头文件#include< algorithm >)

    #include
    #include
    #include//find算法包含在这里边
    
    
    using namespace std;
    int main(){
        vector<int>v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(40);
        vector<int>::iterator ret;
        ret=find(v.begin(),v.end(),20);//找20
        if(ret!=v.end()){
            cout<<"找到数据"<<*ret<<endl;
        }else{
            cout<<"没找到数据20"<<endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    找到数据20
    
    • 1

    6.find_if算法(需要头文件#include< algorithm >)

    #include 
    #include 
    #include  //find_if算法包含在这里边
    
    bool MySort20(int val)
    {
        return val > 20;
    }
    
    class MySort
    {
    public:
        bool operator()(int val){
            return val>20;
        }
    };
    
    using namespace std;
    int main()
    {
        vector<int> v;
        v.push_back(10);
        v.push_back(20);
        v.push_back(30);
        v.push_back(40);
        vector<int>::iterator ret;
        //方一:
        //ret = find_if(v.begin(), v.end(), MySort20); //找>20的第一个数
        
        //方二:
        ret = find_if(v.begin(), v.end(), MySort());
    
        if (ret != v.end())
        {
            cout << "找到数据" << *ret << endl;
        }
        else
        {
            cout << "没找到>20的数据" << endl;
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    找到数据30
    
    • 1

    7.双重vector的遍历(vector)

    class Solution {
    public:
        int countNegatives(vector<vector<int>>& grid) {
            int res = 0;
            //方一:
            for(auto it1=grid.begin();it1 !=grid.end() ; it1++){
                for(auto it2 = it1->begin() ; it2 !=it1->end() ; it2++){
                    if((*it2)<0)res++;
                }
            }
            return res;
        }
    };
    
    /*
    方二:
    int m = nums.size(),n = nums[0].size();
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout<
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    CentOS7.9系统部署(nginx+uwsgi+flask)项目
    SystemC学习(2)— D触发器的建模与测试
    多进程编程(一):基本概念
    TreeMap匿名内部类使用Comparator方法(比较器)被替换
    C++之构造函数、析构函数、拷贝构造函数终极指南:玩转对象的诞生、生命周期与复制
    GPS定位原理
    9.2.4 【MySQL】段的结构
    vue子组件向父组件传参的方式
    小程序, 多选项
    vue3组件之间通讯
  • 原文地址:https://blog.csdn.net/weixin_52668597/article/details/126922278