只包含C++頭文件Ranges和LINQ模板庫:boolinq

jopen 10年前發布 | 17K 次閱讀 boolinq C/C++開發

這是一個只包含C++ 頭文件Ranges和LINQ模板庫。支持STL/Qt集合。

Example with integers

int src[] = {1,2,3,4,5,6,7,8};
auto dst = from(src).where( [](int a){return a%2 == 1;})    // 1,3,5,7
                    .select([](int a){return a*2;})         // 2,6,10,14
                    .where( [](int a){return a>2 && a<12;}) // 6,10
                    .toVector();

// dst type: std::vector<int>
// dst items: 6,10

Example with structs

struct Man
{
    std::string name;
    int age;
};

Man src[] =
{
    {"Kevin",14},
    {"Anton",18},
    {"Agata",17},
    {"Terra",20},
    {"Layer",15},
};

auto dst = from(src).where(  [](const Man & man){return man.age < 18;})
                    .orderBy([](const Man & man){return man.age;})
                    .select( [](const Man & man){return man.name;})
                    .toVector();

// dst type: std::vector<std::string>
// dst items: "Kevin", "Layer", "Agata"

Interesting example

struct Message
{
    std::string PhoneA;
    std::string PhoneB;
    std::string Text;
};

Message messages[] =
{
    {"Anton","Troll","Hello, friend!"},
    {"Denis","Wride","OLOLO"},
    {"Anton","Papay","WTF?"},
    {"Denis","Maloy","How r u?"},
    {"Denis","Wride","Param-pareram!"},
};

int DenisUniqueContactCount =
    from(messages).where(   [](const Message & msg){return msg.PhoneA == "Denis";})
                  .distinct([](const Message & msg){return msg.PhoneB;})
                  .count();

// DenisUniqueContactCount == 2    

容器的支持

  • C++: Native arrays, pairs of pointers
  • STL: list, stack, queue, vector, deque, set, map, any compatible ....
  • Qt: QList, QVector, QSet, QMap.

項目主頁:http://www.baiduhome.net/lib/view/home/1392543545303

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!