-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_4.html
More file actions
78 lines (66 loc) · 3.12 KB
/
Copy pathCpp2_4.html
File metadata and controls
78 lines (66 loc) · 3.12 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
<article>
<h1>C++ II: Page 4 - STL: Algorithms</h1>
<section>
<h2>Generic Algorithms</h2>
<p>The STL algorithms library provides a collection of efficient, generic functions to perform common operations on sequences of elements, such as searching, sorting, and transforming.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: algorithms.cpp
* Demonstration of STL algorithms: sorting, searching, transforming, and numeric ops
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric> // For accumulate
using namespace std;
int main() {
vector<int> nums = {5, 2, 8, 1, 9, 2};
// 1. Sort (using default operator<)
sort(nums.begin(), nums.end());
cout << "Sorted: ";
for (int n : nums) cout << n << " ";
cout << endl;
// 2. Find
auto it = find(nums.begin(), nums.end(), 8);
if (it != nums.end()) {
cout << "Found 8 at index: " << distance(nums.begin(), it) << endl;
}
// 3. Transform (doubling elements)
transform(nums.begin(), nums.end(), nums.begin(), [](int n) { return n * 2; });
// 4. Numeric operations (accumulate)
int sum = accumulate(nums.begin(), nums.end(), 0);
cout << "Sum after doubling: " << sum << endl;
// 5. Predicate algorithms
bool allEven = all_of(nums.begin(), nums.end(), [](int n) { return n % 2 == 0; });
bool anyNegative = any_of(nums.begin(), nums.end(), [](int n) { return n < 0; });
cout << "All even: " << (allEven ? "Yes" : "No") << endl;
cout << "Any negative: " << (anyNegative ? "Yes" : "No") << endl;
return 0;
}
/*
* Algorithm Nuances:
* 1. Algorithms operate on iterators (e.g., begin(), end()).
* 2. They are generally more efficient and safer than manual loops.
* 3. Most algorithms expect a range [first, last), where 'last' is excluded.
* 4. Custom predicates (like lambdas) can be passed to customize behavior.
* 5. STL algorithms are highly optimized, often leveraging compiler intrinsics.
* 6. 'accumulate' and other numeric algorithms live in <numeric>.
*/
</code></pre>
<div class="interactive-sim">
<h3>Algorithm Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_4');
out.innerHTML = 'nums = {5, 2, 8};<br>';
setTimeout(() => out.innerHTML += '>> sort(nums.begin(), nums.end());<br>>> Sorted: {2, 5, 8}', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_4" 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_3.html" style="text-decoration: none; color: #6c757d;">← Previous: STL: Iterators</a>
<a href="#" data-file="Cpp2_5.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Modern C++: Smart Pointers →</a>
</div>
</footer>
</article>