Reputation: 837
struct Data
{
int Id;
std::vector<int> SomeData
}
std::vector<Data> dataList = ....
int wantedIndex = 11;
std::find(dataList.begin(), dataList.end(), "where dataList.Id == wantedIndex")
Is this syntax possible in standard C++ in some way in Visual Studio 2008?
Upvotes: 0
Views: 730
Reputation: 38126
Well if you have visual studio 2008 then you cannot use lambda function i guess The proper way to do is following:
int wanted_index = 11;
struct Data
{
int Id;
std::vector<int> SomeData
}
bool find_fun(int i)
{
return i == wanted_index;
}
//std::vector<Data> dataList = ....
std::find(dataList.begin(), dataList.end(), find_fun);
You can also use boost bind or std::tr1 bind to have lambda function like syntax. As it appears from the msdn visual studio 2008 documentation that you can use std::bind() also see: http://msdn.microsoft.com/en-us/library/bb982702(v=vs.90).aspx
so you can use :
bool find_fun1(int i, int wanted_index)
{
return i == wanted_index
}
int wanted_index = 11;
std::find_if(dataList.begin(), dataList.end(), std::bind(find_fun1,_1,wanted_index));
Upvotes: 1
Reputation: 14119
If you cannot use C++11. Maybe you can use boost phoenix.
It would be like this:
#include <boost\phoenix\phoenix.hpp>
...
std::find_if(dataList.begin(), dataList.end(),
boost::phoenix::bind(&Data::Id, boost::phoenix::placeholders::arg1) == boost::phoenix::val(wantedIndex)));
Although I think in your very simple case bind from boost will also do it.
std::find_if(dataList.begin(), dataList.end(), boost::bind(&Data::Id, _1) == wantedIndex);
Upvotes: 2
Reputation: 59811
C++11 comes with support for lambdas:
std::find_if(dataList.begin(), dataList.end(),
[&wantedIndex](const Data& x) { return x.Id == wantedIndex;});
Upvotes: 1