Introduction to Pre-Built Functions and Standard Libraries in C++
C++ is a strong programming language that has some native functions that allow us to code faster because these functions are well-optimized, allowing us to create robust code in fewer code lines.
Well, first of all, let’s say that we are going to use many of the standard libraries like:
- algorithm
- iostream
- stdio.h
- math.h
So instead of importing them one by one, we can include the next library which already adds them:
- bits/stdc++.h
The code will look the next way, and will import all of the libraries in C++ Standard Library – cppreference.com:
#include<bits/stdc++.h>
using namespace std;
int main(){
//Our code
return 0;
}
Now we can use any function present in those libraries, so we can ensure that by using them, our code will work on any device without further installations because we will only need to include C++ with a greater version than 11.
Useful pre-build functions
Sort
This function allows us to sort any array or vector in an ascending or descending way. It works for any data type that has a comparator, we can update it for custom data types:
Arrays in ascending
#include<bits/stdc++.h>
using namespace std;
int main(){
int nums[]={4,5,6,3,2};
int length=sizeof(nums) / sizeof(int);
sort(nums,nums+length);
for(int i=0;i<length;i++){
cout<<nums[i]<<" ";
}
//stdout: 2 3 4 5 6
return 0;
}
Vectors in descending
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>nums{4,5,6,3,2};
sort(nums.begin(),nums.end(),greater<int>());
for(int i=0;i<nums.size();i++){
cout<<nums[i]<<" ";
}
//stdout: 6 5 4 3 2
return 0;
}
Ascending for custom vector
#include<bits/stdc++.h>
using namespace std;
struct custom{
int x,y;
custom(int x1,int y1){
x=x1;
y=y1;
}
};
bool compareCustom(custom &a, custom&b){
return a.y<b.y;
}
int main(){
vector<custom>nums={custom(2,3),custom(1,5),custom(2,1)};
sort(nums.begin(),nums.end(),compareCustom);
for(int i=0;i<nums.size();i++){
cout<<nums[i].x<<" "<<nums[i].y<<endl;
}
//stdout:
//2 1
//2 3
//1 5
return 0;
}
Map
We can see this function as a dictionary, we have a pair containing the key and the value, they can have a different data type and it’s useful to link two data or check for existence. Map allows us to access and insert the value having the key with a complexity of O(logN)
Also, there is an alternative called Unordered-map which on average takes O(1) for insert and query but it can go to O(N) for querying in some specific scenarios.
#include<bits/stdc++.h>
using namespace std;
int main(){
map<int,int>visited;
visited[1]=1;
visited[3]=1;
for(int i=0;i<10;i++){
if(visited.count(i)){
cout<<i<<" was visited"<<endl;
}
}
//Stdout
//1 was visited
//3 was visited
return 0;
}
Vector
With vector we can easily resize, add, or remove elements, useful when we don’t know how many elements will have, the time complexity for adding an element is O(1) but in case it needs extra store O(N), the query time is O(1), and remove is O(N)
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int>nums;
int n,x;
cin>>n;
while(n--){
cin>>x;
nums.push_back(x);
}
for(auto el:nums){
cout<<el<<" ";
}
//input 3 2 5 6
//output 2 5 6
return 0;
}
Stack
Stack is a LIFO way to store the data, useful to solve problems like math expression validator, solve equations, and others, the time complexity for insertion, query, and removal is O(1).
#include<bits/stdc++.h>
using namespace std;
int main(){
stack<int>s;
s.push(1);
s.push(2);
s.push(3);
while(s.size()){
cout<<s.top()<<" ";
s.pop();
}
//stdout 3 2 1
return 0;
}
Queue
The queue is a FIFO data structure used for BFS algorithms. The time complexity is O(1) for all of the operations, but we can only access the frontmost element.
#include<bits/stdc++.h>
using namespace std;
int main(){
queue<int>q;
q.push(1);
q.push(2);
q.push(3);
while(q.size()){
cout<<q.front()<<" ";
q.pop();
}
//stdout 1 2 3
return 0;
}
I think you are missing some important functions like the priority queue which is useful for algorithms like Dijkstra
Pingback: Elevating Competitive Programming Skills with Data Structures and Algorithms - Algorithms Blog