This part of my C++ tutorial provides numerous examples of what you can do with Lambda Expressions. We’ll cover how to Sort, Filter, Sum, Edit and Generate Lists based on conditions. We’ll also perform operations on multiple lists, create Recursive Lambda Functions and cover the Ternary Operator.
Of course we will solve problems and I provide all of the code along with a transcript of the tutorial below.
If you like videos like this, consider donating $1, or simply turn off Ad Blockers. Either helps me to continue making free educational videos.
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
// ---------- C++ TUTORIAL 9 ---------- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <numeric> #include <sstream> #include <ctime> #include <cmath> std::vector<int> GenerateRandVec(int numOfNums, int min, int max); std::vector<int> GenerateFibList(int maxNums); int main() { // ----- LAMBDA EXPRESSIONS ----- std::vector<int> vecVals = GenerateRandVec(10, 1, 50); // Lambda Expressions make it easy to perform list // operations in one line of code. You designate // them with [] // Here we sort a vector std::sort(vecVals.begin(), vecVals.end(), [](int x, int y){ return x < y; }); for(auto val: vecVals) std::cout << val << "\n"; std::cout << "\n"; // copy_if works like filter does in other languages // Here we keep only even values in a new vector std::vector<int> evenVecVals; std::copy_if(vecVals.begin(), vecVals.end(), std::back_inserter(evenVecVals), [](int x){ return (x % 2) == 0; }); for(auto val: evenVecVals) std::cout << val << "\n"; // ----- SUM A LIST ----- int sum = 0; // for_each cycles through all values // [&] captures all variables used // in the body of the lambda by reference std::for_each(vecVals.begin(), vecVals.end(), [&] (int x) {sum += x; }); std::cout << "SUM : " << sum << "\n"; // ----- END SUM A LIST ----- // ----- PROBLEM DYNAMIC LIST DIVISABLE BY A VALUE ----- // You can define what value is checked for divisability // by passing the value to check in the capture list // which lies between [] // Send a value entered by the user through the capture // list and create a list based on it int divisor; std::vector<int> vecVals2; std::cout << "List of values divisable by : "; std::cin >> divisor; std::copy_if(vecVals.begin(), vecVals.end(), std::back_inserter(vecVals2), [divisor](int x){ return (x % divisor) == 0; }); for(auto val: vecVals2) std::cout << val << "\n"; // ----- END PROBLEM DYNAMIC LIST DIVISABLE BY A VALUE ----- // ----- MULTIPLY ALL VALUES BY 2 ----- std::vector<int> doubleVec; // For_each cycles through all values in the vector // and doubles them. std::for_each(vecVals.begin(), vecVals.end(), [&](int x){doubleVec.push_back(x * 2);}); for(auto val: doubleVec) std::cout << "DBL : " << val << "\n"; // ----- END MULTIPLY ALL VALUES BY 2 ----- // ----- PERFORMING OPERATIONS USING MULTIPLE VECTORS ----- // Add values in separate vectors and save them to another std::vector<int> vec1 = {1,2,3,4,5}; std::vector<int> vec2 = {1,2,3,4,5}; std::vector<int> vec3(5); transform(vec1.begin(), vec2.end(), vec2.begin(), vec3.begin(), [](int x, int y) {return x + y;}); for(auto val: vec3) std::cout << "vec3 " << val << "\n"; // ----- END PERFORMING OPERATIONS USING MULTIPLE VECTORS ----- // ----- TERNARY OPERATOR ----- // A ternary operator works like a compact if else // statement. If the condition is true the first // value is stored and otherwise the second int age = 43; bool canIVote = (age >= 18) ? true : false; // Shows bool values as true or false std::cout.setf(std::ios::boolalpha); std::cout << "Can Derek Vote : " << canIVote << "\n"; // ----- END TERNARY OPERATOR ----- // ----- RECURSIVE LAMBDA FUNCTIONS ----- // Recursive Lambda to calculate Fibonacci Numbers std::function<int(int)> Fib = [&Fib](int n) {return n < 2 ? n : Fib(n-1) + Fib(n-2);}; // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = 1 // Fib(3) = 2 // Fib(4) = 3 std::cout << "Fib 4 : " << Fib(4) << "\n"; // ----- END RECURSIVE LAMBDA FUNCTIONS ----- // ----- PROBLEM GENERATE DYNAMIC VECTOR OF FIBS ----- std::vector<int> listOfFibs = GenerateFibList(10); for(auto val: listOfFibs) std::cout << val << "\n"; // ----- END PROBLEM GENERATE DYNAMIC VECTOR OF FIBS ----- return 0; } std::vector<int> GenerateRandVec(int numOfNums, int min, int max){ std::vector<int> vecValues; srand(time(NULL)); int i = 0, randVal = 0; while(i < numOfNums){ randVal = min + std::rand() % ((max + 1) - min); vecValues.push_back(randVal); i++; } return vecValues; } // ----- PROBLEM GENERATE DYNAMIC VECTOR OF FIBS ----- std::vector<int> GenerateFibList(int maxNums){ std::vector<int> listOfFibs; int i = 0; std::function<int(int)> Fib = [&Fib](int n) {return n < 2 ? n : Fib(n-1) + Fib(n-2);}; while(i < maxNums){ listOfFibs.push_back(Fib(i)); i++; } return listOfFibs; } // ----- END PROBLEM GENERATE DYNAMIC VECTOR OF FIBS ----- |
Leave a Reply