是否有用于 C++ 的 LINQ 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/232222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a LINQ library for C++?
提问by Robert Gould
Are there any Platform agnostic (not CLI) movements to get LINQ going for C++ in some fashion?
是否有任何平台不可知(不是 CLI)运动以某种方式让 LINQ 用于 C++?
I mean a great part of server frameworks around the world run on flavors of UNIX and having access to LINQ for C++ on UNIX would probably make lots of people happy!
我的意思是世界上很大一部分服务器框架都运行在 UNIX 版本上,并且在 UNIX 上访问 LINQ for C++ 可能会让很多人感到高兴!
回答by js.
回答by k06a
This is my solution of template C++ LINQ library.
Source code is here: Boolinq
There are a lot of tests on each feature.
I'm working on it right now.
这是我的模板 C++ LINQ 库的解决方案。
源代码在这里:Boolinq
每个功能都有很多测试。
我现在正在努力。
Any comments?
May be advices?
任何意见?
可能是建议?
UPDATE: project moved to https://github.com/k06a/boolinqand now have version 2.0 with only 700 lines of source code :)
更新:项目移至https://github.com/k06a/boolinq,现在只有 700 行源代码的 2.0 版:)
回答by David Cuccia
Microsoft has just announcedthat they've built LINQ for C and C++. Not yet available, though.
微软刚刚宣布他们已经为 C 和 C++ 构建了 LINQ。不过目前还没有。
Update 11/06/2012:
2012 年 6 月 11 日更新:
Microsoft Open Technologies, Inc. has now releasedand open-sourced(Apache License 2.0) a number of related libraries, including a LINQ implementation (Ix++), and it's new Reactive Extensions (Rx++) library.
Microsoft Open Technologies, Inc. 现已发布并开源(Apache 许可证 2.0)许多相关库,包括 LINQ 实现 (Ix++) 及其新的 Reactive Extensions (Rx++) 库。
回答by yufanyufan
http://cpplinq.codeplex.com/is a very good implementation.
From the author:
The motivation for CppLinq is that both boolinq and Native-RX seems to be based around the operator"." to compose list functions. The problem is that the "." operator is that it can't be overloaded in C++ which makes it hard to extend these libraries with functions of my own design. To me this is important. CppLinq is based around operator>> which is overloadable thus CppLinq can be made extensible.
http://cpplinq.codeplex.com/是一个非常好的实现。
来自作者:
CppLinq 的动机是 boolinq 和 Native-RX 似乎都是基于“操作符”。组成列表功能。问题在于“。” 运算符是它不能在 C++ 中重载,这使得很难用我自己设计的函数扩展这些库。对我来说这很重要。CppLinq 基于可重载的 operator>>,因此 CppLinq 可以扩展。
回答by Paolo Severini
I have written a small library cppLinqthat reimplements IEnumerable<> and its LINQ operators. It is just an experiment; for now it only works on Windows (coroutines are implemented with Win32 fibers), and only builds with the Dev Preview of VS11 (it makes an heavy usage of lambda expressions :-)).
我编写了一个小型库cppLinq,它重新实现了 IEnumerable<> 及其 LINQ 运算符。这只是一个实验;目前它只适用于 Windows(协程是用 Win32 光纤实现的),并且只使用 VS11 的开发预览版(它大量使用 lambda 表达式:-))。
It allows to write code like this:
它允许编写这样的代码:
auto source = IEnumerable<int>::Range(0, 10);
auto it = source->Where([](int val) { return ((val % 2) == 0); })
->Select<double>([](int val) -> double { return (val * val); }));
foreach<double>(it, [](double& val){
printf("%.2f\n", val);
});
回答by zwvista
You may take a look at PSade.Oven, a strongly boostified library working on STL ranges and providing a lot LINQ like functions.
您可以查看PSade.Oven,这是一个在 STL 范围内工作并提供很多类似 LINQ 的功能的强大增强的库。
回答by Paul Fultz II
Actually if you just want to use Linq for list comprehension, you can use this Linqlibrary. It requires C++11(it will work in MSVC 2010 though) and Boost. With the library you can write linq queries like this:
实际上,如果您只是想使用 Linq 进行列表理解,则可以使用这个Linq库。它需要 C++11(尽管它可以在 MSVC 2010 中工作)和 Boost。使用该库,您可以编写如下 linq 查询:
struct student_t
{
std::string last_name;
std::vector<int> scores;
};
std::vector<student_t> students =
{
{"Omelchenko", {97, 72, 81, 60}},
{"O'Donnell", {75, 84, 91, 39}},
{"Mortensen", {88, 94, 65, 85}},
{"Garcia", {97, 89, 85, 82}},
{"Beebe", {35, 72, 91, 70}}
};
auto scores = LINQ(from(student, students)
from(score, student.scores)
where(score > 90)
select(std::make_pair(student.last_name, score)));
for (auto x : scores)
{
printf("%s score: %i\n", x.first.c_str(), x.second);
}
Which will output:
这将输出:
Omelchenko score: 97
O'Donnell score: 91
Mortensen score: 94
Garcia score: 97
Beebe score: 91
回答by ronag
Here is another alternativethat is simply a wrapper around boost and stl algorithms, and thus you get most of the performance benefits of those implementations.
这是另一种替代方案,它只是 boost 和 stl 算法的包装器,因此您可以获得这些实现的大部分性能优势。
It works like this:
它是这样工作的:
std::vector<int> xs;
auto count = from(xs)
.select([](int x){return x*x;})
.where([](int x){return x > 16;})
.count();
auto xs2 = from(xs)
.select([](int x){return x*x;})
.to<std::vector<int>>();
Note that some methods return a proxy for empty ranges, e.g.
请注意,某些方法返回空范围的代理,例如
std::vector<int> xs;
auto max = from(xs)
.select([](int x){return x*x;})
.where([](int x){return x > 16;})
.max()
.value_or(default_max_value);
Feedback is welcome.
欢迎反馈。
回答by Scan
Here is my implemention of c++-linq with c++11(in chinese):
这是我用 c++11(中文)实现的 c++-linq:
http://www.cnblogs.com/cbscan/archive/2012/10/20/2732773.html
http://www.cnblogs.com/cbscan/archive/2012/10/20/2732773.html
It support features like "deferred query","stack based"(use operator new as little as possible),"copy semantic"(so you can iterate a query multitime after backup it), and so on.
它支持诸如“延迟查询”、“基于堆栈”(尽可能少地使用运算符 new)、“复制语义”(因此您可以在备份后多次迭代查询)等功能。
It also support dozens of function including "from, select, where, cast ,range, all, any ,cast, average ,contain, count ,first, last, head, tail ,groupBy ,takeUntil, skipUntil ,max, min ,reduce ,unique, sort, random ,intersect, _union".
它还支持数十种功能,包括“from、select、where、cast、range、all、any、cast、average、contain、count、first、last、head、tail、groupBy、takeUntil、skipUntil、max、min、reduce、唯一,排序,随机,相交,_union”。
I think my code is simple enough to understand and extend by anybody selves.
我认为我的代码足够简单,任何人都可以理解和扩展。