当前位置:网站首页>STL source code analysis: conceptual understanding of iterators, and code testing.
STL source code analysis: conceptual understanding of iterators, and code testing.
2022-07-30 07:49:00 【sunset stained ramp】
目的:理解STL源码
The reason for iterators comes from a design pattern.它提供一个方法,Allow users to access the data one by one,Regardless of the structure of the data inside(树状,线性,数组,图等结构).It's different from how access to data structures looks,It uniformly accesses complete data one by one.
在STLThe iterator can not only access the data in it one by one,It also acts as an adhesive,Separate data structures and algorithms.Data structures and algorithms are divided,Not only can it be easy to read and write code,At the same time, it conforms to the open-closed principle in development.If each data structure uses an algorithm,Algorithms can be developed elsewhere,Then use iterators to glue data structures and algorithms together.具体测试如下:数据结构有vector, list, deque三种;算法有find一种.findAlgorithms are applied to these three data structures.具体代码如下:
#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;
}
代码中,搜索功能的findapplied to three data structures,Its test results are as follows:
它能找到4,5,6.Search function applied,This is also a very important role of iterators.
感悟:STL的设计非常精巧,Iterators are also a very important part,Provide a trick for designing your own code in the future.In the future, try to separate the basic algorithm from the data structure,Then use iterators to realize the fusion of algorithms and data structures.
边栏推荐
猜你喜欢
随机推荐
flask项目快速搭建部署gunicorn+supervisor
matlab机器学习_01
GAIA-IR: Parallelized Graph Query Engine on GraphScope
MongoDB-CUD没有R
如何理解普吕克坐标(几何理解)
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
Deploy GraphScope with Helm
DHCP原理与配置
Interactively compose graphs in GraphScope based on the JupyterLab plugin
MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
测试开发工程师成长日记002 - 从0开始做接口自动化
Network Protocol 03 - Routing and NAT
kubernetes搭建SonarQube进行代码扫描
Waterfall flow (custom layout implementation)
软件测试术语 - 场景测试
LVM和磁盘配额
Test and Development Engineer Growth Diary 009 - Environment Pai Pai Station: Development Environment, Test Environment, Production Environment, UAT Environment, Simulation Environment
Mastering JESD204B (1) – Debugging of AD6676
Shortcut keys commonly used in the use of Word
debian problem









