473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3981
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ian McMeans | last post by:
I was bitten by a bug today that depended on how lambda works. It took me quite a while to realize what was going on. First, I made multiple lambda functions inside a loop, each of which...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
4
by: Charlie Taylor | last post by:
I find that I use lambda functions mainly for callbacks to things like integration or root finding routines as follows. flow = integrate(lambda x: 2.0*pi * d(x)* v(x) * sin(a(x)),xBeg, xEnd) ...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
11
by: ssecorp | last post by:
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.