Skip to main content

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

In continuation of my STL tutorial series, I'll be introducing to some more STL containers in this post.


The containers I shall be covering include:

CONTAINERS

  • String Streams
  • Map
  • Multiset
  • Priority Queue


String Streams

Stringstream is the processing of input/output strings. The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings.
By treating the strings as streams we can perform extraction and insertion operation from/to string just like cin and cout streams.
C++ provides two interesting objects for it: ‘istringstream’ (input stringstream ) and ‘ostringstream’ (output stringstream). Both of these are declared in #include< stream >.


Object istringstream allows you to read from a string like you do from standard input. The ostringstream object is used to do formatting output.

Insertion And Extraction Operations Using stringstream

Insertion
(i) Using Insertion Operator (<<)
Data is assigned to stringstream buffer using << operator.

(ii)Using str(string) Function
Str function can also be used for assigning data to the stringstream buffer. The str function takes the data string as an argument and assigns this data to the stringstream object.

Extraction
(i) Using Extraction Operator (>>)
Stringsream data can be displayed using >> operator.

(ii)Using str() Function
We can use the str() function to get the data out of stringstream.

Demonstration of Insertion and Extraction Operators :


Click Here Run and Check The Output Of Above Code

Map

Map is quite similar to set, except it contains not just values but pairsv<key, value>. Map ensures that at most one pair with specific key exists. Therefore, a map contains keys in a specific order and all the keys are unique with some value.Internally map and set are almost always stored as red-black trees.
Elements of map and set are always sorted in ascending by default.
Traversing map is easy with iterator, here iterator will be a pair of key and value. So, to get the value use ( it->first )  or ( it->second ).

Some Built-In Functions
  • begin(): Returns an iterator to the first element in the map.
  • size(): Returns the number of elements in the map.
  • empty(): Returns a boolean value indicating whether the map is empty.
  • insert( pair(key, value)): Adds a new key-value pair to the map. An alternate way to insert values in the map is:map_name[key] = value;
  • find(val): Gives the iterator to the element val, if it is found otherwise it returns m.end()
  • erase(iterator position): Removes the element at the position pointed by the iterator.
  • erase(const g): Removes the key value g from the map.
  • clear(): Removes all the elements from the map.
Implementation :



Click Here To Run the above code and check it's output

Multiset

Multisets are containers that store elements following a specific order, and where multiple elements can have equivalent values or there can be duplicate elements in it.
Implementation :


Click Here To Run & Check Output

Priority Queue

Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest/smallest of the elements it contains, according to some strict weak ordering criterion.
Priority Queue is used mostly in questions where the order of elements is either strictly increasing or decreasing. Insertions take place in a priority queue using push() function and deletion take place using pop(). 
Greatest/Smallest value element is always present on the top of the priority queue and pop() also deletes the element present on the top of priority queue.
Implementation


Click Here To Run & Check Output

This is not the end of STL but the beginning... stay tuned for more stuff to come.
Happy Coding..!!

Comments

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