STL C - 1810
STL C - 1810
STL C - 1810
While working on a problem that requires some basic tasks like sorting,
searching, etc we are not required to make a separate functionality to
achieve them. Rather, these functionalities are already present in C++ STL.
1. sort()
The sort function in STL is given a start pointer and an end pointer and it
sorts any array in O(log(N)) time complexity.
It can be used in any file using #include<algorithm>.
int main(){
return 0;
}
We can also sort the array of objects on the basis of one of the properties of
a class or structure to whom the object belongs. For that we are required to
pass a compare function along with other parameters and have to define
this compare function.
#include<iostream>
#include<algorithm>
int main(){
Interval arr[] = {{6, 4}, {3, 4}, {4, 6}, {8, 13}};
sort(arr, arr+4, compare);
return 0;
}
2. binary_search
Binary search can be applied only on a sorted array and its time complexity is
O(log(N)).
3. lower_bound
Examples:
1. Input: 10 20 30 40 50
Output: lower_bound for element 30 at index 2
2. Input: 10 20 30 40 50
Output: lower_bound for element 35 at index 3
Lower_bound & upper_bound can be applied only on a sorted array and its
time complexity is O(log(N)).
4. upper_bound
2. Input : 10 20 30 40 50
Output : upper_bound for element 45 is at index 4
Lower_bound & upper_bound can be applied only on a sorted array and its
time complexity is O(log(N)).
int main(){
sort(arr,arr+6);
for(int i=0;i<6;i++){
cout<<arr[i] << " ";
}
cout<<endl;
cout << binary_search(arr,arr+6,2) << endl;
return 0;
}
STL Examples
Print the array of unique elements. (you can change the order of occurrence
of the elements.)
Input Format :
Line 1: Contains the size of array
Line 2: M integers which are elements of the array separated by space
Output :
Resultant array
Sample Input :
7
2433534
Sample Output :
2345
Explanation :
Approach 1 : This problem can be easily solved using a Set. We will first
make a set using the inbuilt STL and a result array. Then we will traverse
through our input array and start adding the elements in the set. While
traversing the input array if we encounter any duplicates, that is if the
element has already been added in the set , then we will not add the element
to the result array otherwise we will.
Approach 2 : Sort the elements in the input array so that all the duplicate
elements will be together. Then traverse through the array and compare
elements with the element present at the previous index; if they are equal do
not add them in the result array otherwise add them.
For example :
vector<int> removeDuplicates(vector<int> input){
vector<int> result;
sort(input.begin(), inpt.end());
result.push_back(input[0]);
if(input[i] != input[i-1]){
result.push_back(input[i]);
}
}
return result;
}
If there are multiple non repeating characters, print the unique character
which comes first in the string. Or if all the characters are repeating, print the
first character of the initial string.
Input Format :
Line 1: A String
Output :
First non repeating character
Sample Input :
aDcadhc
Sample Output :
D
Explanation : This problem can be solved using a map because with a map
we can store the elements along with their frequencies.