Learning STL

I have been coding in C++ since school years and have used C++ and STL sparingly in my 2 years in college.
In the past, I have also felt an irritation due to my code for a specific problems becoming too long, or some functionalities of it becoming a hassle for me, because of which I would leave it in between.

Therefore, I had been feeling motivated to learn STL since a long time. Since I list C++ as a skill, it was becoming pointless to enlist it as a programming language I knew, without being strong in the usage of STL.

Today I just felt motivated to start learning about STL, it’s containers and its various uses. I decided to take the help of TopCoder tutorials in doing that, and with just a few hours of hacking around, I was able to build a significant amount of confidence in my abilities to use STL containers, which is enough to propell me to learn STL further.

As of now, I haven’t learnt much, just the basics of vectors, pairs, iterators and some introduction to sets and maps.
The tutorials I followed were:-

https://www.topcoder.com/community/data-science/data-science-tutorials/power-up-c-with-the-standard-template-library-part-1/

Simpler one – https://community.topcoder.com/tc?module=Static&d1=features&d2=082803

Also, I really couldn’t have learnt anything untill I had applied it with my own hands, so side by side I programmed as well. For now, I have only written code for the basic vector functionalities, which I am sharing here for if anyone wants to follow my approach.

//Implementing vectors
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

//A vtor is basically an array list
int main()
{
vector<int> a;
//vector<int>::iterator=t;

//Empty vector

vector<int> v(10,5); //A vector of length 10, initialised with 5
for(int i=0;i<10;i++)
cout<<v.at(i)<<‘ ‘;

cout<<‘\n’;
for (int i =0; i<10; i++)
{

v[i] = (i+1)*(i+1);
cout<<v[i]<<‘ ‘;

}
cout<<‘\n'<< v.size()<<‘\n’; //To get the size of vtor
cout<<v.empty(); //To check if the vtor is empty, returns 0 if it is not. 0- false

//push_back adds an element to the end of the vtor
for(int i=0;i<10;i++ )
v.push_back(i+1);

cout<<‘\n'<<v.size(); //Now the size becomes 20 after adding 10 elements

for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;

v.resize(20);

cout<<‘\n'<<v.size();

cout<<‘\n’;
for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;

//clear() function will make the vector to contain 0 elements. It does not make the elements 0, it completely erases the container.

cout<<“\nhello”<<v.end()-v.begin()<<‘\n’;
//Creating a vector by initialising with another vector
vector<int> v2=v;
vector<int> v3(v2);
//Both statements will work
//We can create a vector with anything
vector<string> names(20,”unknown”); //Vector initialised with 20 “unknowns”

for(int i=0;i<10;i++)
cout<<names[i]<<‘ ‘;

vector< vector<int> > Matrix;
//Constructing a 2-D array via vectors, i.e, a vector of vectors
sort(v.begin(),v.end());
for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;

cout<<‘\n’;

cout<<v.size();
cout<<‘\n’;
v.pop_back(); //removes the last element, doesn’t return it

cout<<v.size()<<‘\n’;
//size reduced by one after popping the last element

reverse(v.begin(),v.end());
for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;

cout<<‘\n’;
vector<int>::iterator t; //declaring an iterator
t=v.begin(); //initialise iterator with vector.begin()
v.insert(t+10,42);
//insert only takes the index as an iterator, so any integer value will not do

for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;
//to delete the 4th element
t=v.begin();
v.erase(t+3);

cout<<‘\n’;

for(int i=0;i<20;i++)
cout<<v[i]<<‘ ‘;

v.clear(); //used to clear all the elements in the vector

return 0;
} //end of main

This is just a dirty and quick implementation for vectors to understand the basic concepts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s