当前位置:网站首页>STL源码剖析:迭代器的概念理解,以及代码测试。
STL源码剖析:迭代器的概念理解,以及代码测试。
2022-07-30 05:49:00 【夕阳染色的坡道】
目的:理解STL源码
迭代器的缘由来自于一种设计模式。它提供一个方法,让用户能够一一的访问里面的数据,而不用管里面的数据的组成的结构(树状,线性,数组,图等结构)。它不同于数据结构的访问样子,它统一的一一访问完整的数据。
在STL中迭代器不但能够一一访问里面的数据,它同时可以作为一个粘合剂,将数据结构和算法分开。数据结构和算法分来,不但能够易于阅读和写代码,同时符合开发中的开闭原则。如果每个数据结构用一个算法,算法可以在别处开发,然后用迭代器把数据结构和算法粘合在一起。具体测试如下:数据结构有vector, list, deque三种;算法有find一种。find算法应用于这三种数据结构中。具体代码如下:
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
const int arraysize = 7;
int ia[arraysize] = {
0,1,2,3,4,5,6};
vector<int> ivect(ia, ia+arraysize);
list<int> ilist(ia, ia+arraysize);
deque<int> ideque(ia, ia+arraysize);
vector<int>::iterator it1 = find(ivect.begin(), ivect.end(), 4);
if(it1 == ivect.end())
{
cout<<"4 not found in ivect.. "<<std::endl;
}
else
{
cout << "4 found in ivect..."<<std::endl;
}
list<int>::iterator it2 = find(ilist.begin(), ilist.end(), 5);
if(it2 == ilist.end())
{
cout << "5 not found in ilist... " << endl;
}
else
{
cout << "5 found in ilist... " << endl;
}
deque<int>::iterator it3 = find(ideque.begin(), ideque.end(), 6);
if(it3 == ideque.end())
{
cout << "6 not found in ideque... " << endl;
}
else
{
cout << "6 found in ideque... " << endl;
}
return 0;
}
代码中,搜索功能的find应用到三种数据结构中,它测试结果如下:
它能找到4,5,6。应用了搜索功能,这也是迭代器的一个非常重要的作用。
感悟:STL的设计非常精巧,迭代器也是一个非常重要的部分,对以后自己在代码上设计提供一个技巧。以后尽量将基础算法和数据结构分开,再用迭代器去实现算法和数据结构的融合。
边栏推荐
- flask项目快速搭建部署gunicorn+supervisor
- 彻底删除openstack中镜像的记录
- LVM和磁盘配额
- 用 GraphScope 像 NetworkX 一样做图分析
- Build an intelligent network security management and control system for digital government
- Devops基本概念和原理
- Ingress:从静态图分析到动态图分析
- Linux(centos7)下安装MySQL
- The concept and testing method of black box testing
- MySQL common commands and mysqldump backup
猜你喜欢

About memcache kernel, so one of the most popular

使用 Grafana 的 Redis Data Source 插件监控 Redis

DNS域名解析服务

为数字政府构建智能化网络安全管控体系

how to use xilinx's FFT ip

测试开发工程师成长日记001 - 敏捷测试、CI/CD/CT、DecOps的一些介绍

Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)

PXE高效批量网络装机

Data types of Redis6

作为测试leader,考察求职者的几个方面
随机推荐
How to import matlab data into modelsim simulation
Test Development Engineer Growth Diary 017 - The Life Cycle of a Bug
prometheus-Federation机制配置
From installation to compilation: 10 minutes to teach you to use and develop GraphScope locally
图计算101:图计算的类型、语言与系统
Unable to open socket file: target process not responding or HotSpot VM not loaded
使用 Helm 部署 GraphScope
搭建vsftpd服务并实现本地用户访问
测试开发工程师成长日记009 - 环境排排站:开发环境、测试环境、生产环境、UAT环境、仿真环境
空间顶点到直线的距离计算及其源码
从 Vertex 到 Subgraph 再到 PIE: 并行图计算编程模型概览
测开基础知识01
Graph analysis like NetworkX with GraphScope
MongoDB-CUD没有R
基于 JupyterLab 插件在 GraphScope 中交互式构图
prometheus监控mysql
向量三重积的等式推导证明
Required request body is missing 问题解决
Application of graph computing in network security analysis
瀑布流(自定义布局实现)