The Standard Template Library or STL provides a huge amount of facility to the coder or programmer. If we create a library, it means that we want to store some data inside the library.
To store the data inside the library, we use to follow some methods. STL helps us to create these libraries by following some methods.
STL Components
STL consists of three components. They are:
- Container
- Iteration
- Algorithm
We discuss each of them in an efficient manner.
A. Container
- Huge amounts of object are kept in the container in a correct manner.
- Various types of data are put inside the container to store the data in a specific way. For example: array, map, linked list, etc.
- These containers are generic in type. The term “generic” is related to the concept of template in C++ which can hold the elements of any data types.
Example: Omitted the colon since this is a sub-heading.
Different types of data are stored in an array dynamically in a vector class. Vector is a class of container categories.
B. Iterators
- Objects are operated in a systematic manner through the iterators.
- It is used to indicate the containers.
- It actually connects the containers and the algorithms in a proper way.
Example:
The arguments of the sort() algorithm are the starting and ending iterator. These iterators help us to sort the elements present in the container as values or data.
C. Algorithm
- Algorithm acts on containers. Through algorithm, we perform the different types of operations like sort, search, data initialize, etc.
- It holds the different types of the by default functions which help to solve many problems related to program.
Example:
We can reverse the data with the help of a by default function named reverse() function, sort the data with the sort() function, etc.
Common Classes that Exists in Containers
- Vector acts on arrays
- Queue acts on queues
- Stacks acts on stacks
- Priority_queue acts on heaps
- List acts on linked list
- Set acts on trees
- Map acts on arrays
Classification of Containers
There are 4 container like Unordered Associative Containers, Container Adapters, Sequence Containers, and Associative Containers.
STL is a vast concept as it has many classes. We will discuss some important classes in STL.
Array:
- Array is a linear collection of the similar types of elements.
- We use the header array– #include<array>.
- Here, the array is static in type.
Programming Example 1:
#include <array>
using namespace std ;
int main()
{
array< int, 5 >data_array = {5, 10, 15, 20, 25}; // creating array object ;
data_array.fill (10) ; // member function is used ;
for (int i=0;i<=7;i++)
cout<< data_array[ i ] ;
return 0 ;
}
Output:
Programming Example 2:
#include <array>
using namespace std ;
int main()
{
array< int, 5 >data_array = {5,10,15,20,25} ; // Creating array object ;
cout<< data_array.at(2) ;
cout<< data_array[3] ;
cout<< data_array.front() ; // member function is used ;
cout<< data_array.back() ; // member function is used ;
return 0 ;
}
Output:
Explanation:
The previous two programs are the example of the array class.
Here, we declare an array. We store some values like 5, 10, 15, 20, and 25 inside an array.
The (), front(), back(), and fill() are important member functions that are used in the array template.
Vector Classes in STL
- The most general purpose container is the vector class.
- It supports the Dynamic array.
Declaration Types of Vectors
1. vector < int > v1 ;
Zero length vectors.
2. Vector < char> cv (5);
Creates the 5 elements char vector.
3. vector < char > cv ( 4, ‘ a ’ );
Initializes 4 elements char vector with “a”.
Programming Example 3:
#include <vector>
using namespace std ;
int main()
{
vector <int> v1 ;
vector<char> v2( 4 ) ;
vector<int> v3( 5,10 ) ;
vector<string> v4(3," hello ") ;
cout<<v4[0] << endl ;
cout<< v4[1] << endl ;
cout<<v4[2] << endl ;
return 0 ;
}
Output:
Explanation:
In the program, we declare the vectors v1, v2, v3, and v4. We assign some values at the time of declaration inside the vectors v2, v3, v4. Now, we print the values of 0, 1st and 2nd index of v4. All of them show the same result.
Programming Example 4:
#include <vector>
using namespace std;
int main()
{
vector< int >v1 ;
cout<< " Current Capacity is " <<v1.capacity()<< endl ;
for (int i=0; i<= 9; i++)
v1.push_back(10*(i+1)) ;
cout<< " Current capacity is " < <v1.capacity() <<endl ;
v1.pop_back() ;
cout<<" after pop \n" ;
cout<< " Current Capacity is "<< v1.capacity() <<endl ;
v1.pop_back() ;
cout<< "Current Capacity is "<<v1.capacity() << endl ;
v1.pop_back() ;
cout<< " Current Capacity is " << v1.capacity() <<endl ;
cout<< " total number of elements are " <<v1.size() << endl ;
}
Output:
Explanation:
Here in the program, we use some member functions. One of them is the push_back which is used to add value to the vector at the end.
Another member function is pop_back().The last element is removed.
Another function which is used in the program is capacity(). It returns the capacity of the vector.
The last but not the least is the size() which determines the size of the data.
Conclusion
In the concept of STL, we have come to this conclusion that STL is a vast concept in C++. By using the STL, we can use the standard code to make the different data structure like stack, queue, and link list. We also make some operations on the data like searching, sorting, etc.