Monday, May 20, 2013

STL in a nutshell

STL or Standard Template Library is a C++ library which provides many popular data structures and algorithms. So that when you program you don't have to write the implementations from scratch and can code faster during competitions.

STL has a lot of things but you don't use everything , so here is the basic functions which you will need to know most of the time. You can always check cplusplus.com for getting complete information on any topic.

VECTORS:

It is similar to the linked list. Elements are added to the end(by default) of the list and the elements can be easily accessed using index address like arrays.
Header file: vector

#include < vector >
using namespace std;

int main()
{
	int i;
	vector<int> vec;
	for(i=0; i<5; i++)
	{
		vec.push_back(i);			//0 1 2 3 4
	}
	for(i=0; i<vec.size(); i++)
	{
		if(i&1)
			vec[i]+=10;			//0 11 2 13 4
	}
	vec.pop_back();					//0 11 2 13
	vec.erase(vec.begin() + 1);			//0 2 13
	return 0;
}

In the above example a vector of type "int" is created and can be replaced by any datatype ( even structures, classes or vectors itself)
The commonly used functions are:
  • push_back(a) : inserts an element at the end of the vector.
  • size() : returns the no of elements in the vector.
  • pop_back() : deletes the last element.
  • erase( pointer ) : deleted the element at given pointer.
  • begin() / end() : returns the pointer to the begin / end of the vector. (Note that the last element is vec.end() -1 ).
  • clear() : removes all the elements in the vector.

SETS :

It is similar to the Binary Search Tree. The insertion and searching is done in logarithmic time.
Header file: set

#include <set>
#include <iostream>
using namespace std;

int main()
{
	int i;
	set<long int> s;
	for(i=0; i<5; i++)
	{
		s.insert(i);			//0 1 2 3 4
	}
	s.insert(3);				//No new element is inserted
	if(s.find(8)==s.end())
		s.insert(8);			//inserts 8 if not found in set
	else
		s.insert(10);			//inserts 10 if already present
	for(set<long int>::iterator it=s.begin(); it!=s.end(); it++)
		cout<< *it <<" ";
	return 0;
}

In the above example a set of type "long int" is created.
The commonly used functions are:
  • insert(a) : inserts a new element in logarithmic time. Does NOT inserts if element is already present.
  • find(a) : returns the pointer to the element if exists else returns the pointer to the end.
  • size(): returns the no of elements in the set.
set<datatype>::iterator ptr - This statement is used to declare a variable of type set pointer.
vector<datatype>::iterator ptr - declares a vector pointer.

MAP :

It maps the key to value. Keys are unique. This is a one-to-one mapping.
Header file : map

#include <map>
#include <iostream>
using namespace std;

int main()
{
	int i;
	map<char, int> m;
	for(i=0; i<26; i++)
		m['a'+i]=i+100;
	if(m.find('a'+5)==m.end())
		cout<<"Key not found";
	else
		cout<<m['a'+5];
	m.erase('d');
	for(map<char,int>::iterator it=m.begin(); it!=m.end(); it++)
		cout<<it->first<<" "<<it->second<<endl;
	return 0;
}

In the above example the key is of character type and the value is of integer type. The searching is done in logarithmic time.
The "first" keyword returns the key and "second" keyword returns the value.


PRIORITY QUEUE :


It implements MAX HEAP by default (for MIN HEAP see below ).
Header file : queue

#include <queue>
#include <iostream>
using namespace std;

int main()
{
	int i;
	priority_queue<int> pq;
	for(i=0; i<5; i++)
		pq.push(i);
	while(!pq.empty() )
	{
		cout<<pq.top()<<" ";
		pq.pop();
	}
	return 0;
}

top function returns the maximum element. So the output will be : 4 3 2 1 0

To implement MIN HEAP you can use the following statement :
priority_queue<int, vector<int>, greater<int> > pq;


Other info :

The functions which are common to all the above containers(to know why they are called containers please see the website given in the start) are :
  • size() - returns the no of elements.
  • empty() - returns true or false depending on whether the container is empty or not.
  • find(a) - returns the pointer to the element if found else returns the pointer to the end.
  • clear() - removes all the elements.

Other useful header files :

Utility :

  • pair<datatype, datatype> p
    stores a pair of two datatypes.
  • make_pair(a, b)
    returns a pair of a and b.

#include <utility>
#include <iostream>
using namespace std;

int main()
{
	pair<int,int> p(3,5);
	cout<<p.first<<" "<<p.second<<endl;
	p=make_pair(34,67);
	cout<<p.first<<" "<<p.second;
	return 0;
}

String :


#include <string>
#include <iostream>
using namespace std;

int main()
{
	string a="hello", b="world!";
	string c=a+" "+b;
	cout<<c;
}


Algorithm :

 
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
	int a[]={4,6,3,1,2};
	int i;
	vector<int> vec(a, a+5);
	sort(a,a+5);
	for(i=0; i<5; i++)
		cout<<a[i]<<" "; // 1 2 3 4 6

	sort(vec.begin(), vec.end() );
	for(i=0; i<vec.size(); i++)
		cout<<vec[i]<<" ";

	reverse(a,a+5);
	for(i=0; i<5; i++)
		cout<<a[i]<<" "; // 6 4 3 2 1
}

To create a vector from an array you can use the above method i.e.
vector<datatype> vec(pointer of  the array from which values are required , pointer of the array upto which values are required).


I hope this will help you to start using STL .

Sunday, October 14, 2012

Auto-completion in VIM for Python

(This is article has been written based on fedora-16 , vim-ver 7.3)

As we all know that now vim also supports auto-completion for many languages which includes Python too. And if you use vim as a python ide then most probably you will also want the relative functions to pop out as soon you type dot "." so that you don't have to remember them ;) .

So here is quick guide on how to set up your vim for autocompletion of your python codes in case it is not already enabled.

First try this :
a=[1,2,3]
a.

After typing the above lines press "ctrl+x" then "ctrl+o" , if this pops up a menu showing the methods which can be used then you dont need to read further and just enjoy your coding :) .

In case you dont find anything happening then go through the following steps:
1> Type:
vim --version | grep "system"

It should show you something like this:

[root@Rogue codes]# vim --version | grep "system"
system vimrc file: "/etc/vimrc"

So your vim configuration file is in /etc/

2.> Now open it for editing.
It will be better if you make a backup of this file before editing it.
Now type:
vim /etc/vimrc

3.> In the file opened add the following lines in top of the file:

autocmd FileType python set completefunc=pythoncomplete#Complete



Now save it.
Open a python file and try to do the previous thing again that is:
a=[1,2,3]
a.



Now press "ctrl+x" then "ctrl+o" , if this opens the autocompletion window then you are in the right track.


Now for something extra:
It is quite a waste of keyboard strokes to type ctrl+x then ctrl+o, so lets map it to the most easiest thing, i.e. to "." itself.


Again open the vimrc file and add the following line to it:

autocmd FileType python imap . .<C-x><C-o>

after imap there is a space then a "." then a space then again a "." and <C-x><C-o>
Here we are saying that as soon as you press a dot then first type a dot and then press ctrl+x then ctrl+o.
The reason for putting a dot before <C-x><C-o> is that the dot now becomes a map to the sequence of key-strokes, so when you press a dot it is not taken as an input but is rather placed with the given sequence.

Now you just need to press "." and the auto-completion will pop out.

Have fun.