LINQ สำหรับความเข้าใจในรายการใน C ++ ให้การใช้งาน LINQ สำหรับ C ++ ปัจจุบันรองรับช่วง 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 สิ่งนี้ระบุชื่อตัวแปรที่จะใช้สำหรับแลมบ์ดาและคอนเทนเนอร์ที่จะใช้การสืบค้น นอกจากนี้ยังสามารถใช้หลายข้อความ 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 ) ) ; ประโยคที่ส่งคืนองค์ประกอบที่ตรงกับเพรดิเคต มันเป็นทางเลือก แต่ต้องมาจากข้อ 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) ยังสามารถใช้ LINQ จาก CMake:
find_package(Linq)
target_linkq_libraries(yourLib linq::linq)
สำหรับการสนับสนุนเต็มรูปแบบนั้นต้องใช้ Clang หรือ GCC และเพิ่ม มีการสนับสนุนบางส่วนสำหรับ Visual Studio Visual Studio ไม่รองรับ default_if_empty , group_by , group_join , join , order_by , select_many , และส่วนขยาย then_by และมันไม่สนับสนุน orderby group และซ้อนจากข้อ บางทีพ่อมด Visual Studio บางตัวอาจช่วยค้นหาวิธีแก้ปัญหาสำหรับแมลง MSVC
หลายข้อความ from ข้อความจะถูกนำมาใช้โดยใช้แลมบ์ดาซ้อนกัน อย่างไรก็ตามเนื่องจากข้อผิดพลาดใน MSVC 2010 Lambas ซ้อนกันไม่ทำงาน (ควรได้รับการแก้ไขใน MSVC 2012 แต่ฉันยังไม่ได้ทดสอบ) หากมีวิธีการใช้ตัวระบุโปร่งใสใน C ++ สามารถหลีกเลี่ยงแลมบ์ดาที่ซ้อนกันได้
นอกจากนี้ยังไม่ได้รับการสนับสนุน let และ join ไม่สามารถรองรับประโยค into ได้หากไม่ได้รับความช่วยเหลือจาก Polymorhpic Lambdas
BSL-1.0