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

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.

主站蜘蛛池模板: 乌什县| 宜春市| 农安县| 隆昌县| 眉山市| 孟州市| 临桂县| 宝坻区| 台江县| 乐平市| 翁牛特旗| 子长县| 景德镇市| 泸西县| 溧水县| 泸溪县| 香港 | 清苑县| 康马县| 土默特右旗| 黄陵县| 高雄县| 盘锦市| 岳阳县| 郁南县| 南雄市| 伊吾县| 行唐县| 西吉县| 县级市| 左贡县| 连平县| 龙岩市| 桦川县| 奈曼旗| 板桥市| 阳东县| 天长市| 新河县| 托里县| 吉木萨尔县|