Standard Template Library in C++

vector<int> LET_S;
pair<string, int> GET;
list<int> STARTED;
array<int, 3> WITH;
map<int, string> STL;

Still don’t know what these statement do?
Let’s read this node to head start your journey in STL.

Standard Temp…


This content originally appeared on DEV Community and was authored by Abhishek Chandra

vector<int> LET_S;
pair<string, int> GET;
list<int> STARTED;
array<int, 3> WITH;
map<int, string> STL;

Still don't know what these statement do?

Let's read this node to head start your journey in STL.

Standard Template Libraries are powerful set of C++ template classes. It contains numerous pre-defined classes and are used to make programming easier.

Consider Standard Template Libraries as a helping hand for not writing codes for implementing Data Structures like Linked Lists, Stacks, Queues, Trees etc. in every program we require them (mostly in competitive programming questions), we can use these data structures by just including a library and using a pre-defined syntax.

Also, classes in Standard Template Libraries are made through template classes so these are generic in nature.

At the core of the C++ Standard Template Libraries, these are the following three well structured components.

  • Containers.
  • Algorithms.
  • Iterators.

Containers

  • Containers are used to manage and store collection of objects of a certain kind.
  • Container Library in STL provide containers that are used to create data structure like arrays, linked-list, trees, etc.
  • Containers helps us to implement and replicate simple and complex data structures very easily like array, list, trees, associative arrays and many more.
  • These containers are generic in nature, they can hold elements of any data types.

Common Containers that replicates simple and complex data structures

  • vector : replicates arrays
  • queue : replicates queues
  • stack : replicates stack
  • priority_queue : replicated heaps
  • list : replicates linked list
  • set : replicates trees
  • map : associative arrays

Classification of containers

  • Sequence Containers : These containers hold linear storage of data, like arrays, linked list etc.
  • Associative Containers : Ordered containers like map, multimap, set, multiset are associative containers. (Ordered means the values are stored in the respective container in the same order as inputted by the user)
  • Unordered Associative Containers : These are similar to associative containers but have different constraints, the elements are stored unordered due to the use of hash tables while implementing these containers.

How to use containers?

When we use containers to implement data structures, we just have to include a header file and use the respective syntax to initialise the data structure you need.

#include<bits/stdc++.h>

or you can just include a specific container header file,

#include<iostream>

// importing vector header file to implement vectors
#include<vector>

int main(){
 vector<int> first_vector;
 return 0;
}

// vector can be used for creating dynamic arrays of `char`, `int`, `float` and other types.

#include<iostream>

//  importing list header file to implement lists
#include<list>

int main(){
 list<int> my_list;
 return 0;
}

Algorithms

  • Algorithms act on containers. They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of container.
  • Algorithms Library contains built-in functions that perform complex algorithms on the data structures.

Example:

  • We can reverse a range with reverse() function, sort a range with sort() function, search on a range with binary_search() and so on.

  • Algorithm Libraries provide abstraction, i.e. you don't necessarily need to know how the algorithm works.

Sorting of a vector

#include<bits/stdc++.h>

using namespace std;

int main(){
 vector<int> second_vector {44,22,66,33,99};

// begin() and end() method returns an iterator to the vector.
 sort(second_vector.begin(), second_vector.end());
 return 0;
}

/* 
Above code will sort the vector to {22,33,44,66,99}.
More about vector will be covered in a separate node.
*/

Iterators

  • Iterators are used to step through the elements of collection of objects. These collections can be containers or subsets of containers.
  • Iterators in Standard Template Libraries are used to point to the containers.
  • Iterators actually acts as a bridge b/w containers and algorithms.

Let's look at the above code in algorithms section for reference to iterators,

sort() algorithm or function have two parameters, starting iterator (vector.begin()) and ending iterator (vector.end()) pointing to first and last element in the vector respectively, now sort() compares the elements pointed by each of these iterators and arrange them in sorted order, thus it does not matter what is the type of the container and same sort can be used on different types of containers.

How to declare an iterator?

Syntax

// This is an iterator of type vector
vector<int> :: iterator itr; 
// itr is the iterator

Example use of iterator:

Printing elements of a vector

#include<bits/stdc++.h>

using namespace std;

int main(){
    vector<int> third_vector {10,20,30,40,50};
    vector<int> :: iterator itr;
    for(itr = third_vector.begin(); itr != third_vector.end(); itr++){
        cout<<*itr<<" ";
    }
    return 0;
}

/*
OUTPUT : 10 20 30 40 50 
*/

Visit this to view some Standard Template Libraries programs.


This content originally appeared on DEV Community and was authored by Abhishek Chandra


Print Share Comment Cite Upload Translate Updates
APA

Abhishek Chandra | Sciencx (2021-06-10T17:39:20+00:00) Standard Template Library in C++. Retrieved from https://www.scien.cx/2021/06/10/standard-template-library-in-c/

MLA
" » Standard Template Library in C++." Abhishek Chandra | Sciencx - Thursday June 10, 2021, https://www.scien.cx/2021/06/10/standard-template-library-in-c/
HARVARD
Abhishek Chandra | Sciencx Thursday June 10, 2021 » Standard Template Library in C++., viewed ,<https://www.scien.cx/2021/06/10/standard-template-library-in-c/>
VANCOUVER
Abhishek Chandra | Sciencx - » Standard Template Library in C++. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/10/standard-template-library-in-c/
CHICAGO
" » Standard Template Library in C++." Abhishek Chandra | Sciencx - Accessed . https://www.scien.cx/2021/06/10/standard-template-library-in-c/
IEEE
" » Standard Template Library in C++." Abhishek Chandra | Sciencx [Online]. Available: https://www.scien.cx/2021/06/10/standard-template-library-in-c/. [Accessed: ]
rf:citation
» Standard Template Library in C++ | Abhishek Chandra | Sciencx | https://www.scien.cx/2021/06/10/standard-template-library-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.