立山 发表于 2024-7-23 20:44:59

C++之迭代器


[*]1. 什么是迭代器?
[*]2. 如何使用迭代器
[*]3. C++迭代器说明
[*]4. 迭代器的高级应用

[*]4.1. Enumerator.hpp
[*]4.2. Iterator.cpp
[*]4.3. 输出结果
[*]4.4. 更多详细代码

1. 什么是迭代器?

迭代器(Iterator)是按照肯定的顺序对一个或多个容器中的元素从前往遍历的一种机制,比如for循环就是一种最简单的迭代器,对一个数组的遍历也是一种的迭代遍历的过程。GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需袒露该对象的内部细节。迭代器有时也称为枚举器(Enumerator),其布局图如下:
https://img2024.cnblogs.com/other/504387/202407/504387-20240723212002045-1554343931.png
迭代器布局图
迭代器其实就是维护一个当前的指针,这个指针可以指向当前的元素,可以返回当前所指向的元素,可以移到下一个元素的位置,通过这个指针可以遍历容器的全部元素。迭代器一般至少会有以下几种方法:
First(); //将指针移至第一个位置或获得第一个元素
GetCurrent(); //获恰当前所指向的元素
MoveNext(); //移至下一个元素
2. 如何使用迭代器

既然迭代器是封装内里的实现细节,对外提供方便访问容器元素的接口,那我们就先从使用的角度熟悉迭代器,看看C++是如何使用迭代器的。
迭代器示例
void TestIterator()
{
    vector<int> vec;   // 定义一容器
    for(int i = 0; i < 5; i++)
    {
      vec.push_back(i*2);//添加元素
    }
    //用迭代器访问容器中的每个元素
    cout << "iterator vector:" << endl;
    for(vector<int>::iterator itr = vec.begin(); itr != vec.end(); itr ++)
    {
      cout << *itr << "   "; //itr是一个指针,指向当前的元素, 所以要解引用获得元素值
    }
    cout << endl;

    map<int, string> student; //创建一个map,对应学号-姓名的键值对
    //添加元素
    student.insert(pair<int, string>(1, "张三"));
    student.insert(pair<int, string>(3, "王五"));
    student.insert(pair<int, string>(2, "李四"));
    //遍历容器中的元素
    cout << "iterator map:" << endl;
    for (map<int, string>::iterator itr = student.begin(); itr != student.end(); itr ++)
    {
      cout << itr->first << "-->" << itr->second << endl;
    }
}4.2. Iterator.cpp

调用代码
#include "Company.h"#include "Enumerator.hpp"#include "Person.h"#include #include #include int main(){    int empId = 1;    auto pPerson11 = new Developer(empId++, "Developer11", "C++", "智慧都会");    auto pPerson12 = new Developer(empId++, "Developer12", "Java", "智慧都会");    auto pPerson13 = new Developer(empId++, "Developer13", "JavaScript", "智慧都会");    auto pPerson14 = new Tester(empId++, "Tester15", "LoadRunner");    auto pPerson15 = new Tester(empId++, "Tester16", "黑盒测试");    auto pDepartMent1 = new Department("开发1部");    pDepartMent1->AddPerson(pPerson11);    pDepartMent1->AddPerson(pPerson12);    pDepartMent1->AddPerson(pPerson13);    pDepartMent1->AddPerson(pPerson14);    pDepartMent1->AddPerson(pPerson15);    auto pPerson21 = new Developer(empId++, "Developer21", "IOS", "智能语音");    auto pPerson22 = new Developer(empId++, "Developer22", "Android", "智能语音");    auto pPerson23 = new Tester(empId++, "Tester24", "TestIn");    auto pDepartMent2 = new Department("开发2部");    pDepartMent2->AddPerson(pPerson21);    pDepartMent2->AddPerson(pPerson22);    pDepartMent2->AddPerson(pPerson23);    auto pPerson31 = new Developer(empId++, "Developer31", "C++", "电子书内核");    auto pPerson32 = new Tester(empId++, "Tester35", "LoadRunner");    auto pDepartMent3 = new Department("内核研发部");    pDepartMent3->AddPerson(pPerson31);    pDepartMent3->AddPerson(pPerson32);    Company company("阳光教育");    company.AddDepartment(pDepartMent1);    company.AddDepartment(pDepartMent2);    company.AddDepartment(pDepartMent3);    // 遍历全部开发者    std::cout
页: [1]
查看完整版本: C++之迭代器