Professional Documents
Culture Documents
STL CONTAINERS
• STL stands for Standard Template Library
• Container is an object which is used to store a set of objects with a
particular type
TYPES OF CONTAINERS
• C++ supports three types of containers. They are
1. Sequential Containers
2. Associative Containers
3. Unordered Associative Containers
Container Types
Examples
1. SEQUENTIAL CONTAINERS
• Here elements are arranged in sequential order
• This can be implemented either using array or linked list.
Examples
• Array
• Vector
• Forward List (Singly Linked List)
• List (Doubly Linked List)
• Deque
Vector
• Vector is a dynamic array in c++ and available in the header file
<vector>
• Like array, it is used to store the elements of a specified type.
Required Header File
#include<vector>
Syntax
vector<type> name;
Where,
• Type can be any base types like int, char*, float, char, bool, etc,
Example
vector<int> lb;
4. empty()
• It is an instance method of vector class
• This method is used to check whether vector is empty or not
• It does not take any arguments
• Return type: bool
5. clear()
• It is an instance method of vector class
• It is used to remove all the elements in the vector
• It does not take any arguments
• Return type: void
I. EXAMPLE OF INTEGER VECTOR
SOURCE CODE
// add iostream for basic c++ support and IO operations
#include<iostream>
// add vector header file
#include<vector>
using namespace std;
int main()
{
cout<<"--------------------------------------\n";
cout<<"\t Intger Vector Example\n";
cout<<"--------------------------------------\n";
// create an empty vector of int type
vector<int> vd;
// add elements to end of the vector
vd.push_back(12);
vd.push_back(18);
vd.push_back(24);
vd.push_back(41);
vd.push_back(50);
// display the elements in vector
for(int i=0;i<vd.size();i++)
{
cout<<" "<<vd[i]<<"\n";
}
return 0;
}
OUTPUT
for(i=0;i<db.size();i++)
{
cout<<" "<<db[i]<<"\n";
}
}
// pop the elements from end of the vector using pop_back()
void del()
{
int i,ch=0;
cout<<"Enter no of elements to delete : ";
cin>>ch;
if(ch>n)
cout<<"List Index out of range...";
else
{
for(i=0;i<ch;i++)
db.pop_back();
cout<<ch<<" elements are successfully deleted...\n";
cout<<"Do u want to see the elements : press yes /no : ";
cin>>user;
if(strcmp(user,"yes")==0||strcmp(user,"YES")==0)
{
cout<<"------------------------------------------------------\n";
cout<<" \n"; cout<<"\t\tData Retrieval in Vector\n";
disp();
}
}
}
};
int main()
{
cout<<"=========================================\n";
cout<<"\t\tVector (Dynamic Array)\n";
cout<<"=========================================\n";
DynamicArray obj;
cout<<"\t\tInsertion in Vector\n";
obj.add();
cout<<"\t\tData Retrieval in Vector\n";
cout<<"------------------------------------------------------\n";
obj.disp();
cout<<"\t\tDeletion in Vector\n";
cout<<"------------------------------------------------------\n";
obj.del();
cout<<"------------------------------------------------------\n";
return 0;
}
OUTPUT
Where,
• Type can be any base types like int, char*, float, char, bool, etc,
Example
list<int> ld;
2. push_front(ele)
• It is an instance method of list class
• It is used to add the elements of specified type to front of the list
• It takes only one argument which is a particular type like int / float /
char etc,.
• Return type: void
3. pop_back()
• It is an instance method of list class
• It is used to remove the top element (last element) from the list
• It does not take any arguments
• Return type: void
4. pop_front()
}
// pop the elements from end of the linked list (Back End) using
pop_back()
void del()
{
int i,ch=0;
cout<<"Enter no of elements to delete : "; cin>>ch;
if(ch>n)
cout<<"List Index out of range...";
else
{
for(i=0;i<ch;i++)
no.pop_back();
cout<<ch<<" elements are successfully deleted from top...\n";
cout<<"Do u want to see the elements : press yes /no : ";
cin>>user;
if(strcmp(user,"yes")==0||strcmp(user,"YES")==0)
{
cout<<"----------------------------------------------------\n";
disp();
}
}
}
void disp()
{
cout<<"Total elements in Linked List : "<<no.size()<<"\n";
for(ii=no.begin();ii!=no.end();ii++)
{
cout<<"->"<<*ii;
}
Containers in C++ Page 13
| C++ Containers
cout<<endl;
}
};
int main()
{
cout<<"====================================\n";
cout<<"\tList STL(Linked List) at BACK End\n";
cout<<"====================================\n";
Linkedlist obj;
obj.add();
cout<<"----------------------------------------------------\n";
obj.disp();
cout<<"----------------------------------------------------\n";
obj.del();
cout<<"----------------------------------------------------\n";
return 0;
}
OUTPUT
}
// display the elements in linked list
void disp()
{
cout<<"Total elements in Linked List : "<<no.size()<<"\n";
for(ii=no.begin();ii!=no.end();ii++)
{
cout<<"->"<<*ii;
}
cout<<endl;
}
// pop the elements from front of the linked list using pop_front()
void del()
{
int i,ch=0;
cout<<"Enter no of elements to delete : "; cin>>ch;
if(ch>n)
cout<<"List Index out of range...";
else
{
for(i=0;i<ch;i++)
{
no.pop_front();
}
cout<<ch<<" elements are successfully deleted...\n";
cout<<"Do u want to see the elements : press yes /no : ";
cin>>user;
if(strcmp(user,"yes")==0||strcmp(user,"YES")==0)
{
cout<<" \n";
disp();
}
}
}
};
int main()
{
cout<<"==========================================\n";
cout<<"\tList STL(Linked List) at FRONT End\n";
cout<<"==========================================\n";
Linkedlist obj;
obj.add();
cout<<"---------------------------------------------------\n";
obj.disp();
cout<<"---------------------------------------------------\n";
obj.del();
cout<<"---------------------------------------------------\n";
return 0;
}
OUTPUT
3. begin()
• This is the instance method of the set
• It is used to return an iterator which is the beginning of the set (address
of the first element in the set)
• Return type: iterator
4. end()
• This is the instance method of the set
• It is used to return an iterator which is the ending of the set (address of
the last element in the set)
• Return type: iterator
5. clear()
• It is an instance method of the set
• It is used to remove all the elements from set
• Return type: void
6. empty()
• It is an instance method of the set
• It is used to check whether the set is empty or not
• Return type: bool
OUTPUT
db.insert("Welcome to Java");
// remove particular element
db.erase("Hello World");
cout<<"\t Displaying Elements using Iterator\n";
cout<<"-------------------------------------------------------------\n";
// display the contents using iterator
for(ii=db.begin();ii!=db.end();ii++)
{
cout<<*ii<<" "<<"\n";
}
cout<<"\n";
// delete all the elements
db.clear();
cout<<"Elements after Deletion:\n";
cout<<"-------------------------------------------------------------\n";
if(db.size()!=0)
{
for(auto d:db)
{
cout<<d<<"\n";
}
}
else
{
cout<<"Set is empty\n";
}
int main()
{
cout<<"--------------------------------------------\n";
cout<<"\t Set Data Structure\n";
cout<<"--------------------------------------------\n";
disp();
return 5;
}
OUTPUT
MAP
• It is a data structure which is used to store the key value elements in
the sorted order
• Here key does not accept the duplicate elements. It should be
unique. But value can accept duplicate elements
• Map is created using the reserved keyword map followed by { {k1,v1},
..{kn,vn}} and it is available on #include<set> header file
• Elements in the map can be displayed using two ways
▪ Using Iterator
▪ Using ranged for loop
IMPORTANT METHODS AND PROPERTIES OF MAP CONTAINER
1. insert({key, value})
• This is the built-in instance method of the map
• It is used to insert an key-value pair of elements in the map
• This will take one or more number of arguments where first argument
is the key value pair of elements of the specific type of map
• Return type: iterator
2. size()
• This is the built-in instance method of the set
• It is used to display the total number of key-value pair of elements in
map as integer
• Return type: void
3. begin()
• This is the built-in instance method of the map
• It is used to return an iterator which is the beginning of the map
(address of the first element in the map)
• Return type: iterator
4. end()
• This is the built-in instance method of the set
• It is used to return an iterator which is the ending of the set (address of
the last element in the set)
• Return type: iterator
5. clear()
• It is an instance method of the map
• It is used to remove all the key-value pair of elements from map
• Return type: void
6. empty()
• It is an instance method of the map
• It is used to check whether the map is empty or not
• Return type: bool
7. at()
• It is an instance method of the map
• It is used to return a reference to the value associated with the given
key
• Return type: const mapped_type &
8. operator[index key]
• It is the operator [] followed by index position which is the key of the
map
• It is used to return a reference to the value associated with the given
key
• Return: const mapped_type &
OUTPUT
OUTPUT
4. size()
• This is the instance method of stack adapter
• This method is used to display the total number of elements in the
stack
• Return type: size_type
5. empty()
• This is the instance method of stack adapter
• It is used to check whether the stack is empty or not
• Return type: bool
OUTPUT
2. QUEUE
• Unlike stack, it is first in first out data structure
• It uses two ends where,
▪ Rear end is used for adding the elements at the back of queue
▪ Front end is used for removing the elements from the front of
queue.
• Required Header File
▪ #include<queue>
• Example – Ration Queue
IMPORTANT METHODS
1. push(ele)
• This is the instance method of queue adapter
• This method is used to add the element at the end of the queue or
back of the queue (rear end)
• Return type: void
2. pop()
• This is the instance method of queue adapter
• It is used to remove the element at the front of the queue (front end)
• It does not take any argument
• Return type: void
3. front()
• This is the instance method of queue adapter
• This method is used to return or display the first element of the queue
• Return type: const_reference
4. back()
• This is the instance method of queue adapter
• This method is used to return or display the last element of the queue
• Return type: const_reference
5. size()
• This is the instance method of queue adapter
• This method is used to display the total number of elements in the
queue
• Return type: size_type
6. empty()
• This is the instance method of queue adapter
• It is used to check whether the queue is empty or not
• Return type: bool
// display the elements of queue using empty(), top and pop() methods
while(db.empty()!=true)
{
// display (return) the first element of queue
cout<<" "<<db.front()<<" "<<"\n";
// remove the element from queue (front end)
db.pop();
}
cout<<"------------------------------------------------\n";
return 5;
}
OUTPUT
3. PRIORITY QUEUE
• It is extended version of normal queue
• It is a type of STL container which consider only the highest priority
element
• Queue follows the FIFO order for displaying elements while priority
queue follows the priority order for displaying elements
• It is used to display the elements based on the highest priority
(highest priority element is popped first)
• If two or more number of elements exists with same priority then, the
order of element in the queue will be taken into consideration
• Required Header File
▪ #include<queue>
}
cout<<"----------------------------------------------------------\n";
return 5;
}
OUTPUT