lambda and map

Discussion in 'C++' started by Minkoo Seo, Jul 31, 2006.

  1. Minkoo Seo

    Minkoo Seo Guest

    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
     
    Minkoo Seo, Jul 31, 2006
    #1
    1. Advertising

  2. On 2006-07-31 04:07:01 -0400, "Minkoo Seo" <> said:

    > 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.
     
    Clark S. Cox III, Jul 31, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    13
    Views:
    627
    Bengt Richter
    Jan 7, 2005
  2. Tom Anderson
    Replies:
    186
    Views:
    2,680
    Terry Reedy
    Jul 9, 2005
  3. Jp Calderone
    Replies:
    1
    Views:
    292
    Scott David Daniels
    Jul 3, 2005
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    202
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    261
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page