c++ 迭代器++和+1

In this article, we’ll learn about Iterators in C++ in detail. While working with data elements, we often come across situations where we need to traverse through every element of the data structure. In order to serve the purpose, C++ provides us with Iterators.

在本文中,我们将详细了解C ++中的迭代器。 在使用数据元素时,我们经常遇到需要遍历数据结构的每个元素的情况。 为了达到目的, C ++为我们提供了Iterators

Iterators are basically objects which help programmers access the data items from the containers. It points at a particular index position(memory address) of the Standard Template Library container and enables to access the data item at that position.

迭代器基本上是可以帮助程序员访问容器中数据项的objects 。 它指向标准模板库容器的特定索引位置 (内存地址),并允许访问该位置的数据项。

Syntax:

句法:


container-name<data-type>::iterator iterator-name;

Example:

例:


list<int>::iterator A;

Thus, iterators help us move and traverse through the items of the containers.

因此,迭代器可帮助我们在容器中移动和遍历。



C ++中的迭代器类型 (Types of Iterators in C++)

At an initial stage, Iterators and Pointers may feel similar to you in terms of functionality. But by the end of this tutorial, you will get to know the differences between an Iterator and a Pointer in C++.

在初始阶段,迭代器和指针在功能上可能与您相似。 但是,在本教程结束时,您将了解C ++中的Iterator和Pointer之间的区别。

Iterators work in a different manner than pointers. The following are the type of iterators that serve different functionality altogether:

迭代器的工作方式与指针不同。 以下是完全提供不同功能的迭代器的类型:

  • Input Iterator输入迭代器
  • Output Iterator输出迭代器
  • Forward Iterator正向迭代器
  • Bi-directional Iterator双向迭代器
  • Random-Access Iterator随机访问迭代器

1.输入迭代器 (1. Input Iterator)

An Input Iterator is a basic type of iterator which serves the sole purpose of traversing the elements of a container.

输入迭代器迭代器的基本类型,其唯一目的是traversing容器的元素。

It helps us move through the elements of the container but it does not provide us with the access to modify the data values of the container.

它可以帮助我们遍历容器的元素,但不能为我们提供修改容器的数据值的访问权限

Following is the list of operators which can be used along with Input Iterators:

以下是可与输入迭代器一起使用的运算符的列表:

  • Dereference Operator (*)解引用运算符(*)
  • Increment Operator (++)增量运算符(++)
  • Equal to Operator (==)等于运算符(==)
  • Not Equal to Operator (!=)不等于运算符(!=)

Example:

例:


#include <iostream>
#include <list>
using namespace std;
int main()
{ list<int> li= { 10, 20, 42, 50, 75 }; list<int>::iterator x; cout<<"Traversing elements of the list.."<<endl;for (x = li.begin(); x != li.end(); x++) { cout << (*x) << " "; } return 0;
} 

In the above snippet of code, we have used a list as a container and declared ‘x‘ as the iterator to that list ‘li‘. The begin() and end() functions are used to point to the first and last position of the container. The iterator traverses the list in the forward (ascending) direction.

在上面的代码片段中,我们将列表用作容器,并声明“ x ”作为该列表“ li ”的迭代器。 begin()end()函数用于指向容器的第一个和最后一个位置。 迭代器以向前(升序)的方式遍历列表。

Output:

输出:


Traversing elements of the list..
10 20 42 50 75

We will get to learn more about these operations in the coming section of this article.

我们将在本文的后续部分中了解有关这些操作的更多信息。



2.输出迭代器 (2. Output Iterator)

The Output Iterators are complementary to the Input Iterators i.e. opposite to each other in terms of functionality.

输出迭代器与输入迭代complementary ,即在功能上彼此opposite

They can be used to assign and manipulate the values of the data items of the container. But, they cannot be used to access or traverse through the elements of a container.

它们可用于分配操作容器数据项的值。 但是,它们不能用于访问或遍历容器的元素

Following is the list of operators which can be used along with Output Iterators:

以下是可与输出迭代器一起使用的运算符的列表:

  • Dereference Operator (* , ->)解引用运算符(*,->)
  • Increment Operator (++)增量运算符(++)
  • Equal to Operator (==)等于运算符(==)
  • Not Equal to Operator (!=)不等于运算符(!=)

