網(wǎng)站首頁 編程語言 正文
1. 迭代器簡介
為了提高C++編程的效率,STL(Standard Template Library)中提供了許多容器,包括vector、list、map、set等。然而有些容器(vector)可以通過下標(biāo)索引的方式訪問容器里面的數(shù)據(jù),但是大部分的容器(list、map、set)不能使用這種方式訪問容器中的元素。為了統(tǒng)一訪問不同容器時的訪問方式,STL為每種容器在實現(xiàn)的時候設(shè)計了一個內(nèi)嵌的iterator類,不同的容器有自己專屬的迭代器(專屬迭代器負責(zé)實現(xiàn)對應(yīng)容器訪問元素的具體細節(jié)),使用迭代器來訪問容器中的數(shù)據(jù)。除此之外,通過迭代器可以將容器和通用算法結(jié)合在一起,只要給予算法不同的迭代器,就可以對不同容器執(zhí)行相同的操作,例如find查找函數(shù)(因為迭代器提供了統(tǒng)一的訪問方式,這是使用迭代器帶來的好處)。迭代器對一些基本操作如*、->、++、==、!=、=進行了重載,使其具有了遍歷復(fù)雜數(shù)據(jù)結(jié)構(gòu)的能力,其遍歷機制取決于所遍歷的容器,所有迭代器的使用和指針的使用非常相似。通過begin,end函數(shù)獲取容器的頭部和尾部迭代器,end迭代器不包含在容器之內(nèi),當(dāng)begin和end返回的迭代器相同時表示容器為空。
STL主要由?容器、迭代器、算法、函數(shù)對象、和內(nèi)存分配器?五大部分構(gòu)成。
2. 迭代器的實現(xiàn)原理
首先,看看STL中迭代器的實現(xiàn)思路:
從上圖中可以看出,STL通過類型別名的方式實現(xiàn)了對外統(tǒng)一;在不同的容器中類型別名的真實迭代器類型是不一樣的,而且真實迭代器類型對于++、--、*、->等基本操作的實現(xiàn)方式也是不同的。(PS:迭代器很好地詮釋了接口與實現(xiàn)分離的意義)
既然我們已經(jīng)知道了迭代器的實現(xiàn)思路,現(xiàn)在如果讓我們自己設(shè)計一個list容器的簡單迭代器,應(yīng)該如何實現(xiàn)呢?
1.list類需要有操作迭代器的方法
1.begin/end
2.insert/erase/emplace
2.list類有一個內(nèi)部類list_iterator
1.有一個成員變量ptr指向list容器中的某個元素
2.iterator負責(zé)重載++、--、*、->等基本操作
3.list類定義內(nèi)部類list_iterator的類型別名
以上就是實現(xiàn)一個list容器的簡單迭代器需要考慮的具體細節(jié)。
3. 迭代器的簡單實現(xiàn)
my_list.h(重要部分有注釋說明)
// // Created by wengle on 2020-03-14. // #ifndef CPP_PRIMER_MY_LIST_H #define CPP_PRIMER_MY_LIST_H #include <iostream> template<typename T> class node { public: T value; node *next; node() : next(nullptr) {} node(T val, node *p = nullptr) : value(val), next(p) {} }; template<typename T> class my_list { private: node<T> *head; node<T> *tail; int size; private: class list_iterator { private: node<T> *ptr; //指向list容器中的某個元素的指針 public: list_iterator(node<T> *p = nullptr) : ptr(p) {} //重載++、--、*、->等基本操作 //返回引用,方便通過*it來修改對象 T &operator*() const { return ptr->value; } node<T> *operator->() const { return ptr; } list_iterator &operator++() { ptr = ptr->next; return *this; } list_iterator operator++(int) { node<T> *tmp = ptr; // this 是指向list_iterator的常量指針,因此*this就是list_iterator對象,前置++已經(jīng)被重載過 ++(*this); return list_iterator(tmp); } bool operator==(const list_iterator &t) const { return t.ptr == this->ptr; } bool operator!=(const list_iterator &t) const { return t.ptr != this->ptr; } }; public: typedef list_iterator iterator; //類型別名 my_list() { head = nullptr; tail = nullptr; size = 0; } //從鏈表尾部插入元素 void push_back(const T &value) { if (head == nullptr) { head = new node<T>(value); tail = head; } else { tail->next = new node<T>(value); tail = tail->next; } size++; } //打印鏈表元素 void print(std::ostream &os = std::cout) const { for (node<T> *ptr = head; ptr != tail->next; ptr = ptr->next) os << ptr->value << std::endl; } public: //操作迭代器的方法 //返回鏈表頭部指針 iterator begin() const { return list_iterator(head); } //返回鏈表尾部指針 iterator end() const { return list_iterator(tail->next); } //其它成員函數(shù) insert/erase/emplace }; #endif //CPP_PRIMER_MY_LIST_H
test.cpp
//// Created by wengle on 2020-03-14.//#include <string>#include "my_list.h"struct student { std::string name; int age; student(std::string n, int a) : name(n), age(a) {} //重載輸出操作符 friend std::ostream &operator<<(std::ostream &os, const student &stu) { os << stu.name << " " << stu.age; return os; }};int main() { my_list<student> l; l.push_back(student("bob", 1)); //臨時量作為實參傳遞給push_back方法 l.push_back(student("allen", 2)); l.push_back(student("anna", 3)); l.print(); for (my_list<student>::iterator it = l.begin(); it != l.end(); it++) { std::cout << *it << std::endl; *it = student("wengle", 18); } return 0;}// // Created by wengle on 2020-03-14. // #include <string> #include "my_list.h" struct student { std::string name; int age; student(std::string n, int a) : name(n), age(a) {} //重載輸出操作符 friend std::ostream &operator<<(std::ostream &os, const student &stu) { os << stu.name << " " << stu.age; return os; } }; int main() { my_list<student> l; l.push_back(student("bob", 1)); //臨時量作為實參傳遞給push_back方法 l.push_back(student("allen", 2)); l.push_back(student("anna", 3)); l.print(); for (my_list<student>::iterator it = l.begin(); it != l.end(); it++) { std::cout << *it << std::endl; *it = student("wengle", 18); } return 0; }
4. 迭代器失效
// inserting into a vector #include <iostream> #include <vector> int main () { std::vector<int> myvector (3,100); std::vector<int>::iterator it; it = myvector.begin(); it = myvector.insert ( it , 200 ); myvector.insert (it,200,300); //it = myvector.insert (it,200,300); myvector.insert (it,5,500); //當(dāng)程序執(zhí)行到這里時,大概率會crash for (std::vector<int>::iterator it2=myvector.begin(); it2<myvector.end(); it2++) std::cout << ' ' << *it2; std::cout << '\n'; return 0; }
上面的代碼很好地展示了什么是迭代器失效?迭代器失效會導(dǎo)致什么樣的問題?
當(dāng)執(zhí)行完myvector.insert (it,200,300);
這條語句后,實際上myvector已經(jīng)申請了一塊新的內(nèi)存空間來存放之前已保存的數(shù)據(jù)和本次要插入的數(shù)據(jù),由于it
迭代器內(nèi)部的指針還是指向舊內(nèi)存空間的元素,一旦舊內(nèi)存空間被釋放,當(dāng)執(zhí)行myvector.insert (it,5,500);
時就會crash(PS:因為你通過iterator的指針正在操作一塊已經(jīng)被釋放的內(nèi)存,大多數(shù)情況下都會crash)。迭代器失效就是指:迭代器內(nèi)部的指針沒有及時更新,依然指向舊內(nèi)存空間的元素。
上圖展示了STL源碼中vector容器insert
方法的實現(xiàn)方式。當(dāng)插入的元素個數(shù)超過當(dāng)前容器的剩余容量時,就會導(dǎo)致迭代器失效。這也是測試代碼中myvector.insert (it,200,300);
插入200個元素的原因,為了模擬超過當(dāng)前容器剩余容量的場景,如果你的測試環(huán)境沒有crash,可以將插入元素設(shè)置的更多一些。
5. 參考資料
原文鏈接:https://blog.csdn.net/jinchengzhou/article/details/122474498
相關(guān)推薦
- 2022-02-26 slf4日志,指定位置格式輸出,保存日志
- 2022-08-14 Python全局變量關(guān)鍵字global的簡單使用_python
- 2022-04-22 VSCode中g(shù)it上傳遇到 “在簽出前,請清理存儲庫工作樹?!眴栴}解決
- 2022-05-09 Python數(shù)據(jù)結(jié)構(gòu)與算法的雙端隊列詳解_python
- 2022-10-21 使用react+redux實現(xiàn)彈出框案例_React
- 2023-04-12 Docker?login和logout的使用_docker
- 2022-04-18 python?ConfigParser庫的使用及遇到的坑_python
- 2022-09-18 Redis6?主從復(fù)制及哨兵機制的實現(xiàn)_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支