2

我想知道<ranges>在 C++20 中是否最终可以从序列中选择并set在一行中初始化 a ,就像在 C# 中使用IEnumerable. 这可能需要将<ranges>对象转换为std::initializer_list.

C#:

int[] sequence = new int[] { 0,1,2,3,4 };
HashSet<int> set = new HashSet<int>(sequence.Where((int i) => i % 2 == 0));

C++:

std::vector<int> sequence { 0,1,2,3,4 };
auto matcher = sequence | std::ranges::views::filter([](int i) { return !(i % 2); });
std::set<int> myset(matcher.begin(), matcher.end());

我想做类似的事情:

std::vector<int> sequence { 0,1,2,3,4 };
std::set<int> myset { sequence | std::ranges::views::filter([](int i) { return !(i % 2); }) };
4

1 回答 1

3

实际上,目前还不可能,但在不久的将来,我们可以获得具有此类功能的range::to 。

于 2021-03-21T11:29:56.720 回答