Standard Template Library
What is STL (Standard Template Library)?
The C++ STL stands for Standard Template Library is a combination of data structures and function such as list,vactor,stack, etc.
The C++ STL is divided into four parts:
- Algorithms
- Containers
- Iterators
- Functions
Algorithms:
- Sorting
- Searching
- Partition operations
Containers:
- Arrays
- Vector
- Stack
- List
The Container classes are used to store the data and objects.
There are many containers such as:
- Array
- Vector
- Set
- Map
- List
- Stack
- Queue
Array
Arrays are a set of similar types of data types.
Code:
#include<iostream>
#include<array>
#include<tuple>
using namespace std;
int main()
{
array<int,6> ar = {3,4,5,6,7,2};
cout << "The array elements are: ";
for ( int i=0; i<6; i++)
cout << ar.at(i) << " ";
cout << endl;
cout << "First element of array is : ";
cout << ar.front() << endl;
cout << "Last element of array is : ";
cout << ar.back() << endl;
cout << "The number of array elements is : ";
cout << ar.size() << endl;
return 0;
}
Output:
The array elements are : 3 4 5 6 7 2
First element of array is : 3
Last element of array is : 2
The number of array elements is : 6
Vector
It is the same dynamic array with the ability to resize itself automatically when an element is inserted and deleted.
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
cout << "\nOutput of rbegin and rend: ";
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << *ir << " ";
cout << "Size : " << g1.size();
g1.resize(4);
cout << "\n After Size : " << g1.size();
cout << "\n" << g1.front();
cout << "\n" << g1.back();
return 0;
}
Output:
Output of begin and end: 1 2 3 4 5
Output of rbegin and rend: 5 4 3 2 1 Size : 5
After Size : 4
1
4
List
A list is a sequence container and the insertion or deletion is fast than vector or arrays.
Code:
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
void showlist(list<int> g)
{
list<int>::iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}
int main()
{
list<int> gqlist1, gqlist2;
for (int i = 0; i < 10; ++i) {
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
}
cout << "\nList 1 (gqlist1) is : ";
showlist(gqlist1);
cout << "\nList 2 (gqlist2) is : ";
showlist(gqlist2);
cout << "\ngqlist1.front() : " << gqlist1.front();
cout << "\ngqlist1.back() : " << gqlist1.back();
cout << "\ngqlist1.pop_front() : ";
gqlist1.pop_front();
showlist(gqlist1);
cout << "\ngqlist2.pop_back() : ";
gqlist2.pop_back();
showlist(gqlist2);
cout << "\ngqlist1.reverse() : ";
gqlist1.reverse();
showlist(gqlist1);
cout << "\ngqlist2.sort(): ";
gqlist2.sort();
showlist(gqlist2);
return 0;
}
Output:
List 1 (gqlist1) is : 0 2 4 6 8 10 12 14 16 18
List 2 (gqlist2) is : 27 24 21 18 15 12 9 6 3 0
gqlist1.front() : 0
gqlist1.back() : 18
gqlist1.pop_front() : 2 4 6 8 10 12 14 16 18
gqlist2.pop_back() : 27 24 21 18 15 12 9 6 3
gqlist1.reverse() : 18 16 14 12 10 8 6 4 2
gqlist2.sort(): 3 6 9 12 15 18 21 24 27
Set
In the set, all the elements are to be unique and the values are stored in a specific order.
Code:
#include <iostream>
#include <iterator>
#include <set>
using namespace std;
int main()
{
set<int, greater<int> > s1;
s1.insert(40);
s1.insert(30);
s1.insert(60);
s1.insert(20);
s1.insert(50);
s1.insert(50);
s1.insert(10);
set<int, greater<int> >::iterator itr;
cout << "\nThe set s1 is : \n";
for (itr = s1.begin(); itr != s1.end(); itr++) {
cout << *itr << " ";
}
cout << endl;
set<int> s2(s1.begin(), s1.end());
cout << "\nThe set s2 after assign from s1 is : \n";
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}
cout << endl;
cout << "\ns2 after removal of elements less than 30 "
":\n";
s2.erase(s2.begin(), s2.find(30));
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}
int num;
num = s2.erase(50);
cout << "\ns2.erase(50) : ";
cout << num << " removed\n";
for (itr = s2.begin(); itr != s2.end(); itr++) {
cout << *itr << " ";
}
cout << endl;
cout << "s1.lower_bound(40) : \n"
<< *s1.lower_bound(40) << endl;
cout << "s1.upper_bound(40) : \n"
<< *s1.upper_bound(40) << endl;
cout << "s2.lower_bound(40) :\n"
<< *s2.lower_bound(40) << endl;
cout << "s2.upper_bound(40) : \n"
<< *s2.upper_bound(40) << endl;
return 0;
}
Output:
The set s1 is :
60 50 40 30 20 10
The set s2 after assign from s1 is :
10 20 30 40 50 60
s2 after removal of elements less than 30 :
30 40 50 60
s2.erase(50) : 1 removed
30 40 60
s1.lower_bound(40) :
40
s1.upper_bound(40) :
30
s2.lower_bound(40) :
40
s2.upper_bound(40) :
60
Map
In Map Container each element has a key and value.
Code:
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<int, int> gquiz1;
gquiz1.insert(pair<int, int>(1, 40));
gquiz1.insert(pair<int, int>(2, 30));
gquiz1.insert(pair<int, int>(3, 60));
gquiz1.insert(pair<int, int>(4, 20));
gquiz1.insert(pair<int, int>(5, 50));
gquiz1.insert(pair<int, int>(6, 50));
gquiz1.insert(pair<int, int>(7, 10));
map<int, int>::iterator itr;
cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second
<< '\n';
}
cout << endl;
map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());
cout << "\nThe map gquiz2 after"
<< " assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second
<< '\n';
}
cout << endl;
cout << "\ngquiz2 after removal of"
" elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second
<< '\n';
}
int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first << '\t' << itr->second
<< '\n';
}
cout << endl;
cout << "gquiz1.lower_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second
<< endl;
cout << "gquiz1.upper_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second
<< endl;
return 0;
}
Output:
The map gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
7 10
The map gquiz2 after assign from gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
7 10
gquiz2 after removal of elements less than key=3 :
KEY ELEMENT
3 60
4 20
5 50
6 50
7 10
gquiz2.erase(4) : 1 removed
KEY ELEMENT
3 60
5 50
6 50
7 10
gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 50
gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
0 Comments