In C#, if I have a List of objects (e.g. List myObjectList), I can get a subset of that list via:
anotherMyObjectList = myObjectList.Where(x => x.isSomething()).Select(x => x).ToList();
Assuming I don't want to use a 3rd party C++ LINQ library (only standard library and maybe boost), what's the best way to do this in C++? It would be easy to write a function for each instance where I want to do this, but it would be better to know what framework exists to perform this type of operation.
If the answer is different in C++98, C++0x or C++11, it would be good to know the differences.
In C++11, using boost you can do something like this:
// assumming myObjectList is a vector of someObj type
std::vector<someObj> myObjectList = { ... };
auto result = myObjectList | boost::adaptors::filtered([](const someObj& x) { return x.isSomething(); });
std::vector<someObj> anotherMyObjectList(boost::begin(result), boost::end(result));
You can use "ccplinq" :
using namespace cpplinq;
int ints[] = {3,1,4,1,5,9,2,6,5,4};
auto result = from_array (ints)
>> where ([](int i) {return i/2 == 0;})
>> select ([](int i) {std::stringstream s; s << i; return s.str();})
>> to_list ();
You can use std::copy_if()
to create a subset of a container:
#include <algorithm>
#include <iterator>
#include <list>
std::list<object> myObjectList, anotherMyObjectList;
// fill myObjectList somehow
std::copy_if(cbegin(myObjectList),
cend(myObjectList),
std::back_inserter(anotherMyObjectList),
[](const object& o) { return o.IsSomething(); });
or if you're using C++98/03:
#include <algorithm>
#include <iterator>
#include <list>
std::list<object> myObjectList, anotherMyObjectList;
// fill myObjectList somehow
struct is_something {
bool operator()(const object&) {
return object.IsSomething();
}
};
std::copy_if(myObjectList.cbegin()
myObjectList.cend(),
std::back_inserter(anotherMyObjectList),
is_something());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With