- 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.
推薦閱讀
- Visual C++程序設計教程
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- UML和模式應用(原書第3版)
- Intel Galileo Essentials
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- Hands-On C++ Game Animation Programming
- Mastering Predictive Analytics with Python
- 前端HTML+CSS修煉之道(視頻同步+直播)
- Python深度學習原理、算法與案例
- Domain-Driven Design in PHP
- Python Data Science Cookbook
- 精通Spring:Java Web開發與Spring Boot高級功能
- Oracle SOA Suite 12c Administrator's Guide
- Practical Responsive Typography
- 前端Serverless:面向全棧的無服務器架構實戰