2022-06-14 23:33:35 +08:00
# cpp_Range_based_for_loop
#### 2022-06-11
2022-09-03 15:41:36 +08:00
---
2022-06-14 23:33:35 +08:00
##### Data structures:
2022-09-03 15:41:36 +08:00
#DS #array #vector #multiset
2022-06-14 23:33:35 +08:00
##### Difficulty:
2022-09-03 15:41:36 +08:00
2022-09-06 20:22:48 +08:00
#CS_analysis #difficulty_easy
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Related problems:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
- [cppreference ](https://en.cppreference.com/w/cpp/language/range-for )
2022-09-03 15:41:36 +08:00
---
2022-06-14 23:33:35 +08:00
### What is cpp_Range_based_for_loop?
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
Executes a for loop over a range.
Used as a more readable equivalent to the traditional [for loop ](https://en.cppreference.com/w/cpp/language/for "cpp/language/for" ) operating over a range of values, such as all elements in a container.
### Example
```cpp
#include <iostream>
#include <vector>
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
int main()
{
std::vector< int > v = {0, 1, 2, 3, 4, 5};
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (const int& i : v) // access by const reference
std::cout < < i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (auto i : v) // access by value, the type of i is int
std::cout < < i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (auto& & i : v) // access by forwarding reference, the type of i is int&
std::cout < < i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
const auto& cv = v;
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (auto& & i : cv) // access by f-d reference, the type of i is const int&
std::cout < < i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (int n : {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list
std::cout < < n < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
int a[] = {0, 1, 2, 3, 4, 5};
for (int n : a) // the initializer may be an array
std::cout < < n < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
for ([[maybe_unused]] int n : a)
2022-06-14 23:33:35 +08:00
std::cout < < 1 < < ' ' ; / / the loop variable need not be used
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (auto n = v.size(); auto i : v) // the init-statement (C++20)
std::cout < < --n + i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
for (typedef decltype(v)::value_type elem_t; elem_t i : v)
// typedef declaration as init-statement (C++20)
std::cout < < i < < ' ' ;
std::cout << '\n';
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
#if __cplusplus > 23'00 + 200000
for (using elem_t = decltype(v)::value_type; elem_t i : v)
// alias declaration as init-statement, same as above (C++23)
std::cout < < i < < ' ' ;
std::cout << '\n';
#endif
}
```
Possible output:
2022-09-03 15:41:36 +08:00
2022-06-14 23:33:35 +08:00
```
0 1 2 3 4 5
2022-09-03 15:41:36 +08:00
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
1 1 1 1 1 1
5 5 5 5 5 5
0 1 2 3 4 5
0 1 2 3 4 5
```