LINQ like query in c++
A more lazy way of selecting objects from a container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <deque> | |
template <class T> | |
class table | |
{ | |
public: | |
table<T> select (const auto& where) | |
{ | |
table results; | |
for(auto t : rows) if (where(t)) results.insert(t); | |
return results; | |
} | |
void insert(const T& _record) {rows.push_back(_record);} | |
std::deque<T> rows; | |
}; | |
#define where(x,w) [](auto& x) { return w; } | |
int main() | |
{ | |
struct record | |
{ | |
int id; | |
float value; | |
}; | |
table<record> records; | |
records.insert({1,1.123}); | |
records.insert({2,2.234}); | |
records.insert({3,3.345}); | |
auto results = records.select( where(r, r.id==2) ); | |
return results.rows[0].id; | |
} |