Question: Explain how the 1. Vectors 2. Stack, 3. List, 4. Map can be implemented using STL.
Applications
of Container Class
•
Vector is a most general purpose container. It stores the elements in
contiguous memory locations. Any element of vector can be accessed directly
using index of it given by subscript operator [ ]. It supports the dynamic
array. The dynamic array is an array which can grow or shrink dynamically. The
memory allocation for vector is done at the run time.
•
The vector can be declared as
vector <int> v; //It creates a zero length vector
vector<char> v(5);//creates the vector with 5 element character.
vector <double> v2(v1)//creates v2 vector from v1 vector
of double type.
vector <char> v(4, 'a')//initializes 4 element char vector
•
In the program we have to include header file <vector> in the program.
The program for vector implementation is as illustrated below ‒
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> v(10); // create a vector of length 10.
int i;
// display size of vector
cout << "The size of vector = " <<
v.size() << endl;
//store English alphabets to vector
for(i=0; i<10; i++)
v[i] = i + 'A';
// display contents of vector
cout<<"Elements in the vector are:\n";
for(i=0; i<v.size(); i++)
cout << v[i]
<<< " ";
cout << "\n\n";
cout << "Inserting more elements to
vector..."<<endl;
for(i=0; i<5; i++)
v.push_back(i + 10 +
'A');//vector grows
// display size of vector
cout << "New size of vector = "<< v.size()
<< endl;
// display contents of vector
cout<<"Elements in vector are:\n";
for(i=0; i<v.size(); i++)
cout << v[i]
<< " ";
cout << "\n\n";
//deleting last five elements of the list
Standard Template Library
cout<<"\n Now deleting the 5 elements from end of the
vector..."<<endl;
for(i=0;i<5;i++)
v.pop_back();//vector
shrinks
cout<<"\n";
//Retaining the original list
cout << "Elements in vector are:\n";
for(i=0; i<v.size(); i++)
cout << v[i]
<< " ";
cout<<"\n\n";
cout<<"The size is now = "<<v.size();
cout << "\n\n";
retrun 0;
}
Output
The size of vector = 10
Elements in the vector
are:
A B C D E F G H I J
Inserting more
elements to vector...
New size of vector =
15
Elements in vector
are:
A B C D E F G H I J K L
M N O
Now deleting the 5
elements from end of the vector...
Elements in vector
are:
A B C D E F G H I J
The size is now = 10
•
In above program we have declared the char vector of length 10 as
vector<char>
v(10);
•
The vector function v.size() is used to obtain the size of the vector. Then we
inserted some characters in the vector by v.push_back function. By this
function we can insert the element at the end of the vector, i.e. we have
inserted 5 elements after 'A'+10 elements. In other words we have inserted (K,
L, M, N and O after J). Again by using v.size() we can obtain the modified size
of vector.
•
Using v.pop_back() we have deleted the last five elements and retain the
original vector.
•
Thus the above program of vector is for expanding and shrinking the vector.
• Some commonly used vector functions are ‒
Function
‒ Description
begin()
‒ Returns the first element of the
vector.
end()
‒ Returns the last element of the
vector.
size()
‒ Returns the size of the vector.
erase()
‒ Erases the given elements.
push_back()
‒ Insert the element in the vector at
the end.
pop_back()
‒
Deletes the last element of the vector.
resize()
‒ Modifies the original size of the
vector.
Example :
1
Create a vector named
Student to add names of the students in a class. Also display the contents of
the vector after adding necessary elements.
Solution :
#include<iostream>
#include <string>
#include <vector>
using namespace std;
class Student
{
string myName,
myStudentID, myCourse;
public:
Student(string name,
string studentID, string course)
{
myName = name;
myStudentID
studentID;
myCourse = course;
}
void display()
{
cout <<
"\n" << myName << "\t" << myStudentID
<< "\t" << myCourse;
}
};
int main()
{
int n;
vector<Student>
studentList;
cout<<"Enter
Total number of students << endl;
cin >> n;
for (int i = 0; i <
n; i++)
{
string inputName,
inputStudentID, inputCourse;
cout<<"Enter
Student name: ";
cin>>inputName;
cout <<
"Enter Student ID : ";
cin>>inputStudentID;
cout <<
"Enter Course Name: ";
cin>>inputCourse;
Student
obj(inputName, inputStudentID, inputCourse);
studentList.push_back(obj);
cout << endl;
}
cout << "The
student list has a size of " << studentList.size() << endl;
cout << "Name
\t ID \t Course";
for (int i = 0; i <
n; i++)
{
studentList[i].display();
}
cout << endl;
return 0;
}
Output
Enter Total number of
students
3
Enter Student name:
AAA
Enter Student ID: 10
Enter Course Name: Computer
Enter Student name:
BBB
Enter Student ID: 20
Enter Course Name:
Electrical
Enter Student name :
CCC
Enter Student ID: 30
Enter Course Name:
E&TC
The student list has a
size of 3
Name ID
Course
AAA 10
Computer
BBB 20
Electrical
CCC 30 E&TC
•
The stack is a LIFO data structure.
That means the element inserted at the last gets popped off first. The basic
operations that can be implemented on stack are –
1.
Push
2.
Pop
•
Before popping the element it is necessary to check whether the stack is empty
or not. Hence stack empty operation
is also an important operation.
•
Stack is a derived container class which is basically derived from the sequence
container deque.
• Following is a list of operations that are supported by the stack derived container class.
Function ‒ Meaning
push(item)
‒ This function helps to push an item
onto the stack.
pop()
‒ This function pops the topmost element
from the stack.
size()
‒ This function returns the size of the stack.
top()
‒ This function returns value which is at the top of the stack.
•
For using these functions in the program we must include the header file
<stack> at the top. Following is a simple C++ program in which the
derived container class stack is used. Various operations are also performed on
this stack using the inbuilt functions of container class library.
#include<iostream>
#include<stack> //header
file stack must be included
using namespace std;
int main()
{
stack<int> s; //object
is created for stack class
//Note
that this class handles the int values
int item;
char ans;
int choice;
do
{
cout<<"\n
Main Menu";
cout<<"\n
1.Push ";
cout<<"\n
2.Pop ";
cout<<"\n
3.Display ";
cout<<"\n
Enter your choice ";
cin>>choice;
switch(choice)
{
1:
cout<<"\n Enter the element to be pushed ";
cin>>item;
s.push(item);//pushing the element onto the stack
cout<<"\n
Item is pushed!!!";
break;
case 2:
if(!s.empty())//checking stack is empty
or not
{
s.pop();//popping the element from the stack
cout<<"\n
Popped an item!!!";
}
else
cout<<"\n
stack empty!!!";
break;
case
3:if(ls.empty())
{
cout<<"Top
element of the stack is "<<s.top();
// Displaying the stack
top element
}
else
cout<<"\n
The stack is empty!!!";
break;
}
cout<<"\n Do you want to continue?
cin>>ans;
}while(ans= ='y');
return 0;
}
Output
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 1
Enter the element to
be pushed 10
Item is pushed!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display.
Enter your choice 1
Enter the element to
be pushed 20
Item is pushed!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 1
Enter the element to
be pushed 30
Item is pushed!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 3
Top element of the
stack is 30
Do you want to continue?
y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 2
Popped an item!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 3
Top element of the
stack is 20
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 2
Popped an item!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 3
Top element of the
stack is 10
Do you want to
continue? y Main Menu
1.Push
2.Pop
3.Display
Enter your choice 2
Popped an item!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 3
The stack is empty!!!
Do you want to
continue? y
Main Menu
1.Push
2.Pop
3.Display
Enter your choice 2
stack empty!!!
Do you want to
continue?n
•
List is a collection of elements in which only sequential access to the
elements is allowed. This is basically a bidirectional linear list in which we
can traverse the elements from left to right and from right to left. As
bidirectional traversing is possible the insertion and deletion of elements is
efficient.
•
The header file <list> needs to be included in the program.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lst; // create an empty list
int i,n,item;
//storing the elements in the list
cout <<"\n How many elements you want to
insert?"<<endl;
cin>>n;
cout<<"\n Enter the Elements in the
List"<<endl;
for(i=0; i<n; i++)
{
cin>>item;
1st.push_back(item);
}
cout << "The size of list is = " << 1st.size()
<< endl;
//displaying the contents of the list
cout << “The contents of the List are: "<<endl;
list<int>::iterator ptr = lst.begin();
while(ptr ! = 1st.end())
{
cout << ptr
<< " ";//accessing the contents through iterator
ptr++;
}
//sorting the contents of the list
1st.sort();
//displaying the sorted list
cout << "\n\n Sorted elements of the List are:
"<<endl;
ptr = 1st.begin();
while(ptr ! = 1st.end())
{
cout << *ptr
<< " ";
ptr++;
}
// Modifying the List
ptr =1st.begin();
while(ptr != 1st.end())
{
*ptr *ptr + 1000;
ptr++;
}
//displaying the modified list using iterator
cout << "\nModified elements of the list are:
"<<endl;
ptr = 1st.begin();
while(ptr = lst.end())
{
cout << *ptr
<< " ";
ptr++;
}
cout<<"\n\n";
return 0
}
Output
How many elements you
want to insert?
5
Enter the Elements in
the list
30
10
20
40
50
The size of list is =
5
The contents of the
List are:
30 10 20 40 50
Sorted elements of the
list are:
10 20 30 40 50
Modified elements of
the list are:
1010 1020 1030 1040
1050
Program
explanation
•
In above given program, initially the empty list is created by
list<int>
lst;
•
Then using the push_back() function we have inserted the elements in the list
for(i=0; i<n; i++)
{
cin>>item;
1st.push_back(item);
}
•
One iterator is declared and initialized and using which we are trying to
access the contents of the list as follows ‒
list<int>::iterator
ptr = lst.begin();
•
The iterator is given by pointer ptr. The following code is for accessing the
contents of the list.
while(ptr != 1st.end())
{
cout << *ptr
<< " ";
ptr++;
}
•
The list can be sorted simply by invoking Ist.sort() function.
•
Various commonly used functions of list are given in following table ‒
Function ‒ Description
begin()
‒ Returns the first element of the list.
end()
‒ Returns the last element of the list.
size()
‒ Returns the size of the list.
pop_back()
‒ Removes last element of the list.
pop_front()
‒ Removes the first element of the
list.
push_back()
‒ Inserts the specified value at the
end of the list.
push_front()
‒ Inserts the specified value at the
beginning of the list.
reverse()
‒ Reverses the list.
sort()
‒
Sort the contents of the list.
•
The map is an associative container in which the elements are stored in the
form of key value and mapped value. For example ‒ {(1, a),
(2, b), (3, c)}
•
In a map, the key values are generally used to sort and uniquely identify the
elements, while the mapped values store the content associated to this key. The
types of key and mapped value may differ.
•
The duplicate values are not allowed in map. Hence it possess one to one
relationship.
•
The header file <map> is
included in the program for creating and using the map object.
•
The key value and the mapped value are grouped together in member type
value_type which is a pair type as
typedef
pair<const Key, T> value_type;
•
The syntax for creating map is,
map<key,value>
map name
•
Various operations that can be performed on map are ‒
Function name
‒ Purpose
insert(pair)
‒ The element is inserted into the map.
erase(iterator
i) ‒ The element pointed by the iterator is deleted from the map.
void
swap(map) ‒ Swaps the contents of the map.
void
clear()‒ Clears the contents.
size_type size()‒ Returns the size of the
container.
•
The C++ program is as follows
#include <iostream>
#include <map>
using namespace std;
int main()
{
int count,key;
char ans = 'y',item;
int choice;
map<int,char> m;
map<int,char>::iterator i;
do
{
cout<<"\n
Program for Implementing MAP using Associative Container";
cout << "\n
Main Menu";
cout <<
"\n1. Insert an element";
cout <<
"\n2. Delete an element";
cout <<
"\n3. Get the Size of MAP";
cout <<
"\n4. Search an element";
cout<<"\n5.
Display";
cout<<"\n
Enter your choice: ";
cin>> choice;
switch (choice)
{
case 1:cout
<< "\n Enter the element the key: ";
cin >> key;
cout << "\n Enter the value
to be inserted: ";
cin>> item;
m.insert(pair<int,char>(key,item));
break;
case 2:cout
<< "\n Enter the key to be deleted: ";
cin >> key;
m.erase(key);
break;
case 3:count = m.size();
cout << "\n The size of set
is: <<< count;
break;
case 4:cout
<< "\n Enter the Key at for searching the element: ";
cin>> key;
if(m.count(key)!=0) // first represents
key and second represents value
cout << "Element"
<< m.find(key)‒>second << " is present" << endl;
else
cout << "Element is not
present" << endl;
break;
case 5:cout
<< "Elements of MAP are: ";
for (i = m.begin(); i != m.end();
i++)//i is for iterator
cout <<"["
<<(*i).first << ",
"<<(*i).second<<"]";
}
cout << "\n Do you want to continue?(y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
}
Output
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 1
Enter the element the
key: 1
Enter the value to be
inserted: a
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 1
Enter the element the
key: 2
Enter the value to be
inserted: b
Do you want to
continue?(y/n): y
Program for Implementing
MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 1
Enter the element the
key: 3
Enter the value to be
inserted: c
Do you want to
continue?(y/n); y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 1
Enter the element the
key: 4
Enter the value to be
inserted: d
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 5
Elements of MAP are:
[1, a] [2, b] [3, c] [4, d]
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 2
Enter the key to be
deleted: 3
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 5
Elements of MAP are:
[1, a] [2, b] [4, d]
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 3
The size of set is: 3
Do you want to
continue?(y/n): y
Program for
Implementing MAP using Associative Container
Main Menu
1. Insert an element
2. Delete an element
3. Get the Size of MAP
4. Search an element
5. Display
Enter your choice: 4
Enter the Key at for
searching the element: 2
Element b is present
Do you want to
continue?(y/n): n
Implement a Dictionary
named "Index" which consists of Key terms and its Description using
MAP STL. Try to display all the terms and descriptions present in the
dictionary and if a key term has been provided as an input, the corresponding
description should get displayed as an output to the user by searching the
entire dictionary.
Solution :
#include <iostream>
#include<string>
#include <map>
using namespace std;
int main()
{
string key,item;
char ans = 'y';
int choice;
map<string, string> Index;
map<string, string>::iterator i;
do
{
cout << "\n
Main Menu";
cout<<"\n1.
Insert an element";
cout<<"\n2.
Search an element";
cout <<
"\n3. Display";
cout << "\n
Enter your choice: ";
cin>> choice;
switch (choice)
{
case 1:cout
<< "\n Enter the word: ";
cin >> key;
cout<<"\n Enter its meaning:
";
cin >> item;
Index.insert(pair<string,
string>(key, item));
break;
case 2:cout
<< "\n Enter the word for finding its meaning: ";
cin >> key;
if (Index.count(key) != 0) // first
represents key and second represents value
cout << "Meaning is:
" << Index.find(key)‒>second << endl;
else
cout << "Word is not
present" << endl;
break;
case 3:cout
<< "Words in Dictionary: ";
for (i = Index.begin(); i !=
Index.end(); i++)//i is for iterator
cout << "I"
<< (*i).first << ", " << (*i).second <<
"] ";
}
cout << "\n Do you want to continue?(y/n): ";
cin >> ans;
} while (ans = = 'y' || ans = = 'Y');
}
Output
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 1
Enter the word:
attempt
Enter its meaning: try
Do you want to
continue?(y/n): y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 1
Enter the word:
ability
Enter its caning:
capacity
Do you want to
continue?(y/n): y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 1
Enter the word: bad
Enter its meaning:
awful
Do you want to
continue?(y/n): y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 1
Enter the word: calm
Enter its meaning:
quiet
Do you want to
continue?(y/n): y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 1
Enter the word:
dominant
Enter its meaning:
Superior
Do you want to
continue?(y/n); y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 3
Words in Dictionary:
[ability, capacity] [attempt, try] [bad, awful] [calm, quiet] [dominant,
Superior]
Do you want to
continue?(y/n): y
Main Menu
1. Insert an element
2. Search an element
3. Display
Enter your choice: 2
Enter the word for
finding its meaning: calm
Meaning is: quiet
Do you want to
continue?(y/n):
Review Question
1. Explain how the
stack can be implemented using STL.
Data Structures using C PlusPlus: Chapter 8: Standard Template Library : Tag: Data Structure, C++ Programing : Data Structures using C++ Program - Applications of Container Class: Vectors, Stack, List, Map
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation