-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_7.html
More file actions
84 lines (71 loc) · 3.02 KB
/
Copy pathCpp2_7.html
File metadata and controls
84 lines (71 loc) · 3.02 KB
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
<article>
<h1>C++ II: Page 7 - Modern C++: Auto & Lambdas</h1>
<section>
<h2>Writing Cleaner, Faster Code</h2>
<p>Modern C++ offers powerful features like <code>auto</code> for type deduction and <code>lambdas</code> for creating anonymous functions on-the-fly, making code significantly more expressive.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: modern_cpp.cpp
* Demonstration of auto type deduction, lambdas, and mutable lambdas
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// 1. Auto type deduction
auto x = 10; // int
auto y = 3.14; // double
auto z = "Hello"; // const char*
// 2. Lambda expressions
// [capture](params) -> return_type { body }
auto add = [](int a, int b) -> int {
return a + b;
};
cout << "Addition: " << add(5, 3) << endl;
// 3. Capturing variables
int multiplier = 10;
auto multiply = [multiplier](int x) {
return x * multiplier;
};
cout << "Multiplication: " << multiply(5) << endl;
// 4. Mutable Lambda (modifying captured value)
int count = 0;
auto increment = [count]() mutable {
return ++count;
};
cout << "Mutable lambda: " << increment() << endl;
// 5. Using lambda in algorithm
vector<int> nums = {1, 2, 3};
for_each(nums.begin(), nums.end(), [](int n) {
cout << n * 2 << " ";
});
return 0;
}
/*
* Modern C++ Nuances:
* 1. 'auto': Compiler deduces the type at compile-time, reducing verbosity.
* 2. Lambdas: Great for quick, localized logic in algorithms.
* 3. Capturing: [=] captures all by value, [&] captures all by reference.
* 4. Lambdas are effectively function objects (functors) behind the scenes.
* 5. Mutable lambdas ('mutable' keyword) allow modification of captured-by-value variables.
* 6. Use 'auto' when the type is obvious, but prefer explicit types for API interfaces.
*/
</code></pre>
<div class="interactive-sim">
<h3>Lambda Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_7');
out.innerHTML = 'auto f = [](int x){ return x*2; };<br>';
setTimeout(() => out.innerHTML += '>> f(5) is 10', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_7" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_6.html" style="text-decoration: none; color: #6c757d;">← Previous: Modern C++: Move Semantics</a>
<a href="#" data-file="Cpp2_8.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Build: CMake →</a>
</div>
</footer>
</article>