How to iterate over a C++ STL container?

WBOY
Release: 2024-06-05 18:29:01
Original
336 people have browsed it

To traverse an STL container, you can use the container's begin() and end() functions to obtain the iterator range: Vector: Use a for loop to traverse the iterator range. Linked list: Use the next() member function to traverse the elements of the linked list. Mapping: Get an iterator of key-value pairs and use a for loop to traverse.

如何遍历C++ STL容器?

How to traverse C++ STL containers

Traversing C++ Standard Template Library (STL) containers is essential in programmers’ daily work a task. STL provides a series of predefined data structures such as vectors, linked lists, and maps, each with its own traversal methods.

Traverse STL vectors

To traverse a vector, we can use thebegin()andend()functions to obtain the iterator range:

#include  int main() { std::vector v = {1, 2, 3, 4, 5}; // 使用基于范围的 for 循环 for (int num : v) { std::cout << num << " "; } std::cout << std::endl; // 使用迭代器 for (std::vector::iterator it = v.begin(); it != v.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; }
Copy after login

Output:

1 2 3 4 5 1 2 3 4 5
Copy after login
Copy after login

Traversing STL linked list

To traverse a linked list, we can use thefront()and# of the linked list ##back()function and thenext()member function of the linked list:

#include  int main() { std::list l = {1, 2, 3, 4, 5}; // 使用基于范围的 for 循环 for (int num : l) { std::cout << num << " "; } std::cout << std::endl; // 使用迭代器 std::list::iterator it = l.begin(); while (it != l.end()) { std::cout << *it << " "; it = it->next(); } std::cout << std::endl; return 0; }
Copy after login

Output:

1 2 3 4 5 1 2 3 4 5
Copy after login
Copy after login

Traverse the STL mapping

To iterate over a map, we can use the map's

begin()andend()functions to get an iterator of key-value pairs:

#include  int main() { std::map m = {{"Apple", 1}, {"Banana", 2}, {"Cherry", 3}}; // 使用基于范围的 for 循环 for (auto const& [key, value] : m) { std::cout << key << ": " << value << std::endl; } std::cout << std::endl; // 使用迭代器 for (std::map::iterator it = m.begin(); it != m.end(); ++it) { std::cout << it->first << ": " << it->second << std::endl; } return 0; }
Copy after login

Output:

Apple: 1 Banana: 2 Cherry: 3 Apple: 1 Banana: 2 Cherry: 3
Copy after login

The above is the detailed content of How to iterate over a C++ STL container?. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!