A recursive lambda expression - C++ STL

C++ examples for STL:Lambda

Description

A recursive lambda expression

Demo Code

#include <iostream>
#include <functional>

int main()/*from  ww  w  .ja  va  2s . c o  m*/
{
  std::function<int(int)> reverseNumber {[&reverseNumber](int no) -> int
  {
        static int r {};
        if (0 == no)
              return r;
        else
              r = r * 10 + no % 10;
        return reverseNumber(no / 10);
  }};
  int nn {12345}, r {};
  std::cout << nn << "  " << reverseNumber(nn) << std::endl;
}

Result


Related Tutorials