我想知道<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); }) };