STL Functions in C++
Standard Library Items ⭐
Section titled “Standard Library Items ⭐”In C++, std:: is used before standard library items like data types (std::string, std::vector, std::map, std::set, std::pair, std::array), input/output (std::cin, std::cout, std::cerr, std::endl), algorithms (std::sort, std::find, std::reverse, std::min, std::max), utility functions (std::move, std::swap, std::make_pair, std::make_tuple, std::to_string), smart pointers (std::unique_ptr, std::shared_ptr), and exception handling (std::exception, std::runtime_error, std::logic_error, std::invalid_argument). This usage avoids naming conflicts and clarifies that these are standard library elements.
String Input from user
std: :library
- Input a string
cin>>can take String as input directly from terminal . (stops reading at whitespace).
string s;cin>>s;- input a line
getline(cin, a)of strings
string a, b;getline(cin, a);Content:
- Algorithm
- Utility
- Functional
- Math
A. <algorithm>
Section titled “A. <algorithm>”1. Sort()
Section titled “1. Sort()”
<algorithm>
1. Using vec.begin() & vec.end()
std::sort(vec.begin(), vec.end());2. Usingbegin(vec) & end(vec)
std::sort(std::begin(vec),std::end(vec));2. max_element()
Section titled “2. max_element()”
<algorithm>
1. Storing Pointer, then Assigning Point value
Auto max_itr = max_element(arr.begin(), arr.end())int maxx = *max_itr`✅2. Assigning Pointer value directly
int maxx = *max_element(arr.begin(), arr.end())`✅Incorrect: Storing Iterator in an int ❌
int maxx = max_element(arr.begin(), arr.end()) // Errornote: std::max_element() returns an iterator, not an integer.
3. max() & min()
Section titled “3. max() & min()”
<algorithm>
#include <algorithm> // For std::max and std::min
// Maximum of twoint maximum = std::max(a, b);
// Minimum of twoint minimum = std::min(a, b);4. binary_search() (for Sorted Containers)
Section titled “4. binary_search() (for Sorted Containers)”
<Algorithm>
bool found = std::binary_search(v.begin(), v.end(), 3);- Complexity: O(log N).
- Works with:
std::vector,std::deque,std::array(sorted).
B. <utility>
Section titled “B. <utility>”1. swap()
Section titled “1. swap()”
<utility>
The in-built std::swap function in C++ exchanges the values of two variables. It is defined in the <utility> header and works with any data type. Example:
#include <utility>std::swap(a, b);This function is efficient and widely used in algorithms like sorting.
2. make_pair()
Section titled “2. make_pair()”
<utility>
The make_pair function is defined in the <utility> header. However, it is indirectly included when you include headers like <map>, <set>, <unordered_map>, <unordered_set>, <algorithm>, <tuple>, and <queue>. These headers often include <utility> internally because they use std::pair or other utility functions.
Here’s an example of how to use make_pair:
#include <utility> // For std::make_pair
pair<int, int> p;p = make_pair(a, b);or
pair<int, int> p;p = {a,b};This code correctly creates a std::pair using the make_pair function, where a and b are the values you want to pair together.
C. <functional>
Section titled “C. <functional>”1. greater<int>
Section titled “1. greater<int>”
<functional>
Purpose
- Used to create a min-heap in C++‘s
priority_queueby defining a comparison that returnstrueif the first element is greater than the second.
Usage:
#include <queue>#include <functional>
int main() { // Min-heap declaration std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;Effect:
std::greater<int>causes thepriority_queueto behave as a min-heap, where the smallest element is at the top.
Summary
greater<int>: Defines ascending order for a priority queue.- Min-Heap: Smallest element on top.
D. <cmath>
Section titled “D. <cmath>”2. ceil()
Section titled “2. ceil()”
<cmath>
Summary of Using double ceil vs. int ceil with 10/3
int ceil(10/3):- Integer Division:
10 / 3evaluates to3(truncated). - Result:
ceil(3)returns3, not the correct ceiling of the division.
- Integer Division:
double ceil(10/3):- Floating-Point Division: Use
10.0 / 3orstatic_cast<double>(10) / 3to get3.333.... - Result:
ceil(3.333...)correctly returns4.
- Floating-Point Division: Use
Conclusion
- Using
doublewithceilis essential for accurate ceiling calculations, as it ensures floating-point division, yielding the correct result.
3. pow()
Section titled “3. pow()”
<cmath>
To calculate ( x ) raised to the power ( y ) (i.e., ( x^y )).
#include <cmath> double x = 2.0; // Base double y = 3.0; // Exponent double result = pow(x, y); // x^yNote: Result Type: The pow function returns a double type, which allows for floating-point calculations.
Note:-
- Negative Exponents: The
powfunction can handle negative exponents. For example,pow(2, -3)returns0.125(since ( 2^{-3} = \frac{1}{8} )). - Integer and Floating-Point Base/Exponent: Both
xandycan be integers or floating-point numbers. - If both
xandyare integers, you can still usepow, but the result will be cast to adouble. If you need an integer result and bothxandyare integers, you can cast the result back to an integer:
Result Integer
#include <cmath> int x = 2; int y = 3; int result = static_cast<int>(pow(x, y));}E. <numeric>
Section titled “E. <numeric>”1. accumulate()
Section titled “1. accumulate()”
<numeric
accumulate() returns the sum of elements in a range.
vector<int> A = {2, 1, 5, 1, 2, 2, 2};int total = accumulate(A.begin(), A.end(), 0); // total = 15// accumulate(start_iterator, end_iterator, initial_value)