ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

STL---初识容器算法迭代器

2020-03-15 10:02:03  阅读:187  来源: 互联网

标签:容器 vector end 迭代 STL back Person 初识 push


初识容器算法迭代器

1、 vector<类型> v 容器
2、 尾插 v.push_back
3、起始迭代器 v.begin() 指向第一个元素
4、结束迭代器 v.end() 指向容器最后一个元素下一个位置
5、for_each() 遍历, 引入头文件 algorithm
6、练习:内置类型、自定义类型、容器嵌套

#include <iostream>
#include <vector>
#include <algorithm>  //系统算法头文件
#include <string>


using namespace std;


void myPrint(int val)
{
    cout << val << endl;
}


//普通的指针也是一种迭代器
void test01()
{
    int arr[5] = {1, 5, 2, 7, 3};
    int *p = arr;
    for(int i = 0; i < 5; i++)
    {
        cout << *(p++)  << endl;
    }
}


//内置属性类型
void test02()
{
    vector<int> v;   //声明一个vector容器
    //向容器中添加数据
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    //通过迭代器可以遍历容器
    //每个容器都有自己的专属迭代器
    //第一种遍历方式
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    }

    //第二种遍历方式,系统算法
    for_each(v.begin(), v.end(), myPrint);


}

class Person
{
public:
    Person(string name, int age)
    {
        this->myName = name;
        this->myAge = age;
    }


    string myName;
    int myAge;
};



//自定义类型
void  test03()
{
    vector<Person> v;
    Person p1("a", 13);
    Person p2("b", 14);
    Person p3("c", 15);
    Person p4("d", 16);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    for(vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
    {
        //*it --- Person类型
        cout << " 姓名: "  << (*it).myName  << " 年龄: " << it->myAge << endl;
    }
}

void  test04()
{
    vector<Person*> v;
    Person p1("a", 13);
    Person p2("b", 14);
    Person p3("c", 15);
    Person p4("d", 16);

    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);

    for(vector<Person*>::iterator it = v.begin(); it != v.end(); it ++)
    {
        //*it --- Person*类型
        cout << " 姓名: "  << (*it)->myName  << " 年龄: " << (*it)->myAge << endl;
    }
}


//容器嵌套
void test05()
{
    vector< vector<int> > v ;   //相当于一个二维数组
    vector<int> v1;
    vector<int> v2;
    vector<int> v3;
    vector<int> v4;
    for (int i = 0; i < 10 ; i++)
    {
        v1.push_back(i+1);
        v2.push_back(i+10);
        v3.push_back(i+100);
        v4.push_back(i+1000);
    }

    v.push_back(v1);
    v.push_back(v2);
    v.push_back(v3);
    v.push_back(v4);


    for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
    {
        //(*it) ---vector<int>类型
        for (vector<int>::iterator it2 = (*it).begin(); it2 != (*it).end(); it2++)
        {
            // (*it2) --- int
            cout << *it2 ;
        }
        cout << endl;
    }



}

int main()
{
    //test01();
    //test02();
    //cout << "Hello world!" << endl;
    //test03();
    //test04();
    test05();

    return 0;
}

标签:容器,vector,end,迭代,STL,back,Person,初识,push
来源: https://blog.csdn.net/qq_42016124/article/details/104873803

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有