If you like this post, please do upvote.
For a long time now, since I have been a user of C++, I've always wanted to read all the info related to C++ STL in one place and so I've decided to write this post so that it is useful resource for people like me in the future.
(Note: This is not the whole guide, as STL is a vast topic to cover but it can be used by beginners as a starting point to know what different operations are possible on widely used STL containers and depending on the response and demand I would like to add detailed examples to this guide!:)
Let's dive straight into our topic of discussion i.e. the mighty C++ STL
Vector container:
Library - #include<vector>
This is the most widely used STL container!
| iSr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a vector | vector<type>vec(desired_size,initial_val) | bracket values are optional and type can be as per requirement, e.g. int, string and user defined data type etc. |
| 2 | Inserting an element at the end | vec.emplace_back(element_to_be_inserted) | this increases the container size by 1 and vec.push_back(value_to_be_inserted) can also be used but earlier should be preferred as it is faster and you can always read more on this |
| 3 | Deleting an element from the end | vec.pop_back() | reduces the vector size by 1 as well |
| 4 | Getting an element from the end | vec.back() | does not reduce the vector size by 1, only returns the reference to the last element present |
| 5 | Inserting an element in the front | vec.insert(vec.begin(), element_to_be_inserted) | this returns an iterator in this case pointing to the beginning |
| 6 | Deleting an element from the front | vec.erase(vec.begin()) | returns an iterator same as above |
| 7 | Getting an element from the front | vec.front() | does not reduce the vector size by 1, only returns the reference to the first element present |
| 8 | Getting the size of the vector | vec.size() | useful in almost every possible use case involving vectors! |
| 9 | Knowing if the vector is empty or not | vec.empty() | used for knowing if the vector is empty |
| 10 | Clearing the vector | vec.clear() | removes all elements in the vector and are destroyed, making the container to be of size zero. |
Stack container:
Library - #include<stack>
Another popular container that makes our lives easier as many famous interview questions are based on the usage of stack... :)
| iSr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a stack | stack<type>st | type can be as per requirement, e.g. int, string and user defined data type etc. |
| 2 | Inserting an element at the top | st.push(element_to_be_inserted) | pushes the element to the top of the stack and increases the container size by 1. |
| 3 | Deleting an element from the top | st.pop() | returns the last element present and reduces the stack size by 1 as well |
| 4 | Getting an element from the top | st.top() | does not reduce the stack size by 1, only returns the reference to the top element present at stack |
| 5 | Getting size of the stack | st.size() | this returns the stack size |
| 6 | Knowing if the stack is empty | st.empty() | returns true if the stack is empty, and returns false otherwise |
Queue container
Library - #include<queue>
Queue can be greately used in the problems related to graphs and hence this should not be ignored as well.
| iSr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a queue | queue<type>q | type can be as per requirement, e.g. int, string and user defined data type etc. |
| 2 | Inserting an element in the end | q.push(element_to_be_inserted) | pushes the element to the end of the current queue and increases the container size by 1. |
| 3 | Deleting an element from the front | q.pop() | deletes first element present in the queue and reduces the queue size by 1 as well. |
| 4 | Getting an element from the front | q.front() | does not reduce the queue size by 1, only returns the reference to the element at the front of the queue. |
| 5 | Getting an element from the back | q.back() | does not reduce the queue size by 1, only returns the reference to the last element present in the queue |
| 6 | Getting size of the queue | q.size() | this returns the queue size |
| 7 | Knowing if the queue is empty | q.empty() | returns true if the queue is empty, and returns false otherwise |
| Drawback of this container is, it does not allow the operations such as inserting elements at the front and removing elements from the end of the queue, for this purposes "dequeue" STL container can be useful. |
String container
Library - #include<string>
| iSr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a string | string str = "" | here initialization is optional but preferred in most of the cases and depends on the usage |
| 2 | Getting the size of the string | str.size() / str.length() | returns length of a string |
| 3 | Knowing the maximum size the string can reach | str.max_size() | returns the maximum length that the string can reach depending on the system limitation, this function is not widely used though in problem solving |
| 4 | Test if the string is empty | str.empty() | returns true if string is empty, and returns false otherwise |
| 5 | Resizing the string to add the chars | str.resize (desired_size,char_to_be_filled) | fills the additional characters by the element present in the second argument |
| 6 | Clearing string | str.clear() | erases the contents of the string so as to make it empty |
| 7 | Swap two strings | swap(string& x, string& y) | Exchanges the values stored in two string objects |
| 8 | Appending to the string | str.append(str2) | Appends to the end on str, str2 and many different use cases are possible depending on the position we would like to append the string to |
| 9 | Converting string to integer | stoi(str) | returns an integer value correspoding to the string, used whenever we would like to convert any number present in the form of string to integer form, another operations are present as well for converting to other data types from string, and you can always read more about them |
This container also supports use of '+' operator, for e.g. if str=""; and str+="demo"; then str becomes "demo" giving us the power to mutate strings to solve the problems in a much faster and efficient manner.
Map container
Library - #include<map>
Must know STL container and to be well rehearsed as well for getting interview ready if you choose C++ as your language!
| Sr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a map | map<key_type , value_type> mp | here the key and values should be inserted simultaneously, only insertion of keys or values is not allowed although values for all the keys are initialized to be null or zero depending on their type |
| 2 | Getting the size of the map | mp.size() | returns the number of entries present in the map |
| 3 | Testing if the map is empty | mp.empty() | returns true if the map is empty, false otherwise |
| 4 | Inserting a value in map | mp[key]=value_to_be_assigned / mp.insert(pair<key_type,value_type>(key_to_be_inserted,value_for_the_key)) | inserts new key-value pair in the map |
| 5 | Find the iterator of the element | mp.find(key) | returns the iterator to the key element present in the map |
| 6 | Count the element present | mp.count(key) | returns the count of the elements with specific key, because all elements in a map container are unique, the function can only return 1 or 0, depending upon whether the element is present in the map or not. |
| 7 | Clear the element with specified key | mp.erase(itr) | this removes entry from the map referenced by the iterator itr, reducing the size of map by 1. |
| 8 | Removing all the elements from the map | mp.clear() | this removes all the entries, effectively making the map size to be zero |
| 9 | Iterating to the first element in the map | mp.begin() | this returns the iterator to the first entry in the map |
| 10 | Iterating to the last element in the map | mp.end() | this returns the iterator next to the last entry in the map and not to the last |
| 11 | Getting value of or assigning value to the particular key | mp. at(key_val) | this returns a reference to the mapped value of the element identified with key k. |
Set container
Library - #include<set>
Library to get your hands down with, because this library lets us handle trickier algorithms and data structures problems with grace.
| Sr. No. | Operations | Syntax | Remarks |
|---|---|---|---|
| 1 | Creating a set | set<type> st | type can be as per requirement, e.g. int, string and user defined data type etc. |
| 2 | Getting the size of the set | st.size() | returns the number of entries present in a set |
| 3 | Testing if the set is empty | st.empty() | returns true if the set is empty, false otherwise |
| 4 | Inserting a value in set | st.insert(element_to_be_inserted) | inserts new element in a set, if not already present |
| 5 | Find the iterator of the element | st.find(element) | searches for the element in the set and returns the iterator to the element if present in the container, otherwise returns the iterator to the end of the set. |
| 6 | Count the element present | st.count(element) | returns the count of the elements, because all elements in a set container are unique, the function can only return 1 or 0, depending upon whether the element is present in the set or not. |
| 7 | Clear the element with specified key | st.erase(itr) | this removes entry from the set referenced by the iterator itr, reducing the size of the set by 1. |
| 8 | Removing all the elements from the set | st.clear() | this removes all the entries, effectively making the set size to be zero |
| 9 | Iterating to the first element in the set | st.begin() | this returns the iterator that refers to the first entry in the set |
| 10 | Iterating to the last element in the set | st.end() | this returns the iterator next to the last entry in the set and not to the last |
P.S. - please do not forget to add semicolon at the end of these syntaxes!
Also, valuable comments and feedback from readers are always welcomed...