You are on page 1of 6

7/29/2014 ACM

http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 1/6
[anerroroccurredwhileprocessingthisdirective]
[anerroroccurredwhileprocessingthisdirective][anerroroccurredwhileprocessingthisdirective][an
erroroccurredwhileprocessingthisdirective]
Strings
#include <string>
Note:almostallfunctionsthattakeastringwillalsoacceptaCstringorachar!
Constructors
string();
string(const string &s2);
string(unsigned int repetitions, char c);
Assignment
string& operator=(const string &s2);
LengthandCapacity
unsigned int size() const;
unsigned int length() const; // same as size()
void resize(unsigned int size, char c = '\0');
void clear();
bool empty() const;
Elementaccess
char& operator[](unsigned int pos); // No bounds checking
char& at(unsigned int pos); // Same as above but checks bounds
string& substr(unsigned int pos, unsigned int length);
iterator begin();
iterator end();
Append
string& operator+=(const string &s2);
Searching
Thesefunctionseitherreturnaposition>0,oraspecialpositionfornotfound.Totestforthenotfound,
comparetheresulttostring::npos.
unsigned int find(const string &s2, unsigned int pos1 = 0);
unsigned int rfind(const string &s2, unsigned int pos1 = end);
unsigned int find_first_of(const string &s2, unsigned int pos1 = 0);
unsigned int find_last_of(const string &s2, unsigned int pos1 = end);
unsigned int find_first_not_of(const string &s2, unsigned int pos1 = 0);
unsigned int find_last_not_of(const string &s2, unsigned int pos1 = end);
Insert,Erase,Replace
string& insert(unsigned int pos1, const string &s2);
string& insert(unsigned int pos1, unsigned int repetitions, char c);
7/29/2014 ACM
http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 2/6
string& erase(unsigned int pos = 0, unsigned int len = npos);
string& replace(unsigned int pos1, unsigned int len1, const string &s2);
string& replace(unsigned int pos1, unsigned int len1, unsigned int repetitions,
char c);
Comparison
Note:allthesefunctionsworkwitheither2strings,orastringandaCstringineitherorder.
bool operator==(const string &s1, const string &s2);
bool operator!=(const string &s1, const string &s2);
bool operator<(const string &s1, const string &s2);
bool operator>(const string &s1, const string &s2);
bool operator<=(const string &s1, const string &s2);
bool operator>=(const string &s1, const string &s2);
Deques
#include <deque>
Constructors
deque<T>();
deque<T>(unsigned int size);
deque<T>(unsigned int size, const T& copy);
Assignment
deque<T>& operator=(const deque<T> d2)
LengthandCapacity
unsigned int size() const;
bool empty() const;
void resize(unsigned int size)
void resize(unsigned int size, const T &t);
void clear();
iterator erase(iterator pos); // Removes a single element
iterator erase(iterator first, iterator last); // Removes elements [first, last)
Elementaccess
T& operator[](unsigned int pos); // No bounds checking
T& at(unsigned int pos); // Same as above but checks bounds
T& front();
T& back();
iterator begin();
iterator end();
Pushandpop
void push_front(const T &t);
void push_back(const T &t);
void pop_front();
void pop_back();
Comparison
7/29/2014 ACM
http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 3/6
bool operator==(const deque<T> &s1, const deque<T> &s2);
bool operator!=(const deque<T> &s1, const deque<T> &s2);
bool operator<(const deque<T> &s1, const deque<T> &s2);
bool operator>(const deque<T> &s1, const deque<T> &s2);
bool operator<=(const deque<T> &s1, const deque<T> &s2);
bool operator>=(const deque<T> &s1, const deque<T> &s2);
Algorithms
#include <algorithm>
Note:mostofthesealgorithmsrequireaniterator.Aniteratoriseitherreturnedbybegin()andend()
fordeques,oryoucanusearrayNameandarrayName + sizeOfArrayfornormalarrays.Ifyouonly
wanttoworkon,say,thefirst5elements,youcanalsousebegin() + 5orarrayName + 5.
Sorting
Thesealgorithmstakeanoptionalthirdargument:afunctionyoucanusetocomparetheelements.You
couldusethistosortstructs,ortodefineyourowncomparisonfunction(e.g.,use>tosortin
descendingorder).
Usethisfunctionprototype:
bool someKindOfComparison(const Type& t1, const Type& t2);
void sort(iterator first, iterator last);
void sort(iterator first, iterator last, LessThanFunction comp);
void stable_sort(iterator first, iterator last);
void stable_sort(iterator first, iterator last, LessThanFunction comp);
void partial_sort(iterator first, iterator middle, iterator last);
void partial_sort(iterator first, iterator middle, iterator last, LessThanFunction
comp);
bool is_sorted(iterator first, iterator last);
bool is_sorted(iterator first, iterator last, LessThanOrEqualFunction comp);
Modifications
Uniquemovesalltheduplicateelementstothebackofthelist.Thelistmustbesortedbeforecalling
unique!Uniquereturnsaniteratorpointingtothebeginningofthenonuniqueelements.
iterator unique(iterator first, iterator last);
iterator unique(iterator first, iterator last, EqualFunction comp);
void reverse(iterator first, iterator last);
Permutations
Next_permutationproducespermutationsoftheelementsinanarray.Next_permutationwillreturnfalse
whentheelementsaresortedafterbeingpermuted.Therefore,youshouldsortthearrayandthenusea
dowhilelooptoproduceallpermutations.Forexample:
int x[] = {3, 5, 4, 1, 2};
sort(x, x + 5);
do {
// Stuff
} while(next_permutation(x, x + 5));
bool next_permutation(iterator first, iterator last);
bool next_permutation(iterator first, iterator last, LessThanOrEqualFunction
comp);
bool prev_permutation(iterator first, iterator last);
bool prev_permutation(iterator first, iterator last, LessThanOrEqualFunction
comp);
7/29/2014 ACM
http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 4/6
Searching
iterator find(iterator first, iterator last, const T &value);
iterator find_if(iterator first, iterator last, const T &value, TestFunction
test);
bool binary_search(iterator first, iterator last, const T &value);
bool binary_search(iterator first, iterator last, const T &value,
LessThanOrEqualFunction comp);
Stringstreams
#include <sstream>
Stringstreamsallowyoutoreadfromandwritetostringsinthesamewaythatyouusecinandcout.
Example:
stringstream s1;
int i = 22;
s1 << "Hello world! " << i;
cout << s1.str() << endl;
Constructors
stringstream();
stringstream(string s);
istringstream();
istringstream(string s);
ostringstream();
ostringstream(string s);
Displayfunction
string &str();
Maps
#include <map>
Constructors
map();
map(lessThanFunction func);
Assignment/Insertion
valueType& operator[](const keyType& pos);
LengthandCapacity
unsigned int size() const;
void clear();
bool empty() const;
Searching
7/29/2014 ACM
http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 5/6
Findreturnsaniteratortotheelementinthemap.Iftheelementisnotinthemap,thenaniterator
pointingtoend()isreturned.
iterator find(keyType &x);
Iterating
for (map<keyType, valueType>::iterator i = someMap.begin(); i < someMap.end();
++i)
{
cout << i->first; // This is the key
cout << i->second; // This is the element
}
FormattingOutput
#include <iomanip>
Spacing
setw(int n)
left
right
setfill(char ch)
Floatingpoint
setprecision(int n)
fixed
scientific
showpos
noshowpos
uppercase
nouppercase
Integers
dec
oct
hex
showpos
noshowpos
uppercase
nouppercase
Booleans
boolalpha
noboolalpha
Randomalgorithms
#include <cstdlib>
#include <ctime>
int main() {
srand(time(NULL));
7/29/2014 ACM
http://www.cs.uwyo.edu/~seker/acm/stlCheatsheet.html 6/6
Togeneraterandomnumbersintherange[a,b)use:
rand() % (b - a) + a;
Ifyoujustwantrandomnumbersfrom[0,b)use:
rand() % b;
Youcanalsogeneraterandompermutationsinalist:
&35;include <algorithm>
random_permutation(anArray, anArray + 10);
random_permutation(aVector, aVector + 10);
[an error occurred while processing this directive]

You might also like