search
Homephp教程PHP开发Use of Iterator in c++ STL standard container

Most standard containers of C++ STL provide Iterator. Some containers, such as priority_queue, do not have Iterator because semantically they should not allow arbitrary traversal of elements in the container.

There is the iterator pattern among the 23 classic design patterns, and the java collection framework also implements this pattern:

Java code

package java.util;  
  
  
public interface Iterator<E> {  
  
    boolean hasNext();  
  
    E next();  
  
  
    void remove();  
}

C++’s iterator is more flexible than java, mainly reflected in:

1.java only A front-to-back iterator. In addition to front-to-back iterators, c++ also provides back-to-back iterators, such as:

Cpp code

map<int,int> amap;  
amap.insert(pair<int,int>(1,1));  
amap.insert(pair<int.int>(2,2));  
map<int,int>::iterator it;  
for(it = amap.begin();it != ampa.end();it++)//从前向后  
{  
    cout<<"key:"<<it->first<<" value:"<<it->second<<endl;

Cpp code

}

Cpp code

map<int,int>::reverse_iterator rit;  
for(rit = amap.rbegin();rit != amap.rend();rit++)//从后向前  
{  
     cout<<"key:"<<rit->first<<" value:"<<rit->second<<endl;  
}

2. In addition to iterator, c++ also provides const_iterator, which can only read the data in the collection, but cannot change its value.

3. Java’s iterator seems to only be able to increment in a single step, while c++’s iterator In addition, the iterator can also implement arithmetic operations, such as +n, -n, which is very useful for scenarios where a certain element needs to be read randomly. However, it seems that only the iterator of vector supports arithmetic operations. In other words, the iterator it in the previous example cannot perform operations such as it = it+n. This is important to remember.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.