473,403 Members | 2,354 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,403 software developers and data experts.

boost lambda expression

hi,

can't I call member functions from an placeholder of a boost-like
lambda expression?
look on the example below please:

-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/lambda/lambda.hpp>

using namespace std;
using namespace boost::lambda;

int main(int argc, char **argv)
{
// this works:

vector<stringv1(10);
fill(v1.begin(), v1.end(), "haha");
for_each(v1.begin(), v1.end(), cout << _1 << " ");
cout << endl;

// this works, too:

vector<string*v2(10);
fill(v2.begin(), v2.end(), new string("hehe"));
for_each(v2.begin(), v2.end(), cout << *_1 << " ");
cout << endl;

// this doesn't:

vector<stringv3(10);
fill(v3.begin(), v3.end(), "hoho");
for_each(v3.begin(), v3.end(), cout << _1->c_str() << " ");
// error: base operand of '->' has non-pointer type 'const
// boost::lambda::lambda_functor<boost::lambda::place holder<1>
>'
cout << v3.begin()->c_str() << endl; // this works, just to be
sure
cout << endl;

-----------------------------------------------------------

Thanks for any idea!

Apr 25 '07 #1
3 3771
use lambda::bind

for_each(v3.begin(), v3.end(), cout << bind(&string::c_str, _1) << "
");

or specialize ostream::operator<< for string

Apr 26 '07 #2
On Apr 25, 5:08 pm, rammel <ph3...@googlemail.comwrote:
hi,

can't I call member functions from an placeholder of a boost-like
lambda expression?
look on the example below please:

-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <boost/lambda/lambda.hpp>

using namespace std;
using namespace boost::lambda;

int main(int argc, char **argv)
{
// this works:

vector<stringv1(10);
std::vector< std::string v1(10, "haha");
fill(v1.begin(), v1.end(), "haha");
for_each(v1.begin(), v1.end(), cout << _1 << " ");
cout << endl;

// this works, too:

vector<string*v2(10);
fill(v2.begin(), v2.end(), new string("hehe"));
// no its doesn't work, all the pointers are the same
// why? look at std::fill(...) or better yet - print the
pointers
for_each(v2.begin(), v2.end(), cout << *_1 << " ");
cout << endl;

// this doesn't:

vector<stringv3(10);
fill(v3.begin(), v3.end(), "hoho");
for_each(v3.begin(), v3.end(), cout << _1->c_str() << " ");
// error: base operand of '->' has non-pointer type 'const
// boost::lambda::lambda_functor<boost::lambda::place holder<1>
int main()
{
std::vector< std::string v1(10, "haha");
std::for_each(v1.begin(), v1.end(), std::cout << _1 << " ");
std::cout << std::endl;

std::vector< std::string* vp(10);
// sets the pointers in vp to the addresses of v1's objects
// caution: as long as v1 isn't resized
std::transform(v1.begin(), v1.end(), vp.begin(), &_1);
std::for_each(vp.begin(), vp.end(), std::cout << _1 << "\n");
std::cout << std::endl;
}

haha haha haha haha haha haha haha haha haha haha
0x502040
0x502048
0x502050
0x502058
0x502060
0x502068
0x502070
0x502078
0x502080
0x502088

Why not just overload a global templated op<< for a std::vector?

std::cout << v1 << std::endl;

Apr 26 '07 #3
On 26 Apr, 13:25, dasjotre <dasjo...@googlemail.comwrote:
use lambda::bind

for_each(v3.begin(), v3.end(), cout << bind(&string::c_str, _1) << "
");

or specialize ostream::operator<< for string
I meant: write ostream & operator << (ostream &, string const &)

Apr 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
by: flopbucket | last post by:
Hi, After reading a bit about boost::lambda, I became curious how they implemented it. I downloaded it and had a look, but the all the headers and multiple templates make it a bit difficult to...
7
by: Henrique A. Menarin | last post by:
I want to create a function _and so that I could write vector<boolasserts; //... bool b = _and(asserts); and it "anded" all the elements in the vector. I implemented it like this:
0
by: Barry Ding | last post by:
#include <map> #include <utility> #include <algorithm> #include <iostream> #include <list> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std;
1
by: nandor.sieben | last post by:
Let m be of type vector<vector<int. I am trying to sort each element of m: for_each(m.begin(), m.end(), bind(sort, *_1.begin(), *_1.end()); This does not work: 'const struct...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
3
by: Stuart Golodetz | last post by:
Hi, Just wondering why the following code doesn't work: #include <iostream> #include <boost/lambda/lambda.hpp> using namespace boost::lambda; int main()
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.