官术网_书友最值得收藏!

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 7Functional Programming.

主站蜘蛛池模板: 宜兴市| 瑞安市| 平江县| 蒙城县| 天祝| 伽师县| 吉安市| 大名县| 湘潭县| 永修县| 石河子市| 工布江达县| 察哈| 波密县| 杂多县| 平安县| 宜丰县| 苗栗县| 恭城| 苏州市| 突泉县| 阿城市| 思南县| 临汾市| 鄂州市| 陵水| 仲巴县| 东城区| 华宁县| 高邑县| 隆德县| 日照市| 罗山县| 郑州市| 航空| 清镇市| 清河县| 依兰县| 福州市| 梨树县| 年辖:市辖区|