🧠

STL Shortcut Algorithms

 

Finding Maximum and Minimum element in a container :

To find the maximum and minimum element in a container in C++, we can use the std::max_element and std::min_element functions respectively. These functions take the beginning and end iterators of a container and return the iterator pointing to the maximum or minimum element. We can then dereference these iterators to get their values.
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> nums {4, 2, 5, 1, 3}; auto max_elem = std::max_element(nums.begin(), nums.end()); auto min_elem = std::min_element(nums.begin(), nums.end()); std::cout << "Maximum element is: " << *max_elem << '\\n'; std::cout << "Minimum element is: " << *min_elem << '\\n'; return 0; }
 

Reverse a string :

To reverse a string in C++, we can use the std::reverse function in the <algorithm> header. This function takes two iterators, the beginning and end iterators of a container, and reverses the elements between them.
#include <iostream> #include <string> #include <algorithm> int main() { std::string str {"Hello, World!"}; std::reverse(str.begin(), str.end()); std::cout << "Reversed string is: " << str << '\\n'; return 0; }
 

Replace in a string :

To replace a substring in a string in C++, we can use the replace function of the std::string class. This function takes three arguments: the starting position of the substring to be replaced, the length of the substring to be replaced, and the new substring to be inserted in its place.
#include <iostream> #include <string> int main() { std::string str {"I love apples"}; str.replace(7, 6, "oranges"); std::cout << "New string is: " << str << '\\n'; return 0; }
 

Sum of elements in a container :

To find the sum of elements in a container in C++, we can use the std::accumulate function in the <numeric> header. This function takes three arguments: the beginning and end iterators of the container, and the initial value of the sum.
#include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> nums {4, 2, 5, 1, 3}; int sum = std::accumulate(nums.begin(), nums.end(), 0); std::cout << "Sum of elements: " << sum << '\\n'; return 0; }
 

Lower and upper bound :

To find the lower and upper bound of an element in a sorted container in C++, we can use the std::lower_bound and std::upper_bound functions in the <algorithm> header respectively. std::lower_bound takes the beginning and end iterators of the container and the element to be searched for, and returns an iterator pointing to the first element that is not less than the given element. std::upper_bound works similarly, but returns an iterator pointing to the first element that is greater than the given element.
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> nums {4, 2, 5, 1, 3}; std::sort(nums.begin(), nums.end()); auto lower = std::lower_bound(nums.begin(), nums.end(), 2); auto upper = std::upper_bound(nums.begin(), nums.end(), 3); std::cout << "Lower bound of 2 is at index: " << std::distance(nums.begin(), lower) << '\\n'; std::cout << "Upper bound of 3 is at index: " << std::distance(nums.begin(), upper) << '\\n'; return 0; }