- Expert C++
- Vardan Grigoryan Shunguang Wu
- 174字
- 2021-06-24 16:33:52
Ranges
The ranges library provides a new way of working with ranges of elements. To use them, you should include the <ranges> header file. Let's look at ranges with an example. A range is a sequence of elements having a beginning and an end. It provides a begin iterator and an end sentinel. Consider the following vector of integers:
import <vector>
int main()
{
std::vector<int> elements{0, 1, 2, 3, 4, 5, 6};
}
Ranges accompanied by range adapters (the | operator) provide powerful functionality to deal with a range of elements. For example, examine the following code:
import <vector>
import <ranges>
int main()
{
std::vector<int> elements{0, 1, 2, 3, 4, 5, 6};
for (int current : elements | ranges::view::filter([](int e) { return
e % 2 == 0; }))
{
std::cout << current << " ";
}
}
In the preceding code, we filtered the range for even integers using ranges::view::filter(). Pay attention to the range adapter | applied to the elements vector. We will discuss ranges and their powerful features in Chapter 7, Functional Programming.
推薦閱讀
- Web應用系統開發實踐(C#)
- Django Design Patterns and Best Practices
- Apex Design Patterns
- Linux操作系統基礎案例教程
- ASP.NET程序設計教程
- Linux Shell核心編程指南
- Visual Studio 2015高級編程(第6版)
- PHP與MySQL權威指南
- ASP.NET 4.0 Web程序設計
- Mastering JavaScript
- HTML5移動Web開發
- 安卓工程師教你玩轉Android
- Java EE程序設計與開發實踐教程
- Python自動化運維:技術與最佳實踐
- Instant SQL Server Analysis Services 2012 Cube Security