C ++中列表理解的LINQ为C ++提供了LINQ的实现。当前,它仅支持C ++范围,但确实支持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 ) ;
}上面的C ++代码将输出此(是的是上面的C ++代码):
Omelchenko score: 97
O'Donnell score: 91
Mortensen score: 94
Garcia score: 97
Beebe score: 91
使用|实现扩展。管道操作员。这使它们可以应用于任何范围,而无需介入某些特殊的基础类别。因此扩展可以这样工作:
vector< int > numbers = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
auto r = numbers
| linq::where([]( int x) { return x > 2 ; })
| linq::select([]( int x) { return x * x; });支持的扩展方法是:
该库还提供了一个range_extension类,也可以用来编写您自己的扩展名。首先,将函数定义为函数对象类,例如:
struct contains_t
{
template < class Range , class T >
bool operator ()(Range && r, T && x) const
{ return (r | linq::find (x)) != boost::end (r); };
};然后使用静态初始化初始化扩展名:
range_extension< contains_t > contains = {};然后可以像这样使用扩展名:
if (numbers | contains( 5 )) printf( " We have a 5 " );所有LINQ查询都必须从from开始。这指定了要用于lambdas的变量名称以及将应用查询的容器。另外,可以使用from语句的多个。
struct student
{
std :: string name ;
std :: vector < int > grades ;
student ( ) { }
template < class Range >
student ( std :: string name , Range && r )
: name ( name ) , grades ( boost :: begin ( r ) , boost :: end ( r ) )
{ }
} ;
std :: vector < student > students =
{
student ( "Bob" , { 90 , 100 , 75 } )
student ( "Tom" , { 92 , 81 , 70 } )
student ( "Terry" , { 105 , 98 , 94 } )
} ;
// { 90, 100, 75, 92, 81, 70, 105, 98, 94 }
auto q = LINQ ( from ( s , students ) from ( g , s . grades ) select ( g ) ) ;Where子句返回与谓词匹配的元素。它是可选的,但必须在a from之后出现,如果有的话,应在select子句之前。
vector < int > v = { 1 , 3 , 4 , 5 } ;
// { 1, 3, 5 }
auto q = LINQ ( from ( i , v ) where ( i % 2 ) ) ;选择子句将转换应用于元素。它也是可选的,但应该是最后一个子句。
std :: vector < int > v = { 1 , 2 , 4 } ;
// { 3, 6, 24 }
auto q = LINQ ( from ( x , v ) select ( x * 3 ) ) ;orderby子句允许您指定元素以订购范围。此外,还可以在选择器之前ascending或descending以指定订购方向。默认值正在上升。
struct person
{
std :: string name ;
int age ;
person ( ) { }
person ( std :: string name , int age )
: name ( name ) , age ( age )
{ }
} ;
std :: vector < person > people =
{
person ( "Tom" , 25 ) ,
person ( "Bob" , 22 ) ,
person ( "Terry" , 37 ) ,
person ( "Jerry" , 22 )
}
// { "Jerry", "Bob", "Tom", "Terry" }
auto q = LINQ ( from ( p , people ) orderby ( p . age , descending p . name ) select ( p . name ) ) ;group子句将序列的元素分组。第一个参数是键选择器,第二个参数是值选择器。
struct person
{
std :: string name ;
int age ;
person ( ) { }
person ( std :: string name , int age )
: name ( name ) , age ( age )
{ }
} ;
std :: vector < person > people =
{
person ( "Tom" , 25 ) ,
person ( "Bob" , 22 ) ,
person ( "Terry" , 37 ) ,
person ( "Jerry" , 22 )
}
auto q = LINQ ( from ( p , people ) group ( p . age , p . name ) ) ; LINQ可以轻松地使用CGET:
cget install pfultz2/Linq
这将自动安装增强依赖性。该库也可以使用CMAKE手动安装。
find_package(Linq)也可以从Cmake消费LINQ:
find_package(Linq)
target_linkq_libraries(yourLib linq::linq)
为了获得全部支持,它需要叮当声或GCC,并提升。有部分支持视觉工作室。 Visual Studio不支持default_if_empty , group_by , group_join , join , order_by , select_many和then_by扩展,并且不支持从条款中orderby , group和嵌套。也许某些视觉工作室向导可以帮助找到MSVC错误的解决方法。
使用嵌套的lambdas实现了from语句的多个。但是,由于MSVC 2010中的错误,Nested Lambas不起作用(这应该在MSVC 2012中修复,但我尚未对其进行测试)。如果有一种方法可以在C ++中实现透明标识符,则可以避免嵌套的lambdas。
另外, let和join条款尚不支持。如果没有Polymorhpic Lambdas的帮助,就无法支持into条款。
BSL-1.0