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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!