Skip to main content

C++ STL (Containers) Tutorial For Beginners Learn STL Hacks Part -1

So, As I said that I will be writing in the next post about STL hacks and tricks so here I am with this post.


/*

Important Note :

STL: Standard Library  ( To Import It Use: #include<bits/stdc++.h> )
Container types (and algorithms, functors, and all STL as well) are defined not in the global namespace, but in a special namespace called “std.” 
Add the following line after your includes and before the code begin:
using namespace std;
otherwise use:  std::'container'
*/

You must have g++ compiler installed on your PC for using it.
STL is mostly used by intermediate users of C++ and it is quite helpful in Competitive Coding, it saves a lot of time and all the algorithms are implemented with the best possible Time and Space Complexities available.

STL is quite vast and we'll be step by step covering all of it, this post is a brief intro of :


Containers 


  • Vector 
  • Pairs 
  • Iterators   
  • Set


CONTAINERS

In native C (not C++) there was only one type of container: the array. The problem is not that arrays are limited (though, for example, it’s impossible to determine the size of the array at runtime). But the required functionality of containers is a lot more than we've seen in arrays for example :

  1. To add data dynamically
  2. To iterate through the container
  3. To erase any item in the container
  4. To find an item in the container, etc.

VECTOR

The simplest STL container is a vector. A vector is just an array with extended functionality.
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed sequentially. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.

PAIR

This container couples together with a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second. STL std::pair is just a pair of elements.

ITERATOR

In STL iterators are the most general way to access data in containers. An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators.
Iterators play a critical role in connecting algorithm with containers along with the manipulation of data stored inside the containers. The most obvious form of the iterator is a pointer. A pointer can point to elements in an array and can iterate through them using the increment operator (++).




Operations on iterators:-
1. begin() :- This function is used to return the beginning position of the container.
2. end() :- This function is used to return the after end position of the container.


SET

Set is a container that stores only unique elements in a specific order (by default: ascending ). Set can add, remove, and check the presence of a particular element in O(logN), where N is the count of objects in the set. While adding elements to set, the duplicates are discarded. A count of the elements in the set, N, is returned in O(1). Elements of a set are not contiguous therefore are accessed using Iterators.

To run this code and check it's output visit:
https://www.codiva.io/p/84952a2b-403d-4758-9dad-6c9b5967d828
Stay Tuned For Upcoming Posts on STL subscribe to get updates.
Happy Coding!!














Comments

Post a Comment

Your Feedback Will Help Us To Improve...

Most Popular Posts

Coding and Mathematics ( Importance Of Maths In Programming )

Everyone wants to become a good coder but only a few manage to achieve this you know why? BECAUSE everyone wants to code and no one really wants to be a good mathematician which is the basic requirement to be a coder, yet very few know this fact and try to be a  mathematician first than a coder. What exactly is coding? It is the process of solving different technical problems using a programming language. The technical problems can vary from printing a  simple "Hello World" program to solving "2 SAT Problem" using mathematics. You might today be able to solve a few problems using your technical skills but it would help you in the long run only if you're fundamentals of mathematics are also clear. By mathematics, I don't mean the whole of it but some important topics such as Discrete Math, Algebra, Number Theory, etc. Here are some concepts of mathematics that you will use mostly in solving the technical problems : Fundamentals B...

On Which Coding Platform Should I Practice?

Which is the best online platform to practice, how to improve my coding skills? This post is all about giving answers to all such questions. If you're a beginner who has got no idea what programming is then I would recommend you step by step practice any programming language on Hackerrank. Hackerrank has modules that are self-explanatory and would slowly make you understand all the terminologies which are generally used and with practice, you'll be better at it. Moreover, if you're still not able to understand anything you can always search for your answers on GeekForGeeks or HackerEarth both of them provide one of the best tutorials on any topic. If you're an intermediate who's comfortable with any programming language then you can always improve your problem-solving skills studying Data Structures and Algorithms and practicing it. You can also improve by competitive coding. The best place to start with competitive programming according to me would be ...

How to Invert A Binary Tree (Iteratively + Recursively)?

Inverting a binary is one of the most common questions being asked these days. Often, seen many times in the ad of AlgoExpert as well. So, what is an inverted binary tree? Inverted binary tree is a simply a mirror image of given binary tree. Example : So, all we need to do is " Swapping left children with right children of a binary tree at every level." Implementation Recursively TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; swap(root->left,root->right); invertTree(root->left); invertTree(root->right); return root; } Iteratively TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; queue<TreeNode*> q; q.push(root); while(!q.empty()) { TreeNode* c= q.front(); q.pop(); swap(c->left,c->right); if(c->left!=NULL) q.push(c->left); if(c->right!=NULL) q.push(c->right); } return root; } I hope this tutorial hel...