使用点表示法,orderBy和firstOrDefault打开源代码C++ LINQ库?

myW*_*SON 7 c# c++ linq visual-studio-2010

我用C#LINQ dot sintax搜索VS2010兼容的C++ linq库.意思是这样的:from(...).where(...).orderBy.firstOrDefault();

我google了,发现这样回答LINQ库收集/乱七八糟:

其他我发现不使用点符号... btw pfultz2/Linq似乎提供orderBy,并且首先它的SQL像LINQ sintax和Limitations使它成为我不想要的东西=(

那么有任何开源C++ LINQ库带点符号,orderBy和firstOrDefault吗?

Ger*_*ago 1

好吧,我不会给你你想要的答复,但无论如何它都会是一个答复:-)

LINQ 主要针对 C#。我认为您的用例应该是将 C# 代码转换为 C++,但我认为 C++ 中的有效方法是使用Boost.Range

Boost.Range 以一种易于数据查询的方式重用了 C++ 标准库:

  1. 您可以使用从左到右表示法的容器适配器operator |。它们是惰性评估的,就像在 LINQ 中一样。
  2. std::min, std::max, std::all_of, std::any_of, std::none_of您可以在适应范围内执行诸如此类的操作。

我前几天写的一个例子是如何反转字符串中的单词。解决方案是这样的:

using string_range = boost::iterator_range<std::string::const_iterator>;

struct submatch_to_string_range {
    using result_type = string_range;

    template <class T>
    string_range operator()(T const & s) const {
        return string_range(s.first, s.second);

    }
};

string sentence = "This is a sentence";

auto words_query = sentence |
                ba::tokenized(R"((\w+))") |
                ba::transformed(submatch_to_string_range{}) |
                ba::reversed;         


vector<string_range> words(words_query.begin(), words_query.end());

for (auto const & w : words) 
cout << words << endl;
Run Code Online (Sandbox Code Playgroud)

我强烈建议您以这个库为基础进行查询,因为它将在很长一段时间内得到支持,并且我认为这将是未来。您可以执行相同样式的查询。

如果这个库可以用诸如| max和之类的东西来扩展| to_vector,以避免直接命名向量和复制,那就太好了,但我认为作为一种查询语言,现在,它是可以接受的。