Boost. Range: Try range adaptors

Source: Internet
Author: User

# Include <iostream> <br/> # include <vector> <br/> # include <boost/assign. HPP> <br/> # include <boost/range/adaptor/transformed. HPP> <br/> # include <boost/spirit/include/karma. HPP> </P> <p> using namespace STD; <br/> using namespace boost: adaptors; <br/> using namespace boost: Assign; <br/> namespace karma = boost: Spirit: karma; </P> <p> int F (int x) {return x + 1 ;} </P> <p> struct x <br/> {<br/> typedef int result_type; <br/> int operator () (int x) const {return x + 1 ;}< br/>}; </P> <p> int main () <br/>{< br/> vector <int> V; <br/> V + = 1, 2, 4, 5; </P> <p> auto v2 = v | transformed (f); <br/> cout <karma :: format (karma: auto _ % ',' <'/N', V2); </P> <p> auto V3 = v | transformed (x ()); <br/> cout <karma: Format (karma: auto _ % ',' <'/N', V3 ); </P> <p> return 0; <br/>}</P> <p> //, 6 <br/> //, 6, 6# Include <iostream> <br/> # include <vector> <br/> # include <boost/assign. HPP> <br/> # include <boost/range/adaptor/transformed. HPP> <br/> # include <boost/lambda/Lambda. HPP> <br/> # include <boost/spirit/include/Phoenix. HPP> <br/> # include <boost/spirit/include/karma. HPP> <br/> # include <boost/function_types/result_type.hpp> </P> <p> using namespace STD; <br/> using namespace boost: adaptors; <br/> using namespace boost: Assign; <br/> namespace karma = boost: Spirit: karma; </P> <p> template <typename F> <br/> struct result_type_wrapper: boost: function_types: result_type <decltype (& F: Operator ()> <br/>{< br/>}; </P> <p> template <typename F> <br/> struct adaptable_wrapper: F <br/>{< br/> typedef typename result_type_wrapper <F >:: type result_type; <br/> // typedef typename boost: function_types :: result_type <decltype (& F: Operator () >:: type result_type; </P> <p> adaptable_wrapper (f): F (f) {}< br/>}; </P> <p> template <typename F> <br/> adaptable_wrapper <F> make_adaptable (f) <br/>{< br/> return adaptable_wrapper <F> (f); <br/>}</P> <p> int main () <br/>{< br/> vector <int> V; <br/> V + = 1, 2, 4, 5; </P> <p> // auto v2 = v | transformed (boost: Phoenix: arg_names: _ 1 + 1 ); // error <br/> // auto V3 = v | transformed (boost: Lambda: _ 1 + 1 ); // error <br/> // auto V4 = v | transformed ([] (int x) {return x + 1 ;}); // error </P> <p> auto V5 = v | transformed (make_adaptable ([] (int x) {return x + 1 ;})); <br/> cout <karma: Format (karma: auto _ % ',' <'/N', V5 ); </P> <p> return 0; <br/>}</P> <p> // 2, 3, 4, 5, 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.