STL me ek #include <algorithm> header hota hai jisme bahut saare ready-made functions hote hain jo searching, sorting, counting, modifying, etc. ke kaam aate hain.
⚡ Time-saving + error-free code ke liye best hota hai.
| Function | Use |
|---|---|
sort() |
Sorting |
reverse() |
Reverse any container |
count() |
Kitne baar element aya |
find() |
Kisi element ko dhoondhna |
accumulate() |
Sum nikalna |
max_element() |
Max value |
min_element() |
Min value |
binary_search() |
Sorted array me search |
next_permutation() |
Next order permutation |
prev_permutation() |
Previous order permutation |
lower_bound() |
First element >= value |
upper_bound() |
First element > value |
sort(start, end)cpp
Copy code
sort(v.begin(), v.end()); // Ascending order
⚠️ Time Complexity: O(N log N)
👉 Descending ke liye:
cpp
Copy code
sort(v.begin(), v.end(), greater<int>());
reverse(start, end)cpp
Copy code
reverse(v.begin(), v.end()); // Vector ko ulta kar do
count(start, end, value)cpp
Copy code
int c = count(v.begin(), v.end(), 5); // Count how many 5s are there