当前位置:网站首页>Iterator function operation of iterator learning
Iterator function operation of iterator learning
2022-07-28 03:53:00 【I washed my feet in Bangong Lake】
Learning in this chapter Iterator Iterator function operation of , The specific functions are as follows :
Defined in header file | |
advance | Make the iterator advance a given distance ( Function templates ) |
distance | Returns the distance between two iterators |
| begin | Start iterator ( Function templates ) |
| end | End iterator ( Function templates ) |
next (C++11) | Make the iterator self augment ( Function templates ) |
prev (C++11) | Make the iterator subtract itself ( Function templates ) |
Code example 1
advance We know list The list can take the head element and the tail element , What if we want to take the intermediate element and operate , Because it doesn't have [i] There are no at(i) operation , So use advance Function can get any element in the middle , Specific examples are as follows :
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main()
{
list<int> mylist{10,20,30,40,50,60,70,80,90,100};
for(auto li = mylist.begin(); li != mylist.end(); ++li)
{
cout << *li << "\t";
}
cout << "\n";
// Take the first place 5 Gewusu
list<int>::iterator it = mylist.begin();
std::advance(it, 4);
cout << "The five element in mylist is: " << *it << endl;
list<int>::iterator it1 = mylist.begin();
std::advance(it1, 6);
cout << "The seven element in mylist is: " << *it1 << endl;
*it1 = 90;
for(auto li = mylist.begin(); li != mylist.end(); ++li)
{
cout << *li << "\t";
}
cout << "\n";
cout << "Hello World!" << endl;
return 0;
}
Code example 2 distance Take the distance between two iterators
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
list<int> mylist{10,20,30,40,50,60,70,80,90,100};
std::list<int>::iterator first = mylist.begin();
std::list<int>::iterator last = mylist.end();
std::cout << "list distance(first, last) = " << std::distance(first, last) << '\n';
//std::cout << "list distance(last, first) = " << std::distance(last, first) << '\n'; //list Out of commission
std::vector<int> vec{32, 15, 43, 25, 36};
std::vector<int>::iterator vfirst = vec.begin();
std::vector<int>::iterator vlast = vec.end();
std::cout << "vector distance(first, last) = " << std::distance(vfirst, vlast) << '\n';
std::cout << "vector distance(last, first) = " << std::distance(vlast, vfirst) << '\n';
cout << "\n";
cout << "Hello World!" << endl;
return 0;
}

Code example 3 begin Start iterator
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
//3.begin
std::vector<int> vec{32, 15, 43, 25, 36};
std::vector<int>::iterator iter2 = vec.begin();
// take vector The first 2 Elements
++iter2;
std::cout << "vector iter2 = " << *iter2 << '\n';
cout << "Hello World!" << endl;
return 0;
}

Code example 4 end End iterator
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
std::vector<int> vec{32, 15, 43, 25, 36};
// take vector The last element
std::vector<int>::iterator iter = vec.begin();
while(iter != vec.end())
{
++iter;
if(iter == vec.end())
{
// Take the last element
--iter;
std::cout << "vector iter = " << *iter << '\n';
break;
}
}
cout << "\n";
cout << "Hello World!" << endl;
return 0;
}

边栏推荐
- LabVIEW loads and uses custom symbols in tree control projects
- Appnium--APP自动化测试工具
- Leetcode58. 最后一个单词的长度
- pip-script.py‘ is not present Verifying transaction: failed
- Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving
- MySQL Basics (create, manage, add, delete, and modify tables)
- Crowdfunding platform system based on JSP & Servlet
- Redis cluster
- 【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
- input 上传文件并回显 FileReader并限制选择文件时的类型
猜你喜欢

4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits

数据挖掘-01

Dynamic planning - 62. Different paths

Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know

Data rich Computing: m.2 meets AI at the edge

Prefix-Tuning: Optimizing Continuous Prompts for Generation

How to solve MySQL deep paging problem

静态博客搭建工具汇总

It's amazing. This article completely explains the service interface testing

Xctf attack and defense world web master advanced area php2
随机推荐
Collection | 0 basic open source data visualization platform flyfish large screen development guide
Dynamic planning - 1049. Weight of the last stone II
Input upload file and echo FileReader and restrict the type of file selection
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Day08 redis的基础知识
TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
Skillfully use stack backtracking to help you quickly locate problems
[openvx] VX for basic use of objects_ convolution
Basic knowledge of day08 redis
常用的接口测试工具
8000 word explanation of OBSA principle and application practice
Web Security Foundation - Command Execution Vulnerability
常用的弱网测试工具
TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
[openvx] VX for basic use of objects_ image
【OPENVX】对象基本使用之vx_distribution
CH340 RTS DTR引脚编程驱动OLED
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
MangoPapa 的实用小脚本(目录篇)