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

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.

主站蜘蛛池模板: 大足县| 琼海市| 霍山县| 汽车| 佛坪县| 卓资县| 隆昌县| 双江| 鲁山县| 苍山县| 南部县| 和平县| 双牌县| 玉田县| 大城县| 蒙阴县| 蒙城县| 宝坻区| 新余市| 吴江市| 克什克腾旗| 罗江县| 汤阴县| 塘沽区| 越西县| 广德县| 寿阳县| 梁河县| 随州市| 天气| 钦州市| 惠水县| 绥化市| 城市| 巴东县| 镇康县| 阳曲县| 南皮县| 河曲县| 抚宁县| 夏河县|