Well, I won’t give you the answer you want, but still the answer will be :-)
LINQ is considered the main thing for C #. I think your use case should be for translating C # code into C ++, but I believe that using Boost.Range is an effective way in C ++.
Boost.Range reuses the standard C ++ library, which easily performs a query on the data:
- You can use adapters for containers with left and right symbols using
operator | . They are evaluated lazily, as in LINQ. - You can perform operations such as
std::min, std::max, std::all_of, std::any_of, std::none_of in adapted ranges.
An example that I wrote the other day is how to cancel 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 that you base your queries on this library, as it will be supported for a very long time, and I think it will be the future. You can do the same style of queries.
It would be nice if this library could be expanded with things like | max | max and | to_vector | to_vector , to avoid overlaying the vector directly and copying, but I believe that now as a query language this is more than acceptable.
Germán Diago
source share