Well, I will not give you the reply you want, but it will be a reply anyway :-)
LINQ is thought for C# mainly. I think your use case should be for translating C# code into C++, but I think that the effective way in C++ is to use Boost.Range.
Boost.Range reuses the c++ standard library in a way that is easy to do query on data:
- You can use adaptors for containers with left to right notation using
operator |
. They are evaluated lazily, just as in LINQ.
- You can perform operations such as
std::min, std::max, std::all_of, std::any_of, std::none_of
in the adapted ranges.
An example I wrote the other day is how to reverse words in a string.
The solution was something like this:
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;
I highly recommend you to base your queries upon this library, since this is going to be supported for a very long time and I think it will be the future.
You can do same style of queries.
It would be nice if this library could be extended with things like | max
and | to_vector
to avoid naming the vector directly and copying, but I think that as a query language, right now, it's more than acceptable.