implementation of copy_if using lambda

M

Minkoo Seo

Hi list.

I'm trying to implment copy_if using boost::lambda:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/format.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/algorithm.hpp>

using namespace std;
using namespace boost::lambda;


int main()
{
vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);
vi.push_back(5);

vector<int> vi2;

for_each(vi.begin(), vi.end(),
if_then(_1 == 1, vi2.push_back(_1)));

return EXIT_SUCCESS;
}

This fails with the errors:

lambda_test.cpp: In function 'int main()':
lambda_test.cpp:30: error: no matching function for call to
'std::vector<int, std::allocator<int> >::push_back(const
boost::lambda::lambda_functor<boost::lambda::placeholder<1> >&)'
/usr/lib/gcc/x86_64-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:602:
note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const
_Tp&) [with _Tp = int, _Alloc = std::allocator<int>]

Does anybody know how to push _1 into vi2? I've tried bind( ), but no
luck.

Sincerely,
Minkoo Seo
 
M

mlimber

Minkoo said:
Hi list.

I'm trying to implment copy_if using boost::lambda:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/format.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/algorithm.hpp>

using namespace std;
using namespace boost::lambda;


int main()
{
vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);
vi.push_back(5);

vector<int> vi2;

for_each(vi.begin(), vi.end(),
if_then(_1 == 1, vi2.push_back(_1)));

return EXIT_SUCCESS;
}

This fails with the errors:

lambda_test.cpp: In function 'int main()':
lambda_test.cpp:30: error: no matching function for call to
'std::vector<int, std::allocator<int> >::push_back(const
boost::lambda::lambda_functor<boost::lambda::placeholder<1> >&)'
/usr/lib/gcc/x86_64-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:602:
note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const
_Tp&) [with _Tp = int, _Alloc = std::allocator<int>]

Does anybody know how to push _1 into vi2? I've tried bind( ), but no
luck.

Try asking on the Boost User list:

news://news.gmane.org/gmane.comp.lib.boost.user

Cheers! --M
 
B

Bernd Strieder

Hello,

Minkoo said:
for_each(vi.begin(), vi.end(),
if_then(_1 == 1, vi2.push_back(_1)));

This should read


for_each(vi.begin(), vi.end(),
if_then(_1 == 1,
bind(&vector<int>::push_back, var(vi2), _1)));

See the Boost docs for the details. Calling member functions with bind,
und using var are explained. The error messages when e.g. omitting the
var are horrible, over 7,5KB in 10 lines.

I think in this case one could do better using a back_insert_iterator.
Strictly speaking you don't implement copy_if, but something remotely
similar.

template <class II, class OI, class COND>
OI my_copy_if(II b1, II e1, OI b2, COND cond)
{
for_each(b1, e1,
if_then(cond(_1), (var(b2) = _1, ++b2)));
return b2;
}

....

my_copy_if(vi.begin(), vi.end(), back_inserter(vi2), _1 < 3);


Bernd Strieder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top