C++14 early adopters: Implementing LINQ in C + +

Source: Internet
Author: User

Implementing LINQ in C + +

#include <iostream> #include <vector>template<typename titerator>struct from_range{typedef TypeName Std::iterator_traits<titerator>::value_type Value_type; Titerator Current, upcoming, End;from_range (Titerator begin, Titerator end): current (begin), upcoming (begin), end (end) { }template<typename trangebuilder>decltype (Auto) operator>> (Trangebuilder builder) Const {return Builder.build (*this); }decltype (Auto) front () const {return *current;} BOOL Next () {return upcoming = = end? False: (current = upcoming++, true);}}; Template<typename Trange, TypeName tpredicate>struct Where_range{typedef TypeName Trange::value_type value_type ; Trange range; Tpredicate Predicate;where_range (Trange range, tpredicate predicate): Range (range), predicate (predicate) {}template <typename trangebuilder>decltype (Auto) operator>> (Trangebuilder builder) Const {return Builder.build (* this); }decltype (Auto) front () const {return Range.front ();} BOOL Next () {while (Range.next ()) if (predicate (Range.front ())) return True;return false;}}; Template<typename tpredicate>struct where_builder{tpredicate predicate;where_builder (TPredicate predicate): predicate (predicate) {}template<typename Trange>auto build (Trange range) {return where_range<trange, Tpredicate> (range, predicate);}; struct To_vector_builder{template<typename Trange>auto build (Trange range) {Std::vector<trange::value_type > Result;while (Range.next ()) Result.push_back (Range.front ()); return result;}}; Template<typename Tcontainer>auto from (Tcontainer const& container) {return From_range<decltype (std:: Begin (Container)) > (Std::begin (Container), Std::end (Container));} Auto To_vector () {return To_vector_builder ();} Template<typename Tpredicate>auto WHERE (tpredicate predicate) {return where_builder<tpredicate> ( predicate); }int Main () {int a[] = {1, 2, 3, 4};auto v = from (a) >> where ([] (int n) {return n% 2 = = 0;}) >> to_vector (); (int i : v) std::cout << i << Std::endl;} /*24*/


C++14 early adopters: Implementing LINQ in C + +

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.