468,512 Members | 1,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

implementation of copy_if using lambda

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<intvi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);
vi.push_back(5);

vector<intvi2;

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::place holder<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

Jul 31 '06 #1
2 3624
Minkoo Seo wrote:
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<intvi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);
vi.push_back(5);

vector<intvi2;

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::place holder<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

Jul 31 '06 #2
Hello,

Minkoo Seo wrote:
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

Aug 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ian McMeans | last post: by
53 posts views Thread by Oliver Fromme | last post: by
4 posts views Thread by Charlie Taylor | last post: by
6 posts views Thread by jena | last post: by
267 posts views Thread by Xah Lee | last post: by
5 posts views Thread by google | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.