1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-01-17 22:28:32 +01:00

Add Reverse iterator (C++) (#4374)

This commit is contained in:
Mikhail Ostashchenko 2023-08-22 13:18:19 +02:00 committed by GitHub
parent 76c2686269
commit f2b29f80f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,19 @@ while (itr != nums.end()) {
}
```
**Reverse Iterator**: Similar to input iterators but can be used for multiple passes over the elements in a container. They cannot move forward.
Example:
```cpp
std::list<int> nums = {1, 2, 3, 4};
std::list<int>::reverse_iterator itr = nums.rbegin();
while (itr != nums.rend()) {
std::cout << *itr << " ";
++itr;
}
```
- **Bidirectional Iterator**: These iterators offer the ability to move both forward and backward in a container. List and set containers have bi-directional iterators.
Example:
@ -78,4 +91,4 @@ for (auto itr = nums.begin(); itr != nums.end(); ++itr) {
}
```
When working with algorithms, remember that the C++ Standard Library provides various algorithms that already utilize iterators for tasks like searching, sorting, and manipulating elements.
When working with algorithms, remember that the C++ Standard Library provides various algorithms that already utilize iterators for tasks like searching, sorting, and manipulating elements.