473,226 Members | 1,535 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,226 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 3971
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.