 # STL Classes – C++ (Part – 1)

As we all know, Data Structure is one of most important part of an Computer Science student. It tells us how we efficiently use our data. It is also important for competitive programming which sharpens our mind for solving any problem.

In C++, there are various in-built libraries which saves your time while using data structure or doing competitive programming.

I suggest you to learn about code implementation of Data Structures thoroughly before going to STL classes.

Contents

### What is STL classes?

STL stands for Standard Template Library which includes every Data structures in short. Just we have to include library accordingly and use in-built functions of it like for Array – “Vector”, Linked list – “List”, Stack – “Stack”, Queue – “Queue”,etc. Functions in STL classes have o(1) time complexity so in competition it saves us from TLE. Let’s have a deep dive in each.

### Vector STL

Vector is used for array implementation. It has in-built functions for inserting, deleting, searching and many more. Vector is dynamical we can increase size without re-initialize it.

1. push_back(value) :- push_back(value) function is used to insert element in array.
2. pop_back() :- pop_back() is used to delete element from array.
3. assign() :- assign() is used to assign new value to old position and delete old one.
4. clear() :- clear() is used to remove all elements from vector.
5. size() :- size() returns size of vector.
6. swap() :- swap() is used to swap elements of two vectors.
7. begin() :- begin() returns first element of vector in iterator.
8. end() :- end() returns last element of vector in iterator.

```#include<iostream>
#include<vector>
using namespace std;

int main(){
vector<int> vec; //initializing vector
//inserting 4 values into vector
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
vec.push_back(40);
for(int i=0;i<vec.size();i++)
cout<<vec[i]<<" "; //output :- 10 20 30 40
vec.pop_back(); //deleting element
cout<<"\nAfter deleting an element ";
for(int i=0;i<vec.size();i++)
cout<<vec[i]<<" "; //output :- 10 20 30
vec.clear();
return 0;
}
```

### Stack STL

As name tells it is used for implementing stack using its in-built function. It is also dynamic we don’t need to initialize stack size before using.

Initialize :- stack<data type> stack_name;

1. push(value) :- push() is used to insert value at top of stack.
2. pop() :- pop() is used to delete top element from stack.
3. empty() :- empty() checks whether stack is empty or not.
4. top() :- top() returns top element of stack.
5. size() :- size() returns size of stack.
6. swap() :- swap() exchange elements of two stack.
7. emplace(value) :- emplace(value) construct a value then insert in stack.
```#include<iostream>
#include<stack>
using namespace std;
int main(){
stack<int> st; //initializing vector
//inserting 4 values into vector
st.push(10);
st.push(20);
st.push(30);
st.push(40);
cout<<"Elements are ";
while(!s.empty())
{
cout<<st.top()<<","; //output :- 40,30,20,10
st.pop();
}
st.pop(); //deleting element
cout<<"Elements are ";
while(!st.empty())
{
cout<<st.top()<<" "; //output :- 30,20,10
st.pop();
}
cout<<st.top(); //output :- 30
return 0;
}
```

### Queue STL

This is used to implement a dynamical sized queue data structure using FIFO principal.

Initialize :- queue<data type> queue_name;

1. push(value) :- push(value) is used to insert value at front of queue.
2. pop() :- pop() is used to delete element from last of queue.
3. front() :- front() returns value of first index of queue.
4. back() :- back() returns value of last index of queue.
5. size() :- size() returns size of queue.
6. swap() :- swap() exchange elements of two queue.
```#include <iostream>
#include<queue>
using namespace std;

int main()
{
queue<int> q1;
q1.push(100);
q1.push(200);
q1.push(300);
q1.push(400);
cout<<"\nElements are ";
while(!q1.empty())
{
cout<<q1.front()<<" "; //output :- 100 200 300 400
q1.pop();
}
cout<<"\nFront element of queue = "<<q1.front(); //output :- 100
cout<<"\nLast element of Queue = "<<q1.back(); //output :- 400
cout<<"\nAfter performing deque operation ";
q1.pop();
cout<<"\nElements are ";
while(!q1.empty())
{
cout<<q1.front()<<" ";  //output :- 200 300 400
q1.pop();
}
return 0;
}```

There are many STL classes left which we will learn in next chapter.