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

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.

主站蜘蛛池模板: 绥德县| 临安市| 山东| 陆丰市| 本溪市| 凤城市| 湘乡市| 梁平县| 庆云县| 大连市| 鄂尔多斯市| 隆子县| 宜宾县| 梅州市| 蓬安县| 卫辉市| 呼图壁县| 五指山市| 巴林左旗| 田阳县| 慈溪市| 黑山县| 平凉市| 个旧市| 仪陇县| 湘潭县| 高青县| 洪洞县| 马公市| 黎川县| 布尔津县| 加查县| 曲阜市| 枞阳县| 封开县| 滦平县| 台东市| 玛沁县| 河南省| 银川市| 石城县|