C++ with LINQ

by May 18, 2018

I found an interesting C++ libraly.That is "LINQ for C++".

What's "LINQ"?

LINQ (Integrated Language Query) is a function of C#. LINQ issues a query to the container and gets the result.

For details, please refer to MSDN.

https://docs.microsoft.com/en-us/dotnet/csharp/linq/

LINQ for C++

"LINQ for C++" is the libraly enable to use LINQ on C++.

https://archive.codeplex.com/?p=cpplinq

Usage is simple. Just include the header file published on GitHub.

https://github.com/mrange/cpplinq

Using "LINQ for C ++" in C++ Builder, first make sure to use the new Embarcadero C++ compiler with project options.

#pragma hdrstop
#pragma argsused

#ifdef _WIN32
#include <tchar.h>
#else
  typedef char _TCHAR;
  #define _tmain main
#endif

#include <stdio.h>
#include <iostream>
#include "CppLinq/cpplinq.hpp"    

int computes_a_sum ()
{
  using namespace cpplinq;
  int ints[] = {3,1,4,1,5,9,2,6,5,4};

  return
      from_array (ints)
    >>  where ([](int i) {return i%2 ==0;})     // extract odd number
    >>  sum ()                                  // Summing extracted numbers
    ;
}


int _tmain(int argc, _TCHAR* argv[])
{
  std::cout << computes_a_sum() << std::endl;

  return 0;
}

The sum of the even numbers extracted from the array was output.

Many libraries can be used by using the new Embarcadero C++ compiler. please try it