Home > Backend Development > C++ > How to iterate over a C++ STL container?

How to iterate over a C++ STL container?

WBOY
Release: 2024-06-05 18:29:01
Original
427 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 the begin() and end() functions to obtain the iterator range:

#include <vector>

int main() {
  std::vector<int> v = {1, 2, 3, 4, 5};

  // 使用基于范围的 for 循环
  for (int num : v) {
    std::cout << num << " ";
  }

  std::cout << std::endl;

  // 使用迭代器
  for (std::vector<int>::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 the front() and# of the linked list ##back() function and the next() member function of the linked list:

#include <list>

int main() {
  std::list<int> l = {1, 2, 3, 4, 5};

  // 使用基于范围的 for 循环
  for (int num : l) {
    std::cout << num << " ";
  }

  std::cout << std::endl;

  // 使用迭代器
  std::list<int>::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() and end() functions to get an iterator of key-value pairs:

#include <map>

int main() {
  std::map<std::string, int> 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<std::string, int>::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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template