lambda and map

M

Minkoo Seo

Hi list.

I'm trying to extract values from my map. Here's the code:

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

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

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>

using namespace std;
using namespace boost::lambda;

int main()
{
map<string, int> m;
m["a"] = 1;
m["b"] = 2;

for_each(m.begin(), m.end(), cout << _1.second);

return EXIT_SUCCESS;
}


This code does not compile and says:

[mkseo@uranus tmp]$ !g
g++ lambda_test.cpp
lambda_test.cpp: In function 'int main()':
lambda_test.cpp:20: error: 'const struct
boost::lambda::lambda_functor<boost::lambda::placeholder<1> >' has no
member named 'second'

Where's my second?

Sincerely,
Minkoo Seo
 
C

Clark S. Cox III

Hi list.

I'm trying to extract values from my map. Here's the code:

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

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

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>

using namespace std;
using namespace boost::lambda;

int main()
{
map<string, int> m;
m["a"] = 1;
m["b"] = 2;

for_each(m.begin(), m.end(), cout << _1.second);

return EXIT_SUCCESS;
}


This code does not compile and says:

[mkseo@uranus tmp]$ !g
g++ lambda_test.cpp
lambda_test.cpp: In function 'int main()':
lambda_test.cpp:20: error: 'const struct
boost::lambda::lambda_functor<boost::lambda::placeholder<1> >' has no
member named 'second'

Where's my second?

Since "operator." cannot be overloaded, it can't be used to create
lambda functions. You could write a function such as

template<typename T, typename U>
U get_second(std::pair<T,U> p) { return p.second; }

and then use bind in your for_each call.
 

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

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top