Early adopters of C ++ 14: Implementing LINQ in C ++

Source: Internet
Author: User

Early adopters of C ++ 14: Implementing LINQ in C ++

Implement LINQ in C ++

 

#include 
 
  #include 
  
   template
   
    struct from_range{typedef typename std::iterator_traits
    
     ::value_type value_type;TIterator current, upcoming, end;from_range(TIterator begin, TIterator end): current(begin), upcoming(begin), end(end) {}template
     
      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
      
       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
       
        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
        
         struct where_builder{TPredicate predicate;where_builder(TPredicate predicate) : predicate(predicate) {}template
         
          auto build(TRange range){return where_range
          
           (range, predicate);}};struct to_vector_builder{template
           
            auto build(TRange range){std::vector
            
              result;while(range.next())result.push_back(range.front());return result;}};template
             
              auto from(TContainer const& container){return from_range
              
               (std::begin(container), std::end(container));}auto to_vector() { return to_vector_builder(); }template
               
                auto where(TPredicate predicate) { return where_builder
                
                 (predicate); }int main(){int a[] = {1, 2, 3, 4};auto v = from(a) >> where([](int n){return n % 2 == 0;}) >> to_vector();for(int i : v) std::cout << i << std::endl;}/*24*/
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 


 

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.