- 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.
推薦閱讀
- Puppet 4 Essentials(Second Edition)
- LaTeX Cookbook
- Python從菜鳥(niǎo)到高手(第2版)
- OpenCV for Secret Agents
- jQuery從入門(mén)到精通 (軟件開(kāi)發(fā)視頻大講堂)
- 差分進(jìn)化算法及其高維多目標(biāo)優(yōu)化應(yīng)用
- R的極客理想:工具篇
- Learning Zurb Foundation
- Getting Started with React Native
- 小程序,巧應(yīng)用:微信小程序開(kāi)發(fā)實(shí)戰(zhàn)(第2版)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- PHP 7從零基礎(chǔ)到項(xiàng)目實(shí)戰(zhàn)
- Java 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- Spring Boot從入門(mén)到實(shí)戰(zhàn)
- Oracle SOA Suite 12c Administrator's Guide