是否有一个完整的等效项来查看成员函数/变量? [英] Is there and neat equivalent to view a member function/variable?

查看:68
本文介绍了是否有一个完整的等效项来查看成员函数/变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

流库具有简洁的map 函数以通过成员函数查看范围. Range-V3 中是否有任何等效视图?

Streams library has a neat map function to view a range by a member function. Is there any equivalent view in Range-V3?

view :: transform 是唯一的选择吗?

推荐答案

文章示例:

std::vector widgets = /* ... */    
std::set ids = stream::MakeStream::from(widgets)
         .map(&Widget::getId)
         .to_set();

范围-v3中的

(忽略 std :: vector std :: set 缺少的模板参数)将是:

(ignoring the missing template arguments for std::vector and std::set) in ranges-v3 would be:

std::vector<Widget> widgets = // ...
std::set<Widget::ID> ids = widgets | ranges::view::transform(&Widget::getId);

是的, transform 等同于Streams中的 map .

Yes, transform is the equivalent to map in Streams.

range-v3中的所有算法都接受

All the algorithms in range-v3 accept Invokable Projections that allow enable the algorithm to select range elements on the basis of a transformation, but still operate on the entire element. For example, we can sort the Widgets by their IDs:

widgets |= ranges::action::sort(std::greater<Widget::ID>{}, &Widget::getId);

这篇关于是否有一个完整的等效项来查看成员函数/变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