-
Notifications
You must be signed in to change notification settings - Fork 1
Standard Template Library STL
When you are taking part in a competition, creating functions/methods for certain tasks, like extracting sub-string, sorting, adding and removing elements array, requires time to code and hence you lose out on valuable time. A good understanding of the library functions is important for writing quick and bug-free code. Most programming languages provide with predefined libraries which help you do the same. Here are a few you'll need to know to get started with competitive coding.
It is always suggested you understand how these are implemented without using STL, because if you ever face a problem where time is a constraint or requires a specific form of updation which STL do not have, you will have to code a program for it.
Imports for C++ STL
#include<bits/stdc++.h>
Imports for Java Utility Package
import java.util.*;
import java.io.*; // Classes for Fast I/O implementation in Java
If you have ever tried coding in C, you are probably used to declaring a string using an array of characters. C++ offers a string class that makes string handling easier.
The following code snippet shows how to declare and use a string :
In C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str = "Coding is fun"; // Declaring and initialising string
int len = str.length(); // the length() function returns the length of the string
cout<<"The length of string "<<str<<" is "<<len<<endl;
return 0;
}
Java equivalent
import java.util.*;
public class Solution{
public static void main(String[] args){
String str = "Coding is fun"; // Declaring and initialising string
int len = str.length(); // the length() function returns the length of the string
System.out.println("The length of the string "+str+" is "+len);
}
}
A major advantage of using string class instead of character array is that you don't need to know the size of the string while declaring it.
Concatenating strings is also easier with the string class. You can concatenate two strings using the '+' operator.
For comparing if two strings are equal , you can either use the relational operators('<' , '>' , '==' , "!="). Another way is using the compare function.
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Input
string str1; // Declaring a string
cin>>str1; // Taking user input for the string
int len = str1.length(); // the length() function returns the length of the string
cout<<"The length of string "<<str1<<" is "<<len<<endl;
// Concatenating
string s1 = "Hello " , s2 = "World" , s3;
s3 = s1 + s2; // Concatenating the strings
cout<<s3<<endl;
//Referencing strings elements
for(int i=0 , n=s3.length() ; i < n ; i++)
{
cout<<"Char "<<i+1<<" of s3 is "<<s3[i]<<endl; // Referencing ith element of a string
}
// Comparing strings
string s4 = "aaa" , s5 = "aab"; // s5 is greater than s4
if(s4 <= s5) cout<<"s4 is less than s5\n"; // Use the <= operator to compare strings instead strcomp()
if(s4.compare(s5)) cout<<"s4 and s5 are not equal\n"; // s4.compare(s5) returns 0 if s4 and s5 are equal
string s6=""; // declare an empty string
for(int i=0 ; i < 26 ; i++)
{
s6 += (i + 'a'); // Concatenation using compound assignemnt ( += ) also works
}
cout<<s6<<endl;
return 0;
}
Java equivalent
import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.next();
int len = str.length();
System.out.println("The length of the string "+str+" is "+len);
String s1 = "Hello",s2 = "World",s3;
s3 = s1 + s2; // Concatenating strings
System.out.println(s3);
for(int i=0;i<s3.length();i++){
// ith element
System.out.println("Char at "+(i+1)+" of s3 is "+s3.charAt(i));
}
String s4 = "aaa" , s5 = "aab";
// comparing using compareTo()
if(s5.compareTo(s4)>0)
System.out.println("s5 greater than s4");
else if(s5.compareTo(s4)<0)
System.out.println("s5 less than s4");
else
System.out.println("s5 equal to s4");
String s6 = "";
for(int i=0;i<26;i++){
s6 += (char)(i+'a'); // shorthand operator +=
}
System.out.println(s6);
}
}
A vector is a container i.e. something you can use to store many elements together. It is similar to an array but with added functionalities.
You don't need to know the size of a vector when declaring it(dynamic allocation of memory). You can also get a vector's length and resize a vector at runtime.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v1(10); // this is a declaration for a vector of integers that has size 10. Note that unlike an array declaration, we use ()
// instead of []
for(int i=0 ; i < 10 ; i++)
v1[i] = i;
}
for(int i=0 ; i < 10 ; i++)
{
cout<< v1[i] << " ";
}
cout<< endl;
vector<int> v2; // Here, we are not declaring the size of the vector
int len = v2.size(); // v.size() returns the size of the vector
cout<<"Initial size of v2 is "<<len<<endl; // Output is 0, because we have not specified the size of the vector yet.
{
for(int i=0 ; i < 15 ; i++)
v2.push_back(i); // Since v[i] is not a valid index yet, we use the push_back() function to insert an element into the vector
}
for(int i=0 ; i < 15 ; i++) cout<<v2[i]<<" ";
cout<<endl;
int len1 = v1.size() , len2 = v2.size();
cout<<"Sizes of the two vectors are "<<len1<<" and "<<len2<<endl;
}
return 0;
You can use the empty() function which returns boolean value true or false to check if a vector is empty.
if(v.empty())
cout<<"Vector is empty"<<endl;
else
cout<<"Vector is not empty"<<endl;
The resize function can be used to resize a vector once it is filled up to its current capacity.
The clear function clears the vector.
(Code snippet shows the use of above mentioned function)
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> v1(10,"Hello"); // Declaring a vector of of size 10 that holds strings. Each index in the vector
// is initialized to the value "Hello"
for(int i=0 , n = v1.size() ; i<10 ; i++)
{
cout<<"Replacing "<<v1[i]<<" with ";
v1[i] = "World";
cout<<v1[i]<<endl;
}
v1.resize(20); // Resizing the vector to 20
for(int i=10 ; i<19 ; i++) v1[i] = "Hello";
vector<string> v2 = v1 ; // Here v2 is initialized with the same size and values as v1
for(int i=0 , n = v2.size() ; i<n ; i++) cout<<v2[i]<<" ";
cout<<endl;
if(!v1.empty()) cout<<"Length of v2 is "<<v2.size()<<endl; // v1.empty() returns 1/true if the vector is empty, else it returns 0/false
v2.clear(); // After clearing the vector v2 , the outut of v.size() is 0
cout<<"Size of v2 after clearing is "<<v2.size()<<endl;
return 0;
}
Java equivalent is ArrayLists
import java.util.*;
public class Solution{
public static void main(String[] args){
ArrayList<Integer> v1 = new ArrayList<Integer>(Arrays.asList(new Integer[10]));
for(int i=0;i<10;i++){
v1.set(i,i);
}
for(int i=0;i<10;i++){
System.out.println(v1.get(i));
}
ArrayList<Integer> v2 = new ArrayList<Integer>();
System.out.println("Initial size is "+v2.size());
for(int i=0;i<15;i++){
v2.add(i); // cannot use set() as they cannot be indexed yet
}
for(int i=0;i<15;i++){
System.out.println(v2.get(i));
}
System.out.println("Length of v1 : "+v1.size()+" and Length of v2 : "+v2.size());
v2.ensureCapacity(50); // resize to 50
if(!v2.isEmpty()) // isEmpty() function to check if arraylist is empty
System.out.println("Size of v2 is "+v2.size());
v2.clear(); // clear arraylist
System.out.println("Size of v2 is now : "+v2.size());
}
}
C++ provides the sort(pointer_to_first_ele , pointer_to_last_ele) function to sort arrays and vectors. The sort function is more efficient than sorting algorithms like bubble sort and selection sort. It is also easier to use.
int a[5] = {5 , 4 , 3 , 2 , 1};
sort(a , a + n); // to sort an array we pass the pointer to the first and last element of the array
vector<int> v(10);
for(int i=10 ; i > -1 ; i--) v[i] = i;
sort(v.begin() , v.end()) // Here also, we pass the pointer to the first and last element in the vector.
// v.begin() returns the pointer to the first element and v.end() returns the pointer to the last element
Java equivalent is using Arrays.sort() and Collections.sort()
int a[] = {5 , 4 , 3 , 2 , 1};
Arrays.sort(a); // sort an array
for(int i=0;i<5;i++)
System.out.println(a[i]);
ArrayList<Integer> v = new ArrayList<Integer>();
for(int i=4;i>=0;i--) v.add(i);
Collections.sort(v); // sort an arraylist
for(int i=0;i<5;i++)
System.out.println(v.get(i));
The min and max functions can be used to find the minimum and maximum of multiple numbers without using many if else statements.
int a=1 , b=2 , c=3 , d=4;
int maximum = max(max(a , b) , max(c , d)); // maximum of four numbers
int minimum = min(min(a , b) , min(c , d)); // minimum of four numbers
Java equivalent
int a = 1,b = 2,c = 3,d = 4;
int maximum = Math.max( Math.max(a,b) , Math.max(c,d) );
int minimum = Math.min( Math.min(a,b) , Math.min(c,d) );
To use max(), min() directly you can use static imports
import static java.lang.Math.*;
A great place to get started would be geeksforgeeks, links for which are provided below.