Here we explore what is considered by many to be the most important feature added to C++ in recent years: the standard template library (STL). The inclusion of the STL was one of the major efforts that took place during the standardization of C++. It provides general-purpose, templatized classes and functions that implement many popular and commonly used algorithms and data structures, including, for example, support for vectors, lists, queues, and stacks. It also defines various routines that access them. Because the STL is constructed from template classes, the algorithms and data structures can be applied to nearly any type of data.(cited from Herbert schlidt).
Lets explore some of the very important features of the STL. In this post lets content ourselves with just counterparts of arrays called vectors . We shall have a look at the basic features of Vectors……
Before that …
STL has three main parts
1.)Container class (that contains the data structures)
i)Sequence container:Stored in sequence. Eg:list,vectors,deque etc;
ii.)Associative container:Each data value is referred by one key value. Eg:Maps
iii.)Adapter Container: Which contains another type of container. Eg: Stack,Queue etc.
2.)Iterators(to access the container)
3.)Algorithms(to manipulate the data structures)
With this back-ground we can start our discussion.Before you do anything you should
#include<vector> in your program
However the beauty of the vectors is that unlike in arrays you can increase the size of it in run time by just using push_back function whenver you want and use the size to get the size then.
Here are a few important functions you ought to know. There are many more and you can visit http://www.cplusplus.com/reference/vector/ for more functions.
1.)clear() erases all the data present in the vector
2.)empty() returns whether the vector is empty or not(true or false)
3.)size() returns the size of the vector at this point of time
4.)resize() v.resize(10,v1) means v has 10 elements and v1’s elements are appended
5.)v.push_back(5) adds the element 5 at the end of vector v
6.)v.pop_back () removes the last element from the vector
By now,this thought must have crossed your mind, how do you create 2-d vectors? Here lies the answer
The above declaration creates a 3*2 array as follows. First it creates v1 , contains 2 elements each of which is 0. Then another vector v2 contains elements of type “vector” i.e. it has 3 vector elements and we initialised each of them to v1.
You can also access them using normal 2d array notation like v2[i][j]….Of course there is another method which you can use to access them, using iterators.
Lets see how we can access a 1d array using iterators. Later we shall extend that to 2d vectors also……
We have a flexibility of having a matrix(2d array) with variable number of columns, I mean, each row of it can have different number of columns. Lets look at one more example.
More or less, thats all the basics you need to pull the remaining yourself. Please comment,if you have any doubts……
Spare a few moments to read this http://www.researchershub.org/2014/01/scheduling-problem-using-greedy-algorithm/