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

100+ STL Algorithms, "I Bet If You Know Even 20 Out of These..." ( C++ 17 )

So, In the previous posts, I've been telling you about the various containers in STL and their implementation. I left a few of them purposely such as unordered_map, unordred_set, etc. so that you also do some homework. This post will entirely be about various functions available in STL and it would be your task to completely code & implement them. Do tell me if you have any doubts related to it in the comment section below. Some common Data Structures Implementation In STL : Stack Syntax : stack<data_type> stack_name Example  : stack<int> st; Built-in functions such as top() ,  push() , pop() , empty() , size() etc. are available in STL for Stack. Queue Syntax :  queue<data_type> queue_name Example  :  queue<int> q; Built-in functions such as push() , pop() , empty() , size() , front() , back() are available in STL for Queue. List ( Doubly Linked List ) Syntax :  list<data_type> list_name Example  :  list<int> l;

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

Left & Right View Of a Binary Tree Made Easy!

When talking about binary trees then the view of a binary tree is something that has the most probability to be asked in a coding interview. In this blog post, I'm going to talk mainly about the Right & Left views of a binary tree. There are so many ways available on the internet, that it's really a very difficult task to choose which method you should follow. Suppose, you're using one method for Left View and another method for Right View, now you need to know two different methods for two similar questions. In this blog we'll discuss a single approach by which you can solve both Left View and Right View problems.   Before diving directly into the approach we need to understand what exactly is this Right/Left view. For your better understanding I've created a binary tree : So let's try to understand the right view for this binary tree. Right View simply means looking at the tree from its right side and the only nodes visible from that view are required to b