STL Algorithms Tutorial
Introduction to STL Algorithms
The Standard Template Library (STL) in C++ provides a comprehensive set of algorithms that can be used with the various containers such as vectors, lists, and arrays. These algorithms are designed to work with iterators, offering a high level of abstraction and flexibility.
Commonly Used STL Algorithms
Here are some of the commonly used STL algorithms:
- Sorting Algorithms:
sort
,stable_sort
- Searching Algorithms:
find
,binary_search
- Non-modifying Sequence Algorithms:
for_each
,count
- Modifying Sequence Algorithms:
copy
,transform
- Set Algorithms:
set_union
,set_intersection
Sorting with sort
The sort
algorithm is used to sort elements in a range. It requires the beginning and end iterators of the range to be sorted.
Example
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {5, 2, 9, 1, 5, 6}; std::sort(vec.begin(), vec.end()); for (int n : vec) { std::cout << n << ' '; } return 0; }
Searching with find
The find
algorithm searches for the first occurrence of a value in a range. It returns an iterator to the element if found, or the end iterator if not found.
Example
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {5, 2, 9, 1, 5, 6}; auto it = std::find(vec.begin(), vec.end(), 9); if (it != vec.end()) { std::cout << "Element found at position: " << std::distance(vec.begin(), it); } else { std::cout << "Element not found"; } return 0; }
Counting with count
The count
algorithm counts the number of occurrences of a value in a range.
Example
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {5, 2, 9, 1, 5, 6}; int count = std::count(vec.begin(), vec.end(), 5); std::cout << "Number of occurrences of 5: " << count; return 0; }
Transforming with transform
The transform
algorithm applies a function to a range of elements and stores the result in another range.
Example
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::vector<int> result(vec.size()); std::transform(vec.begin(), vec.end(), result.begin(), [](int x) { return x * x; }); for (int n : result) { std::cout << n << ' '; } return 0; }
Set Operations with set_union
The set_union
algorithm creates a sorted union of two sets.
Example
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> set1 = {1, 3, 5, 7}; std::vector<int> set2 = {2, 3, 5, 8}; std::vector<int> result; std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), std::back_inserter(result)); for (int n : result) { std::cout << n << ' '; } return 0; }
Conclusion
STL algorithms in C++ offer a powerful and flexible way to perform various operations on containers. Understanding and using these algorithms can significantly simplify your code and improve performance. This tutorial has covered some of the basic and commonly used algorithms, but there are many more that you can explore and utilize in your applications.