Difference between revisions of "CPP/C++0x/Lambda"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Lambda.cpp== <source lang="cpp"> #include <iostream> #include <unordered_set> // must use -std=c++0x flag int main(int argc, char* argv[]) { // Create a set std::unorder…')
 
 
Line 1: Line 1:
 
==Lambda.cpp==
 
==Lambda.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
 
 
#include <iostream>
 
#include <iostream>
#include <unordered_set>
+
#include <vector>
 +
#include <algorithm>
 +
#include <utility>
  
// must use -std=c++0x flag
+
void Lambda();
  
int main(int argc, char* argv[])
+
void Equivalent();
 +
 
 +
int main()
 
{
 
{
  // Create a set
 
  std::unordered_set<unsigned int> S;
 
  
  // Add 10 elements to the set
+
}
   for(unsigned int i = 0; i < 10; i++)
+
 
 +
 
 +
void Lambda()
 +
{
 +
    std::vector<std::pair<int, float> > v{
 +
        {1, 4.5}, {3, 6.7}, {9, 7.8}
 +
    };
 +
 
 +
    auto it=std::find_if(v.begin(), v.end(), [](std::pair<int, float> p) {
 +
        return p.first==3;
 +
    }); // This lambda expression (or "anonymous function") lets you "on the fly" define a function to be used.
 +
    // Here, we are comparing the .first of all of the items in the container to "3"
 +
    std::cout << it->second;
 +
}
 +
 
 +
struct Equals
 +
{
 +
   Equals(const int value)
 
   {
 
   {
     S.insert(i);
+
     CompareValue = value;
 
   }
 
   }
 
+
    
   // Output all of the elements in the set
+
   bool operator()(const std::pair<int, float>& value)
   for(auto it1 = S.cbegin(); it1 != S.cend(); it1++)
+
 
   {
 
   {
     std::cout << " " << *it1;
+
     return value.first == 3;
 
   }
 
   }
 +
 
 +
  int CompareValue;
 +
};
 +
 +
void Equivalent()
 +
{
 +
    std::vector<std::pair<int, float> > v{
 +
        {1, 4.5}, {3, 6.7}, {9, 7.8}
 +
    };
 +
 +
    // This works, but this following is shorter:
 +
//    Equals myEquals;
 +
//    myEquals.CompareValue = 3;
 +
//    auto it=std::find_if(v.begin(), v.end(), myEquals);
  
  return 0;
+
    auto it=std::find_if(v.begin(), v.end(), Equals(3));
 +
   
 +
    std::cout << it->second;
 
}
 
}
  

Latest revision as of 13:40, 13 July 2012

Lambda.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
 
void Lambda();
 
void Equivalent();
 
int main()
{
 
}
 
 
void Lambda()
{
    std::vector<std::pair<int, float> > v{
         {1, 4.5}, {3, 6.7}, {9, 7.8}
    };
 
    auto it=std::find_if(v.begin(), v.end(), [](std::pair<int, float> p) {
        return p.first==3;
    }); // This lambda expression (or "anonymous function") lets you "on the fly" define a function to be used.
    // Here, we are comparing the .first of all of the items in the container to "3"
    std::cout << it->second;
}
 
struct Equals
{
  Equals(const int value)
  {
    CompareValue = value;
  }
 
  bool operator()(const std::pair<int, float>& value)
  {
    return value.first == 3;
  }
 
  int CompareValue;
};
 
void Equivalent()
{
    std::vector<std::pair<int, float> > v{
         {1, 4.5}, {3, 6.7}, {9, 7.8}
    };
 
    // This works, but this following is shorter:
//     Equals myEquals;
//     myEquals.CompareValue = 3;
//     auto it=std::find_if(v.begin(), v.end(), myEquals);
 
    auto it=std::find_if(v.begin(), v.end(), Equals(3));
 
    std::cout << it->second;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(Lambda)
ADD_EXECUTABLE(Lambda Lambda.cpp)