Home > Backend Development > C++ > Tips on using iterators in C++

Tips on using iterators in C++

王林
Release: 2023-08-22 17:18:24
Original
1327 people have browsed it

Tips on using iterators in C++

C is a powerful programming language with many advanced features, such as iterators, which allow programmers to use data structures in the standard library more efficiently. This article will introduce the use of iterators so that you can better utilize the C standard library.

What is an iterator?

Iterator (iterator) is an important concept in C. It is a data access tool used to traverse elements in a container. It provides a universal way to access various containers, including vector , list, map, etc.

Iterators have the following types:

  1. Forward iterator (forward iterator): The container can only be traversed forward, and each element can be accessed once and only once.
  2. Bidirectional iterator (bidirectional iterator): You can traverse the container forward and backward. Each element is accessed once and only once.
  3. Random-access iterator (random-access iterator): It can perform arithmetic operations like a pointer, move freely in the container, and the access address is more flexible.

How to use iterator?

The following will introduce how to use iterators.

  1. Container traversal

You can use iterators to traverse the elements in the container. The code is as follows:

std::vector<int> v{1, 2, 3, 4, 5};
for (auto it = v.begin(); it != v.end(); ++it) {
  std::cout << *it << " ";
}
Copy after login

In the above code, the vector container is used The begin() and end() methods in get the start and end positions of the iterator, and then use a for loop to traverse the entire container.

  1. Insert/Delete Elements

Use iterators to insert or delete elements in the container. The code is as follows:

std::vector<int> v{1, 2, 3, 4, 5};
for (auto it = v.begin(); it != v.end(); ++it) {
  if (*it == 3) {
    // 插入元素
    v.insert(it, 6);
    break;
  }
}
for (auto it = v.begin(); it != v.end(); ++it) {
  if (*it == 4) {
    // 删除元素
    v.erase(it);
    break;
  }
}
for (auto i : v) {
  std::cout << i << " ";
}
Copy after login

In the above code, use The insert() and erase() methods in the vector container are used to specify the position of the element to be inserted or deleted through the iterator.

  1. Traversal of multiple containers

Using iterators can also traverse multiple containers and operate on them. The code is as follows:

std::vector<int> v1{1, 2, 3};
std::vector<int> v2{4, 5, 6};
std::vector<int> v3{7, 8, 9};
// 构造多容器迭代器
auto it1 = v1.begin();
auto it2 = v2.begin();
auto it3 = v3.begin();
for (; it1 != v1.end() && it2 != v2.end() && it3 != v3.end(); ++it1, ++it2, ++it3) {
  std::cout << *it1 << " " << *it2 << " " << *it3 << std::endl;
}
Copy after login

The above In the code, multiple vector containers are used, traversing them through iterators, and printing their element values.

Summary

Iterator is a powerful data access tool in C. It can be used to traverse elements in a container, insert/delete elements, and access multiple containers and operate on them. Mastering the use of iterators can make programmers more proficient in using the C standard library and improve code execution and coding efficiency.

The above is the detailed content of Tips on using iterators in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template