C++ STL powerful guide | Compiled list of popular STL operations

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

  1. Vector container:
    Library - #include<vector>
    This is the most widely used STL container!

    iSr. No.OperationsSyntaxRemarks
    1Creating a vectorvector<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.
    2Inserting an element at the endvec.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
    3Deleting an element from the endvec.pop_back()reduces the vector size by 1 as well
    4Getting an element from the endvec.back()does not reduce the vector size by 1, only returns the reference to the last element present
    5Inserting an element in the frontvec.insert(vec.begin(), element_to_be_inserted)this returns an iterator in this case pointing to the beginning
    6Deleting an element from the frontvec.erase(vec.begin())returns an iterator same as above
    7Getting an element from the frontvec.front()does not reduce the vector size by 1, only returns the reference to the first element present
    8Getting the size of the vectorvec.size()useful in almost every possible use case involving vectors!
    9Knowing if the vector is empty or notvec.empty()used for knowing if the vector is empty
    10Clearing the vectorvec.clear()removes all elements in the vector and are destroyed, making the container to be of size zero.
  2. 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.OperationsSyntaxRemarks
    1Creating a stackstack<type>sttype can be as per requirement, e.g. int, string and user defined data type etc.
    2Inserting an element at the topst.push(element_to_be_inserted)pushes the element to the top of the stack and increases the container size by 1.
    3Deleting an element from the topst.pop()returns the last element present and reduces the stack size by 1 as well
    4Getting an element from the topst.top()does not reduce the stack size by 1, only returns the reference to the top element present at stack
    5Getting size of the stackst.size()this returns the stack size
    6Knowing if the stack is emptyst.empty()returns true if the stack is empty, and returns false otherwise
  3. 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.OperationsSyntaxRemarks
    1Creating a queuequeue<type>qtype can be as per requirement, e.g. int, string and user defined data type etc.
    2Inserting an element in the endq.push(element_to_be_inserted)pushes the element to the end of the current queue and increases the container size by 1.
    3Deleting an element from the frontq.pop()deletes first element present in the queue and reduces the queue size by 1 as well.
    4Getting an element from the frontq.front()does not reduce the queue size by 1, only returns the reference to the element at the front of the queue.
    5Getting an element from the backq.back()does not reduce the queue size by 1, only returns the reference to the last element present in the queue
    6Getting size of the queueq.size()this returns the queue size
    7Knowing if the queue is emptyq.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.
  4. String container
    Library - #include<string>

    iSr. No.OperationsSyntaxRemarks
    1Creating a stringstring str = ""here initialization is optional but preferred in most of the cases and depends on the usage
    2Getting the size of the stringstr.size() / str.length()returns length of a string
    3Knowing the maximum size the string can reachstr.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
    4Test if the string is emptystr.empty()returns true if string is empty, and returns false otherwise
    5Resizing the string to add the charsstr.resize (desired_size,char_to_be_filled)fills the additional characters by the element present in the second argument
    6Clearing stringstr.clear()erases the contents of the string so as to make it empty
    7Swap two stringsswap(string& x, string& y)Exchanges the values stored in two string objects
    8Appending to the stringstr.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
    9Converting string to integerstoi(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.

  1. 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.OperationsSyntaxRemarks
    1Creating a mapmap<key_type , value_type> mphere 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
    2Getting the size of the mapmp.size()returns the number of entries present in the map
    3Testing if the map is emptymp.empty()returns true if the map is empty, false otherwise
    4Inserting a value in mapmp[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
    5Find the iterator of the elementmp.find(key)returns the iterator to the key element present in the map
    6Count the element presentmp.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.
    7Clear the element with specified keymp.erase(itr)this removes entry from the map referenced by the iterator itr, reducing the size of map by 1.
    8Removing all the elements from the mapmp.clear()this removes all the entries, effectively making the map size to be zero
    9Iterating to the first element in the mapmp.begin()this returns the iterator to the first entry in the map
    10Iterating to the last element in the mapmp.end()this returns the iterator next to the last entry in the map and not to the last
    11Getting value of or assigning value to the particular keymp. at(key_val)this returns a reference to the mapped value of the element identified with key k.
  2. 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.OperationsSyntaxRemarks
    1Creating a setset<type> sttype can be as per requirement, e.g. int, string and user defined data type etc.
    2Getting the size of the setst.size()returns the number of entries present in a set
    3Testing if the set is emptyst.empty()returns true if the set is empty, false otherwise
    4Inserting a value in setst.insert(element_to_be_inserted)inserts new element in a set, if not already present
    5Find the iterator of the elementst.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.
    6Count the element presentst.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.
    7Clear the element with specified keyst.erase(itr)this removes entry from the set referenced by the iterator itr, reducing the size of the set by 1.
    8Removing all the elements from the setst.clear()this removes all the entries, effectively making the set size to be zero
    9Iterating to the first element in the setst.begin()this returns the iterator that refers to the first entry in the set
    10Iterating to the last element in the setst.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...

Comments (24)