Mastering STL

Table of contents
- Introduction of STL
- Array โThe Organized Squad of Elements ๐๏ธ
- Vector โ The Magical Expanding Wallet ๐ณ
- List - The Train of Numbers ๐
- Queue - The Line at the Ticket Counter ๐๏ธ
- Stack โ The Pancake Pile ๐ฅ
- Map Mayhem: Choosing the Right Partner for Your Data Journey ๐
- Set It Right: Your Unique Squad of STL Containers ๐ฏ
- Bitset: The Superpower of Compact Data Handling ๐ฆธโโ๏ธ
- STL Algorithms: The Magic Toolbox of C++ ๐งฐ
Introduction of STL
What is STL in C++? ๐ค
The Standard Template Library (STL) is like the IKEA of C++ programming ๐ ๏ธโit gives you all the pieces you need to build complex programs without starting from scratch. Think of it as a magic toolbox where you can pull out ready-made parts for data structures (like vectors and maps) and algorithms (like sorting and searching). You donโt need to reinvent the wheel every timeโjust pick the right tool, plug it in, and youโre good to go. For example, if youโre trying to organize your playlist ๐ต, std::sort()
will shuffle things into perfect order without you lifting a finger!
STL also works like an all-you-can-eat buffet ๐ฝ๏ธโit doesnโt matter if youโre working with integers, strings, or custom objects, thereโs something for everyone. Want to store your collection of Pokรฉmon cards? Use a std::set
to make sure there are no duplicates. Need to search for a name in a long list of people? std::binary_search()
is like Ctrl+F but on steroids ๐ช. STL combines efficiency, flexibility, and simplicity, making coding feel less like work and more like a cheat code for solving problems.
Why Use STL in C++? ๐
The Standard Template Library (STL) is like your personal coding superhero ๐ฆธโโ๏ธโalways ready to save the day! Instead of sweating over writing tedious sorting or searching functions, just call in STLโs pre-built tools like std::sort()
or std::binary_search()
and watch the magic happen. Itโs like ordering fast food ๐ instead of cooking a 5-course mealโit saves time and works flawlessly every time! Plus, STL is battle-tested, so you can trust it to handle your data structures like stacks, queues, or maps without breaking a sweat.
What makes STL even cooler is its ability to work with any data type, thanks to templates. Need to store your gaming scores? Use a std::vector
. Want to map student names to their marks? Try std::map
. Navigating your data with STL iterators is like flipping through a Netflix menu ๐ฅโeasy and efficient! And if youโre picky, STL lets you customize how things work, like sorting tasks by deadlines using custom comparators. In short, STL doesnโt just make coding easierโitโs the ultimate life hack for every C++ programmer. Who wouldnโt want a Swiss Army knife for their code? ๐ ๏ธ
Array โThe Organized Squad of Elements ๐๏ธ
Think of std::array
like a pizza box with fixed slices ๐โthe size is constant, and each slice (element) is neatly organized!
- You can declare it, initialize it, and access its contents with ease, but no sneaking in extra slices. Here's how:
#include <iostream>
#include <array>
int main() {
// Declaration and Initialization ๐
std::array<int, 5> pizzaSlices = {10, 20, 30, 40, 50};
// Accessing Elements ๐ด
std::cout << "First slice has " << pizzaSlices[0] << " calories.\n"; // Output: 10
std::cout << "Last slice has " << pizzaSlices[4] << " calories.\n"; // Output: 50
// Using `.at()` for safe access
std::cout << "Middle slice has " << pizzaSlices.at(2) << " calories.\n"; // Output: 30
return 0;
}
- predictable and structured. Let's see how it works with some cool (and relatable) examples:
#include <iostream>
#include <array>
#include <algorithm> // for sort
#include <numeric> // for accumulate
#include <iterator> // for std::find
int main() {
// 1. Create an array of exam scores ๐
std::array<int, 5> scores = {85, 90, 78, 92, 88};
// Empty - Is the pizza box empty? ๐โ
std::cout << "Empty? " << (scores.empty() ? "Yes" : "No") << "\n"; // Output: No
// Size - How many slices in the box? ๐๐ฆ
std::cout << "Size: " << scores.size() << "\n"; // Output: 5
// Fill - Everyone gets the same score (tough teacher!) ๐ฉโ๐ซ๐
scores.fill(100);
std::cout << "After fill: ";
for (int score : scores) std::cout << score << " "; // Output: 100 100 100 100 100
std::cout << "\n";
// Sort - Sort exam scores in ascending order ๐
scores = {85, 90, 78, 92, 88};
std::sort(scores.begin(), scores.end());
std::cout << "Sorted: ";
for (int score : scores) std::cout << score << " "; // Output: 78 85 88 90 92
std::cout << "\n";
// Find - Where is the 88 hiding? ๐
auto it = std::find(scores.begin(), scores.end(), 88);
std::cout << "88 found at index: " << (it - scores.begin()) << "\n"; // Output: 2
// Accumulate - What's the total score? ๐งฎ
int total = std::accumulate(scores.begin(), scores.end(), 0);
std::cout << "Total score: " << total << "\n"; // Output: 433
return 0;
}
Vector โ The Magical Expanding Wallet ๐ณ
Imagine std::vector
as your stretchy backpack ๐งณโit grows as you add more items! Unlike fixed-size containers, itโs perfect for carrying dynamic loads, but be careful not to overpack (you still need logic)! Letโs look at how it works:
#include <iostream>
#include <vector>
int main() {
// Declaration and Initialization ๐ฆ
std::vector<int> nums = {100, 200, 300, 400, 500};
// Accessing Elements ๐ฏ
std::cout << "First item: " << nums[0] << "\n"; // Output: 100
std::cout << "Last item: " << nums.back() << "\n"; // Output: 500
std::cout << "Third item: " << nums.at(2) << "\n"; // Output: 300
// Adding a new item ๐ผ
nums.push_back(600);
std::cout << "New last item: " << nums.back() << "\n"; // Output: 600
return 0;
}
"A vector
is like your wallet ๐ชชโyou always manage to fit in one more card, but make sure not to lose track of whatโs already inside!" ๐
Imagine a vector as your wallet that magically grows to hold all your cash ๐ต. You can easily add money, check the amount, update it, and even spend it (remove). Plus, it tells you how much space it has left for more! Here's a crash course:
#include <iostream>
#include <vector>
int main() {
// Create an empty vector (your wallet ๐ณ)
std::vector<int> wallet;
// Add elements (earn some cash ๐ต)
wallet.push_back(100);
wallet.push_back(200);
wallet.push_back(500);
// Access elements (count your money ๐ฐ)
std::cout << "First note: " << wallet.front() << "\n"; // Output: 100
std::cout << "Last note: " << wallet.back() << "\n"; // Output: 500
// Change elements (exchange 500 for 50s ๐ฑ)
wallet[2] = 50;
// Remove elements (spend your money ๐ธ)
wallet.pop_back(); // Removes the last element (50)
// Check size and capacity ๐
std::cout << "Wallet size: " << wallet.size() << "\n"; // Output: 2
std::cout << "Wallet capacity: " << wallet.capacity() << "\n"; // Capacity might be >= 3
// Iterators to see what's inside ๐
std::cout << "Wallet contents: ";
for (auto it = wallet.begin(); it != wallet.end(); ++it)
std::cout << *it << " "; // Output: 100 200
std::cout << "\n";
// Check if wallet is empty ๐
std::cout << "Wallet empty? " << wallet.empty() << "\n"; // Output: 0 (false)
// Clear all money (oh no! ๐ญ)
wallet.clear();
std::cout << "Wallet size after clear: " << wallet.size() << "\n"; // Output: 0
std::cout << "Wallet empty? " << wallet.empty() << "\n"; // Output: 1 (true)
return 0;
}
List - The Train of Numbers ๐
Think of std::list
as a train where each coach (element) is connected to the next, but you can freely add or remove coaches anywhere! ๐ Itโs great for when you need dynamic flexibility in organizing your data. Here's a quick tour:
#include <iostream>
#include <list>
int main() {
// Declaration and Initialization ๐
std::list<int> train = {10, 20, 30, 40, 50}; // A train with 5 coaches
// Accessing elements (Front and Back ๐๐)
std::cout << "First coach: " << train.front() << "\n"; // Output: 10
std::cout << "Last coach: " << train.back() << "\n"; // Output: 50
// Adding coaches (Add elements ๐ ๏ธ)
train.push_front(5); // Add at the front
train.push_back(60); // Add at the back
// Removing coaches (Remove elements ๐ ๏ธ)
train.pop_front(); // Remove the first coach
train.pop_back(); // Remove the last coach
// Displaying the train coaches ๐
std::cout << "Train coaches: ";
for (int coach : train)
std::cout << coach << " "; // Output: 10 20 30 40 50
std::cout << "\n";
return 0;
}
Function | Description |
reverse() | Reverses the order of the elements |
sort() | Sorts the list elements in a particular order. |
unique() | Removes consecutive duplicate elements. |
size() | Returns the number of elements in the list. |
empty() | Checks whether the list is empty. |
clear() | Clears all the values from the list |
merge() | Merges two sorted lists. |
Queue - The Line at the Ticket Counter ๐๏ธ
A std::queue
is like a ticket queue at a movie theater ๐ฅโfirst person in, first person out (FIFO). People (elements) line up at the back and leave from the front, no cutting allowed! Here's how it works:
#include <iostream>
#include <queue>
int main() {
// Declaration and Initialization ๐๏ธ
std::queue<int> ticketQueue;
// Adding people to the queue (enqueue ๐ ๏ธ)
ticketQueue.push(101); // Person with ticket 101
ticketQueue.push(102); // Person with ticket 102
ticketQueue.push(103); // Person with ticket 103
// Accessing front and back of the queue (Front and Back ๐ช)
std::cout << "First in line: " << ticketQueue.front() << "\n"; // Output: 101
std::cout << "Last in line: " << ticketQueue.back() << "\n"; // Output: 103
// Removing from the queue (dequeue ๐ถโโ๏ธ)
ticketQueue.pop(); // Person with ticket 101 leaves the queue
// Check the new first in line
std::cout << "New first in line: " << ticketQueue.front() << "\n"; // Output: 102
// Checking the size of the queue
std::cout << "People still in queue: " << ticketQueue.size() << "\n"; // Output: 2
return 0;
}
With std::queue
, keep the line moving smoothlyโfirst come, first served! No sneaky line-jumpers allowed! ๐๐ฅ.
Function | Description |
push() | Inserts an element at the back of the queue. |
pop() | Removes an element from the front of the queue. |
front() | Returns the first element of the queue. |
back() | Returns the last element of the queue. |
size() | Returns the number of elements in the queue. |
empty() | Returns true if the queue is empty. |
Stack โ The Pancake Pile ๐ฅ
A std::stack
is like a stack of pancakesโlast pancake added is the first one you eat (LIFO: Last In, First Out). You keep stacking them up and munching from the top. Here's how to whip up your own stack:
#include <iostream>
#include <stack>
int main() {
// Declaration and Initialization ๐ฅ
std::stack<int> pancakeStack;
// Adding pancakes to the stack (push ๐ณ)
pancakeStack.push(1); // First pancake (bottom-most)
pancakeStack.push(2); // Second pancake
pancakeStack.push(3); // Third pancake (top-most)
// Accessing the top pancake ๐ฝ๏ธ
std::cout << "Top pancake: " << pancakeStack.top() << "\n"; // Output: 3
// Eating the top pancake (pop ๐ฅข)
pancakeStack.pop(); // Removes pancake 3
// Accessing the new top pancake
std::cout << "New top pancake: " << pancakeStack.top() << "\n"; // Output: 2
// Checking the number of pancakes left
std::cout << "Pancakes left: " << pancakeStack.size() << "\n"; // Output: 2
// Checking if the stack is empty ๐ฝ๏ธ
std::cout << "Is the pancake stack empty? " << (pancakeStack.empty() ? "Yes" : "No") << "\n"; // Output: No
return 0;
}
With std::stack
, keep stacking and snackingโjust donโt let them topple over! ๐โจ
Operation | Description |
push() | adds an element into the stack |
pop() | removes an element from the stack |
top() | returns the element at the top of the stack |
size() | returns the number of elements in the stack |
empty() | returns true if the stack is empty |
Map Mayhem: Choosing the Right Partner for Your Data Journey ๐
Map: The Strict and Orderly Librarian ๐
A map
is like a librarian who insists on keeping books in alphabetical order. Itโs perfect for tasks where order matters, but donโt expect her to rush!
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> studentMarks = {{"Alice", 85}, {"Bob", 92}, {"Charlie", 78}};
// Accessing
cout << "Alice's marks: " << studentMarks["Alice"] << endl;
return 0;
}
Multimap: The Party Host with Duplicate Keys ๐
A multimap is like a party host who allows multiple guests with the same name. Itโs great for handling duplicates but can get messy!
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<string, int> movieRatings = {{"Inception", 9}, {"Interstellar", 10}, {"Inception", 8}};
// Accessing
for (auto &pair : movieRatings) {
cout << pair.first << " rated: " << pair.second << endl;
}
return 0;
}
//output :-
//Inception rated: 9
//Inception rated: 8
//Interstellar rated: 10
Unordered_map: The Quick and Chaotic Friend โก
An unordered_map is like your friend who throws things in a box. No order, but super quick to find stuff (most of the time)!
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, string> countryCodes = {{"India", "IN"}, {"United States", "US"}, {"Germany", "DE"}};
// Accessing
cout << "Country code of India: " << countryCodes["India"] << endl;
return 0;
}
//Output:
//Country code of India: IN
When to Use What?
map
: When order matters ๐งโโ๏ธ. (Think: Keeping sorted records.) O(log n)multimap
: When you expect duplicates ๐คน. (Think: Movies with multiple reviews.) O(log n)unordered_map
: When speed is king ๐๏ธ. (Think: Quickly finding country codes.)Average: O(1), Worst: O(n)
Method | Description |
insert() | adds an element (key-value pair) to the map |
erase() | removes an element or range of elements from the map |
clear() | removes all the elements from the map |
find() | searches the map for the given key |
size() | returns the number of elements in the map |
empty() | returns true if the map is empty |
count() | returns total number of occurrences of the specified key #multimap |
at() | returns the element at the specified key #unordered_map |
Pro Tip: If you care about efficiency, unordered_map
can be your BFF unless you need order or duplicates! ๐
Set It Right: Your Unique Squad of STL Containers ๐ฏ
Set: The No-Nonsense Gatekeeper ๐ช
A set is like a bouncer at a partyโno duplicates allowed, and everyone is sorted before they enter!
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> numbers = {5, 2, 8, 2, 1};
// Accessing
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
1 2 5 8
(Duplicates removed, sorted order)
Multiset: The Chill Collector ๐ธ
A multiset is like your grandmaโs cookie jarโduplicates are welcome, and everything is neatly arranged.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> numbers = {5, 2, 8, 2, 1};
// Accessing
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output:
1 2 2 5 8
(Duplicates retained, sorted order)
Unordered_set: The Fast and Free-Spirited ๐ข
An unordered_set is like a free spiritโno order, but everyoneโs unique. Perfect for speed!
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<int> numbers = {5, 2, 8, 2, 1};
// Accessing
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output (example):
8 1 2 5
(Order is unpredictable, no duplicates)
Unordered_multiset: The Unpredictable Hoarder ๐
An unordered_multiset
is like that friend who keeps everythingโduplicates includedโbut doesnโt care about tidiness.
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> numbers = {5, 2, 8, 2, 1};
// Accessing
for (int num : numbers) {
cout << num << " ";
}
return 0;
}
Output (example):
5 2 2 8 1
(Duplicates retained, no specific order)
When to Use What?
set
: When order and uniqueness are key ๐งโโ๏ธ. (Think: Unique usernames.)multiset
: When duplicates matter, but order is nice ๐. (Think: Word frequency in a document.)unordered_set
: When speed is all you care about โก. (Think: Quickly finding unique items.)unordered_multiset
: When you need duplicates, fast ๐. (Think: Counting items without worrying about order.)
Operation | Description |
insert() | Insert elements into a set. set multiset |
erase() | Delete elements from a set.set multiset |
clear() | Remove all the elements from a set.set multiset |
empty() | Check if the set is empty.set multiset |
size() | Returns the size of the set.set multiset |
found() | Find the occurrence of a value. unordered_multiset |
count() | Count the frequency of a value. unordered_set |
find() | returns the iterator to the element with the specified value unordered_set |
Pro Tip: unordered_set
and unordered_multiset
are fast but trade memory for speed, while set
and multiset
are memory-friendly but slower for large data! ๐ง
Bitset: The Superpower of Compact Data Handling ๐ฆธโโ๏ธ
Bitset: The Compact Genius ๐ฏ
A bitset
is like a high-tech switchboard operatorโit handles multiple on/off switches (bits) efficiently while saving space. Perfect for memory-conscious situations like flags in games! ๐ฎ
#include <iostream>
#include <bitset>
using namespace std;
int main() {
// Declaration & Initialization
bitset<8> lights("10101010"); // 8 lights, alternating ON (1) and OFF (0)
// Accessing
cout << "Initial state of lights: " << lights << endl;
cout << "Is light 3 ON? " << lights[2] << endl; // Access 3rd bit (index starts at 0)
// Modifying
lights.flip(2); // Toggle the 3rd light
cout << "After flipping light 3: " << lights << endl;
return 0;
}
output
Initial state of lights: 10101010
Is light 3 ON? 1
After flipping light 3: 10101110
Pro Tip: Use bitset
for tasks like permissions, binary operations, or optimizing boolean flags!
Basic Operations
Function | Purpose | Example |
size() | Returns the number of bits in the bitset . | cout << b.size(); |
count() | Counts the number of 1 s (set bits). | cout << b.count(); |
any() | Checks if any bit is set to 1 . | cout << b.any(); |
none() | Checks if all bits are 0 . | cout << b.none(); |
all() | Checks if all bits are 1 . | cout << b.all(); |
Access and Modification
Function | Purpose | Example |
b[i] | Accesses the bit at position i . | cout << b[2]; |
set(pos) | Sets the bit at position pos to 1 . | b.set(3); |
reset(pos) | Resets (sets to 0 ) the bit at position pos . | b.reset(3); |
flip(pos) | Toggles (inverts) the bit at position pos . | b.flip(3); |
set() | Sets all bits to 1 . | b.set(); |
reset() | Resets all bits to 0 . | b.reset(); |
flip() | Toggles all bits. | b.flip(); |
Conversion
Function | Purpose | Example |
to_string() | Converts bitset to a string. | cout << b.to _string(); |
to_ulong() | Converts bitset to an unsigned long. | cout << b.to _ulong(); |
to_ullong() | Converts bitset to an unsigned long long. | cout << b.to _ullong(); |
Bitwise Operators
Operator | Purpose | Example |
~ | Bitwise NOT (inverts all bits). | ~b |
& | Bitwise AND with another bitset . | b1 & b2 |
` | Bitwise OR with another bitset . | |
^ | Bitwise XOR with another bitset . | b1 ^ b2 |
ยซ | Left shift. Moves bits left by n positions. | b << 2 |
ยป | Right shift. Moves bits right by n positions. | b >> 2 |
STL Algorithms: The Magic Toolbox of C++ ๐งฐ
STL algorithms are like the Swiss army knives of C++. They let you perform tasks like sorting, searching, and modifying data with ease, making your life much simpler when working with containers. ๐ All these algorithms are available in the <algorithm>
and <numeric>
header files.
We can classify these algorithms into Manipulative and Non-Manipulative based on whether they modify the container or not.
Manipulative Algorithms: The Movers & Shakers ๐
Manipulative algorithms are the life of the STL party! Theyโre like the dance moves that shake things up in your containers, rearranging or modifying their elements to fit your needs. Here are some key moves:
1. copy()
Copying, like sharing cookies ๐ช!
Copies elements from one range to another.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> original = {1, 2, 3, 4};
std::vector<int> copied(4);
std::copy(original.begin(), original.end(), copied.begin());
// Output: 1 2 3 4
for (int num : copied) std::cout << num << " ";
return 0;
}
2. fill()
When you want all your cups full ๐ต!
Assigns a specified value to all elements in a range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v(4);
std::fill(v.begin(), v.end(), 7);
// Output: 7 7 7 7
for (int num : v) std::cout << num << " ";
return 0;
}
3. transform()
Transforming like a magic spell! ๐ฎ
Applies a function to each element and stores the result in another range.
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
int main() {
std::vector<int> v = {1, 4, 9, 16};
std::vector<int> transformed(v.size());
std::transform(v.begin(), v.end(), transformed.begin(), [](int x) { return std::sqrt(x); });
// Output: 1 2 3 4
for (int num : transformed) std::cout << num << " ";
return 0;
}
4. replace()
Say goodbye to the old, hello to the new ๐!
Replaces all occurrences of a specific value in a range with a new value.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 2, 3};
std::replace(v.begin(), v.end(), 2, 5);
// Output: 1 5 5 3
for (int num : v) std::cout << num << " ";
return 0;
}
5. swap()
Switching places, like two best friends! ๐ฏ
Exchanges the values of two variables.
#include <iostream>
#include <algorithm>
int main() {
int a = 5, b = 10;
std::swap(a, b);
// Output: a = 10, b = 5
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
6. reverse()
The perfect flip! ๐
Reverses the order of elements in a range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::reverse(v.begin(), v.end());
// Output: 4 3 2 1
for (int num : v) std::cout << num << " ";
return 0;
}
7. rotate()
Spin those elements around! ๐ก
Rotates the elements in a range such that a specific element becomes the first.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::rotate(v.begin(), v.begin() + 2, v.end());
// Output: 3 4 1 2
for (int num : v) std::cout << num << " ";
return 0;
}
8. remove()
Out with the old! ๐ฎ
Removes all elements with a specified value from a range but does not reduce the container size.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 2, 4};
auto it = std::remove(v.begin(), v.end(), 2);
// Output: 1 3 4 (container size remains the same, but 2s are removed)
for (int num : v) std::cout << num << " ";
return 0;
}
9. unique()
When youโve had enough of duplicates! ๐ฏโโ๏ธ
Removes consecutive duplicate elements from a range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 3, 3, 4};
auto it = std::unique(v.begin(), v.end());
// Output: 1 2 3 4 (remaining unique elements)
for (int num : v) std::cout << num << " ";
return 0;
}
These algorithms are your secret weapon to make containers in STL behave just the way you want whether you're filling them with love (or values) or transforming them into something new. Keep dancing! ๐
STL Non-Manipulative Algorithms: Keeping Things Chill ๐
Non-manipulative algorithms are like the chill friends at a partyโjust observing and offering advice without getting involved in the drama. They donโt change anything, they just analyze and help you find things. Here's how they work:
1. max_element()
Finding the rockstar of the group! ๐
Finds the maximum element in a given range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {3, 5, 7, 2, 8};
auto maxElem = *std::max_element(v.begin(), v.end());
// Output: 8
std::cout << "Max Element: " << maxElem << std::endl;
return 0;
}
2. min_element()
The underdog who deserves the spotlight ๐
Finds the minimum element in a given range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {3, 5, 7, 2, 8};
auto minElem = *std::min_element(v.begin(), v.end());
// Output: 2
std::cout << "Min Element: " << minElem << std::endl;
return 0;
}
3. accumulate()
Bringing everyone together for the greater good ๐ฐ
Finds the sum of elements in a given range.
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
int sum = std::accumulate(v.begin(), v.end(), 0);
// Output: 10
std::cout << "Sum: " << sum << std::endl;
return 0;
}
4. count()
How many times have we seen this face? ๐
Counts the occurrences of a specific element in a range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 2, 3, 4, 2};
int countTwo = std::count(v.begin(), v.end(), 2);
// Output: 3
std::cout << "Count of 2: " << countTwo << std::endl;
return 0;
}
5. find()
Whereโs Waldo? ๐
Returns an iterator to the first occurrence of an element in the range.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::find(v.begin(), v.end(), 3);
// Output: Found at position 2
if (it != v.end()) std::cout << "Found 3 at position: " << std::distance(v.begin(), it) << std::endl;
return 0;
}
6. is_permutation()
Twinsies! ๐คฉ
Checks if one range is a permutation of another.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {3, 2, 1};
if (std::is_permutation(v1.begin(), v1.end(), v2.begin())) {
// Output: Yes, they are permutations!
std::cout << "Yes, they are permutations!" << std::endl;
}
return 0;
}
7. is_sorted()
Are we in order? โ
Checks if the elements in a range are sorted in non-decreasing order.
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
if (std::is_sorted(v.begin(), v.end())) {
// Output: Yes, sorted!
std::cout << "Yes, sorted!" << std::endl;
}
return 0;
}
8. partial_sum()
Adding up the vibes ๐ฅ
Computes the cumulative sum of elements in a range.
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::vector<int> result(v.size());
std::partial_sum(v.begin(), v.end(), result.begin());
// Output: 1 3 6 10
for (int num : result) std::cout << num << " ";
return 0;
}
Non-manipulative algorithms are like your super cool, chill friends who keep things running smoothly without interfering. They help you analyze, find, and verify things without changing the flow of your data. So sit back, relax, and let these algorithms do their thing! ๐
Subscribe to my newsletter
Read articles from Sohil_Tamboli directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
