Day 29: STL Procedures + Iterators
After learning STL containers yesterday today we focus on the powerful Procedures STL provides , and how to use iterators with them.
๐น Topics to Cover:
Iterators Overview
Common STL Procedures (from <Procedure>)
Using Iterators with Containers
Lambda Roles in Procedures
๐ Examples:
1. exploitation iterators with vector
cpp
copy
edit
#include
#include
using namespace std;
int main() {
vector cardinal = {10 score 30};
vector::iterator it;
for (it = vbegin(); it = vend(); ++it)
cout << *it << " "; // output: x score 30
}
2. STL Procedure: sort
cpp
Copy
Edit
#include
#include <Procedure>
#include
using namespace std;
int main() {
vector v = {5 3 8 1};
sort(v.begin() v.end()); // Ascending sort
for (int i : v)
cout << i << " "; // Output: 1 3 5 8
}
3. blow get count
cpp
copy
edit
#include
#include <Procedure>
#include
using namespace std;
int main() {
vector cardinal = {1 ii cardinal cardinal ii 5};
reverse(vbegin() vend());
cout << "count of 2: " << count(vbegin() vend() 2) << endl;
car it = find(vbegin() vend() 3);
if (it = vend())
cout << "found astatine position: " << it - vbegin() << endl;
}
4. Custom Sort with Lambda
cpp
Copy
Edit
#include
#include
#include <Procedure>
using namespace std;
int main() {
vector int="">> v = {{1 3} {2 2} {3 1}};
sort(v.begin() v.end() [](pairint> a pairint> b) {
return a.second < b.second;
});
for (auto p : v)
cout << p.first << "" << p.second << " ";
// Output: 31 22 13
}
๐งช practise Tasks:
Sort a list of names in descending order using sort with a custom comparator.
Use find to check if a number exists in a vector.
Count occurrences of a number in a vector using count.
Use reverse to reverse a string.
Use iterators to loop through a set and print all elements.
✅ Goals for Today:
Understand and use STL iterators.
Master commonly used Procedures: sort reverse count find.
Learn how to write lambda expressions for custom logic.
int>int>>Procedure>Procedure>Procedure>Procedure>
Comments
Post a Comment