Example:

例:


#include <iostream>
#include <list>
using namespace std;
int main()
{ list<int> li= { 10, 20, 42, 50, 75 }; list<int>::iterator x; cout<<"Traversing elements of the list.."<<endl;for (x = li.begin(); x != li.end(); x++) {
//using output iterator to change or assign new values to all the
// elements of the list*x = 4;} for (x = li.begin(); x != li.end(); x++) {
//using input iterator to traverse the listcout << (*x) << " "; } return 0;
} 

As seen above, we have assigned value = 4 to all the elements of the container using output iterator.

如上所示,我们已使用输出迭代器将value = 4分配给了容器的所有元素。

Output:

输出:


Traversing elements of the list..
4 4 4 4 4

3.双向迭代器 (3. Bi-directional Iterator)

Bi-directional Iterator is quite similar to an Input Iterator. The only difference is that a bi-directional iterator can traverse the elements in both the directions i.e. forward and backward(reverse) direction.

双向迭代器与输入迭代器非常相似。 唯一的区别是,双向迭代器可以在两个方向(即forward and backward(reverse) direction.遍历元素forward and backward(reverse) direction.

Following is the list of operators which can be used along with Input Iterators:

以下是可与输入迭代器一起使用的运算符的列表:

  • Dereference Operator (* , ->)解引用运算符(*,->)
  • Increment Operator (++)增量运算符(++)
  • Equal to Operator (==)等于运算符(==)
  • Not Equal to Operator (!=)不等于运算符(!=)
  • Decrement Operator (–)减量运算符(–)

Example:

例:


#include <iostream>
#include <list>
using namespace std;
int main()
{ list<int> li= { 10, 20, 42, 50, 75 }; list<int>::iterator x; cout<<"Traversing elements of the list in forward direction.."<<endl;for (x = li.begin(); x != li.end(); x++) {
//using input iterator to traverse the listcout << (*x) << " "; } cout<<endl;cout<<"Traversing elements of the list in backward direction.."<<endl;for (x=li.end();x!=li.begin();--x) { if (x != li.end()) { cout << (*x) << " "; } } cout << (*x); return 0;
} 

Output:

输出:


Traversing elements of the list in forward direction..
10 20 42 50 75
Traversing elements of the list in backward direction..
75 50 42 20 10

4.转发迭代器 (4. Forward Iterator)

Forward Iterator can be termed as a combination of Input and Output Iterator respectively. It allows us to modify the values of the data items of the containers as well as traverse through the elements of the container.

正向迭代器可以分别称为输入和输出迭代器组合 。 它允许我们modify容器的数据项的值以及traverse容器的元素。

But, unlike Bi-directional Iterator, the Forward Iterator does not permit the traversal of elements in a reverse or backward direction.

但是,与双向迭代器不同, 前向迭代器不允许反向或向后遍历元素

Following is the list of operators which can be used along with Input Iterators:

以下是可与输入迭代器一起使用的运算符的列表:

  • Dereference Operator (* , ->)解引用运算符(*,->)
  • Increment Operator (++)增量运算符(++)
  • Equal to Operator (==)等于运算符(==)
  • Not Equal to Operator (!=)不等于运算符(!=)

Example:

例:


#include <iostream>
#include <list>
using namespace std;
int main()
{ list<int> li= { 10, 20, 42, 50, 75 }; list<int>::iterator x; cout<<"Traversing elements of the list.."<<endl;for (x = li.begin(); x != li.end(); ++x) { cout << (*x) << " "; //using input iterator to traverse the list} for (x = li.begin(); x != li.end(); x++) {
// using output iterator to change or assign new values to all the
// elements of the list*x = 4; } cout<<endl;cout<<"Traversing elements of the list after assigning a new value to the list.."<<endl;for (x = li.begin(); x != li.end(); ++x) { cout << (*x) << " "; //using input iterator to traverse the list} return 0;
} 

Output:

输出:


Traversing elements of the list..
10 20 42 50 75
Traversing elements of the list after assigning a new value to the list..
4 4 4 4 4


5.随机访问迭代器 (5. Random-Access Iterator)

Random-Access Iterators enable the users to access the data items at any random index/position in the container. Moreover, it contains all the properties of a Bi-directional container.

随机访问迭代器使用户可以在容器中的任何随机索引/位置访问数据项。 而且,它包含双向容器的所有属性。

Further, Random-Access Iterators have access to pointer subtraction and addition for the sake of random access to the data items.

此外,为了随机访问数据项,随机访问迭代器可以访问指针减法和加法



C ++中迭代器执行的操作 (Operations performed by Iterators in C++)

The following are some of the basic and most frequently used functions to manipulate data items through iterators:

以下是一些通过迭代器操作数据项的基本功能和最常用功能:

  • begin(): This method returns the starting/beginning index of the data items in the container.begin() :此方法返回容器中数据项的开始/开始索引。
  • end(): It returns the ending index of the data items in the container.end() :返回容器中数据项的结束索引。
  • prev(int): Returns a new iterator that points to the data item which is present ahead of the position mentioned in the function parameter.prev(int) :返回一个新的迭代器,该迭代器指向位于函数参数中提到的位置之前的数据项。
  • next(int): It returns a new iterator that points to the data item which is present after the position mentioned in the function parameter.next(int) :它返回一个新的迭代器,该迭代器指向函数参数中提到的位置之后的数据项。
  • inserter(container, int): This function adds/inserts a value at the specified index or position in the container.inserter(container, int) :此函数在容器中的指定索引或位置添加/插入一个值。
  • advance(int): It increments by the number specified in the parameter and points to the data element specified at that index.advance(int) :以参数中指定的数字递增,并指向该索引处指定的数据元素。

Example:

例:


#include <iostream>
#include <list>
using namespace std;
int main()
{ list<int> li= {10, 20, 42, 50, 75}; list<int>::iterator x; cout<<"Traversing elements of the list in forward direction.."<<endl;for (x = li.begin(); x != li.end(); x++) { cout << (*x) << " "; //using input iterator to traverse the list} cout<<endl;list<int>::iterator ee = li.begin(); advance(ee, 3); cout << "The element pointed by the iterator after using advance(): "; cout << *ee << " ";
cout<<endl;
list<int>::iterator A = li.begin();
list<int>::iterator B = li.end(); auto aa = next(A, 2); auto  bb = prev(B, 2); cout << "Element pointed using next() is : "; cout << *aa << " "; cout << endl; cout << "Element pointed using prev()  is : "; cout << *bb << " "; cout << endl; return 0;
} 

Output:

输出:


Traversing elements of the list in forward direction..
10 20 42 50 75
The element pointed by the iterator after using advance(): 50
Element pointed using next() is : 42
Element pointed using prev()  is : 50


C ++中迭代器的优点 (Advantages of Iterators in C++)

  • Easy for programming: With the help of iterators, we don’t need to keep an account of the varying size of the container and we can thus easily traverse through the container using begin() and end() methods.Easy for programming :借助迭代器,我们无需考虑容器大小的变化,因此可以使用begin()end()方法轻松地遍历容器。
  • High level of re-usability of code.高度re-usability of code
  • Dynamically add or remove data elements from the container.从容器Dynamically添加或删除数据元素。


C ++中的指针与迭代器 (Pointers vs Iterators in C++)

Iterator Vs Pointer迭代器与指针

结论 (Conclusion)

Thus, in this article, we have understood the need, working and benefits of Iterators in C++ STL.

因此,在本文中,我们了解了C ++ STL中Iterators的需求,工作和好处。

参考资料 (References)

Iterator in C++ – Documentation

C ++中的迭代器–文档

翻译自: https://www.journaldev.com/37011/iterators-in-c-plus-plus

c++ 迭代器++和+1

c++ 迭代器++和+1_C ++中的迭代器简介相关推荐

  1. java设计模式迭代器模式_Java中的迭代器设计模式–示例教程

    java设计模式迭代器模式 迭代器模式是一种行为模式,它用于提供遍历一组对象的标准方式. Iterator模式在Java Collection Framework中得到了广泛使用,其中Iterator ...

  2. java设计模式迭代器模式_Java中的迭代器设计模式

    java设计模式迭代器模式 Iterator design pattern in one of the behavioral pattern. Iterator pattern is used to ...

  3. python将list转换为迭代器代码_python中的迭代器附带代码示例

    迭代的概念 迭代就是执行重复的特定的任务,知道任务完成为止 相当于我们盖房子,今天添一块砖,明天加一块瓦,直到房子盖完为止.这里每天的工作就是一次迭代 (1.)可迭代对象 a.可以直接作用于for-i ...

  4. python迭代器两个基本方法可供参考_2018.8.10 python中的迭代器

    主要内容: 1.函数名的使用 2.闭包 3.迭代器 一.函数名的运用 函数名是一个变量,但他是一个特殊的变量,与括号配合可执行函数的变量. 1.函数名的内存地址 def func(): print(' ...

  5. 什么是Java中的迭代器?如何使用它

    Java中的迭代器是一种用于遍历集合(Collection)和映射(Map)的对象.它提供了一种简单的方法来访问容器中的元素,而无需了解容器的底层实现.在这篇文章中,我们将详细介绍Java中的迭代器, ...

  6. python中的迭代器,可迭代对象(详细剖析)

    文章目录 一 什么是迭代?什么是迭代器? 二 python中的迭代器 1. python中的迭代器协议 2. 迭代器的实现原理 三 python中的可迭代对象 1.可迭代对象 2.可迭代对象如何被迭代 ...

  7. dataloader 源码_pytorch :: Dataloader中的迭代器和生成器应用

    在使用pytorch训练模型,经常需要加载大量图片数据,因此pytorch提供了好用的数据加载工具Dataloader. 为了实现小批量循环读取大型数据集,在Dataloader类具体实现中,使用了迭 ...

  8. C++中的迭代器(STL迭代器)iterator

    1.Cpp中的迭代器 要访问顺序容器和关联容器中的元素,需要通过迭代器(iterator)进行.迭代器是一个变量,相当于容器和操纵容器的算法之间的中介.迭代器可以指向容器中的某个元素,通过迭代器就可以 ...

  9. Php-SPL库中的迭代器类详解(转)

    SPL提供了多个迭代器类,分别提供了迭代访问.过滤数据.缓存结果.控制分页等功能.,因为php总是在不断壮大,我尽可能列出SPL中所有的迭代类.下面其中一些迭代器类是需要php5.4,另外一些如Sea ...

最新文章

  1. 解决rtl8723be网卡故障
  2. ugui unity 取消选择_UGUI中几种不规则按钮的实现方式
  3. 【Web开发】级联查询(Ajax/ jQuery/ Servlet)
  4. 单链表插入元素 注释 c语言,数据结构之无头单链表的相关练习题——C语言实现(详细注释)...
  5. Golang笔记—封装/继承/接口
  6. your port 80 is actually used by server IIS解决办法
  7. java形状函数_java基础:10.4 Java FX之形状
  8. BlenderPython (三)bpy模块
  9. ECO生态币官网blog.sina.com.cn/ecocoin
  10. 安卓手机使用Tasker实现应用级功能,屏幕翻译v9,翻译复制贴图
  11. python的reshape(-1)和torch中的torch[-1]都是什么意思
  12. 怎么样关掉红米note开发者选项
  13. 创建PHP测试页面,连接并查询MariaDB数据库
  14. 麓言科技CAD制图技巧
  15. 地图 显示 动态轨迹_动态轨迹怎么制作?华为Watch GT2轻松搞定
  16. web学习-项目练习-No.4-朋友圈
  17. bitmap的六种压缩方式,Android图片压缩
  18. 刷脸支付应用广泛,万亿市场等你并驱争先
  19. openmeetings6.10安装配置 踩坑记录
  20. 计算机网络虚电路数据报,计算机网络——网络层-虚电路和数据报网络

热门文章

  1. NHibernate官方文档中文版——批量插入(Batch inserts)
  2. POJ 1753 Flip Game 简单BFS
  3. photoshop菜鸟实用入门(2)----常用的一些快捷操作
  4. inner/left/right inner
  5. 35岁是青春的后期你最好把下面十件事做好
  6. [转载] python中pass的使用_Python pass详细介绍及实例代码
  7. [转载] Python Pandas 转换unix时间戳
  8. 【iOS开发】使用iFrameExtractor实现视频直播
  9. LODOP打印当前日期时间的方法
  10. Java设计模式学习记录-解释器模式