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.
/*
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 :
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.
/*
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 :- To add data dynamically
- To iterate through the container
- To erase any item in the container
- To find an item in the container, etc.
VECTOR
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:-
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.
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!!
Awesome post!
ReplyDeleteThank You....!!
Delete