在C ++中执行C#LINQ Where/Select的正确方法是什么? [英] What is the proper way to do a C# LINQ Where/Select in C++?

查看:610
本文介绍了在C ++中执行C#LINQ Where/Select的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,如果我有一个对象列表(例如List myObjectList),则可以通过以下方式获取该列表的子集:

In C#, if I have a List of objects (e.g. List myObjectList), I can get a subset of that list via:

anotherMyObjectList = myObjectList.Where(x => x.isSomething()).Select(x => x).ToList();

假设我不想使用第三方C ++ LINQ库(仅标准库,也许使用boost),那么在C ++中实现此目的的最佳方法是什么?为我要在其中执行此操作的每个实例编写一个函数将很容易,但是最好知道存在哪种框架来执行这种类型的操作.

Assuming I don't want to use a 3rd party C++ LINQ library (only standard library and maybe boost), what's the best way to do this in C++? It would be easy to write a function for each instance where I want to do this, but it would be better to know what framework exists to perform this type of operation.

如果答案在C ++ 98,C ++ 0x或C ++ 11中不同,那么最好知道这些区别.

If the answer is different in C++98, C++0x or C++11, it would be good to know the differences.

推荐答案

在C ++ 11中,使用boost可以执行以下操作:

In C++11, using boost you can do something like this:

// assumming myObjectList is a vector of someObj type
std::vector<someObj> myObjectList = { ... };
auto result = myObjectList | boost::adaptors::filtered([](const someObj& x) { return x.isSomething(); });
std::vector<someObj> anotherMyObjectList(boost::begin(result), boost::end(result));

这篇关于在C ++中执行C#LINQ Where/Select的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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