an approach to programming contests with c++
TRANSCRIPT
A lil Bit About Templates B4 V A lil Bit About Templates B4 V BeginBegin
So, What Are They ???So, What Are They ???It Aids Generic Programming
( Basically, Makes Life Much Easier )
You Can Create a Queue Of Type T where, T is of any type !!!
Eg. queue<int> iq;queue<string> sq;
File HandlingFile Handling
Handling Input, Output From Files Is Handling Input, Output From Files Is Much Easier Than You Think !!!Much Easier Than You Think !!!
freopen(“input file name”,”r”,stdin);freopen(“input file name”,”r”,stdin);freopen(“output file name”,”w”,stdout);freopen(“output file name”,”w”,stdout);
- TIP -
- TIP -
string s;string s;getline(cin , s , '\n');getline(cin , s , '\n');
Use this to read a whole Use this to read a whole line line of text from the input fileof text from the input file
A world of strings
FindFindSimplest way to find a substring
What Does It Do ?What Does It Do ?Returns The Index Of The First Occurrence Of The Substring.
If There Is No Such Occurrence, returns a value > Length Of String.
string a=“hello world”;cout<<a.find(“0 w”);
ReplaceReplaceAn Easy Way To Replace Text In Strings
What Does It Do ?What Does It Do ?
Replaces Some Particular Text In A String With The Text You Want
To Replace With
string a=“hello world”;cout<<a.replace(i,j,”hi”);
Other Useful String FunctionsOther Useful String Functions
string s=“01236789876543210”; string s=“01236789876543210”;
s.insert(i,”45”);s.insert(i,”45”);s.erase(i,j);s.erase(i,j);s.length();s.length();s.rfind(“3”);s.rfind(“3”);
Extract Data From A String With Extract Data From A String With sscanf()sscanf()
It is like scanf() but, scans from a string It is like scanf() but, scans from a string instead of standard input !!!instead of standard input !!!
string s=“12:40”;string s=“12:40”;int h,m;int h,m;sscanf(s.c_str() , "%d:%d“ , &h , &m);sscanf(s.c_str() , "%d:%d“ , &h , &m);cout<<h<<endl<<m;cout<<h<<endl<<m;
- TIP -
- TIP -
You can use find and replace You can use find and replace together, to replace all occurences together, to replace all occurences of a particular substring in a string, of a particular substring in a string, with some other sequence of with some other sequence of characters, irrespective of the size characters, irrespective of the size of the new sequence of characters ! of the new sequence of characters ! Very Useful !!!Very Useful !!!
#include<sstream>#include<sstream>
Converting Converting AnythingAnything Printable, To A Printable, To A StringStringNot as hard as it sounds !!Not as hard as it sounds !!
stringstream ss;string s;float f=1.732;int i=43;char c=‘g’;
ss<<“StringstreamsRock!!”<<f<<i<<c;ss>>s;cout<<s;
- TIP -
- TIP -
It Is A Better Idea To Use--It Is A Better Idea To Use--
getline(ss , s , ‘\n’);getline(ss , s , ‘\n’);
(or)(or)
s=ss.str();s=ss.str();
a^=b^=a^=b;a^=b^=a^=b;
What Does It Do ??What Does It Do ??Guesses Permitted !!Guesses Permitted !!
a=a^b;a=a^b;b=a^b;b=a^b;a=a^b;a=a^b;
After Simplifying It A Little BitAfter Simplifying It A Little Bit
It Works Coz Of The Fact It Works Coz Of The Fact That:That:
a^b=ca^b=c =>=> b^c=a b^c=a c^a=bc^a=b
#include<vector>#include<vector>
Capabilities:Capabilities:
Easy Sorting!!Easy Sorting!!Constant Access Time!!Constant Access Time!!Linear Time for finding Linear Time for finding
elements!!elements!!
So why not Arrays??So why not Arrays??
Variable Size!!Variable Size!!
Useful Functions:Useful Functions:
push_back()push_back()pop_back()pop_back()
insert()insert()erase()erase()clear()clear()
assign()assign()
Ready to use Algorithms Ready to use Algorithms #include<algorithm>#include<algorithm>
C++ Already Contains: C++ Already Contains: (Used with respect to (Used with respect to Vector v1)Vector v1)
sort() sort() stable_sort()stable_sort()
qsort()qsort()find()find()
reverse()reverse()merge()merge()
There’s More..There’s More..
min_element()min_element()max_element()max_element()
next_permutation()next_permutation()prev_permutation()prev_permutation()