stl of c++

Authentic STL of c++ guide, Algorithms,Iterator’s,Container’s class’s

The standard template library (STL )   is a collection of C++ template  classes for data structures and algorithms

It is impossible to learn c++ (STL) without proper working knowledge  of data structure  and algorithms

Therefore, a novice has to learn first DSA and algorithms  to study STL in C++

During competitive programming where time is a valuable asset  at this instant, you can use STL  to overcome the problem of writing function again and again

STL library has three components

  1. Algorithms
  2. Container’s
  3. Iterator’s

Algorithms

there are build in algorithms available in STL C++

Ex: sort(); set_union(); binary_search(); sort_heap(); push_heap(); pop_heap(); make_heap(); merge_heap(); reverse(); copy();

Example of  a sort algorithm in STL
#include <iostream>
#include <algorithm>

using namespace std;

void show(int A[])
{
    for(int i = 0; i < 8; i++)
        cout << A[i] << " ";
}

int main()
{
    int A[8]= {1, 5, 6, 7, 3, 4, 2, 0};
    sort(A, A+8);//sort algorithm
    show(A);

    return 0;

}
Output
0 1 2 3 4 5 6 7

Iterator’s

Iterator’s are available in every collection

First method

vector<int>:: iterator itr;
    for(itr=v.begin();itr!=v.end();itr++)
        cout<<*itr<<endl;

Second  method

for(int x:v)
        cout<<x<<endl;

Container’s class’s

Ex: vector, forward_list, list,

deque,priority_queue, stack,

set, multiset

Vector class contain a different types of member functions

Ex: push_back(), pop_back(), insert(), remove(), size(), empty()

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //Vector of type integer
//V is an object and it is initialized with 1,2,3,4,5

    vector<int> v ={1,2,3,4,5};
    v.push_back(6);//Push_back member function of vector class
    v.push_back(7);
    //iterator class belong to a vector class
    //itr is an object of iterator

    vector<int>::iterator itr;
    cout<<"First iterator  method" << endl;
    for(itr=v.begin();itr!=v.end();itr++)////First iterator  method
        cout<<*itr<<endl;
    cout<<"Second  iterator by each loop method"<<endl;
    for(int x:v)//Second  iterator by each loop method
    cout <<x<<endl;
    return 0;
}

Output

First iterator  method
1 2 3 4 5 6 7
Second  iterator by each loop method
1 2 3 4 5 6 7

vector class

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> v={1,2,3,4,5,6,7};
    v.push_back(8);
    v.push_back(9);
    for(int x:v)
        cout<<x<<endl;
    return 0;
}

Output

1 2 3 4 5 6 7 8 9

List class

#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> v={1,2,3,4,5,6,7};
    v.push_back(8);
    v.push_back(9);
    for(int x:v)
        cout<<x<<endl;
    return 0;
}

Output

1 2 3 4 5 6 7 8 9

NOTE:  you can use other container class’ by including header file and declaring its class, every container class have a different member function s

 for an instant, forward_list class won’t have push_back member function, instead, it has push_front  member function

related posts

  1. error : in c++98 ‘v’ must be initialized by constructor, not by ‘{….}’
  2. Algorithm examples to illustrate the algorithm concept
  3. What is Data Structure?
  4. Introducing the basic in C programming

Mohammed Anees

Hey there, welcome to aneescraftsmanship I’m Mohammed Anees an independent developer/blogger.

Leave a Reply

Your email address will not be published.