C++ Standard Template Library
STL Components
Containers
Sequence Containers
Container Adaptors
Associative Containers
Unordered Associative Container
Algorithms
Others
<array>
View Index
array<int , 5> arr{1 , 2 , 3 , 4 , 5 };
for (int i : arr)cout << i << " " ;
for (int i = 0 ; i < arr.size (); ++i)cout << arr[i] << " " ;
for (array<int , 5 >::iterator itr = arr.begin (); itr != arr.end (); ++itr)cout << *itr << " " ;
int * ptr = arr.data ();
for (int i = 0 ; i < 5 ; ++i)cout << *(ptr + i) << " " ;
arr.size ();
arr.empty ();
arr.front ();
arr.back ();
arr.fill (0 );
arr.swap ();
arr[pos];
get<pos>(arr);
arr1==arr2;
<vector>
View Index
Internally, Dynamically allocated array is used
vector<int > v;
vector<int >v1{1 , 2 , 3 , 4 , 5 };
vector<int >v2 (5 , 0 );
vector<int >v3 (v1.begin (), v1.begin () + 3 );
vector<int >v4 (v1);
v4=v2;
for (int i : v1)cout << i << " " ;
for (vector<int >::iterator itr = v1.begin (); itr != v1.end (); ++itr)
cout << *itr << " " ;
for (int i = 0 ; i < v1.size (); ++i)cout << v1[i] << " " ;
int *ptr = v.data ();
for (int i = 0 ; i < v.size (); ++i)cout << *(ptr + i) << " " ;
v1.push_back (9 );
v1.pop_back ();
v1.size ();
v1.capacity ();
v1.reserve (20 );
v1.empty ();
v1.resize (3 );
v1.resize (5 );
v1.erase (v.begin ()+3 );
v1.erase (v.begin (),v.begin ()+2 );
v1.front ();
v1.back ();
v1.clear ();
v1.assign (v.begin (), v.end ());
v1.assign ({1 , 2 , 3 });
int vay[] {9 , 8 , 7 , 6 , 5 };
v1.assign (vay, vay + 3 );
v.insert (v.begin () + 2 , 99 );
v.insert (v.begin () + 2 , 3 , 0 );
v.insert (v.begin (), v.begin (), v.begin () + 2 );
<list>
View Index
Implemented as doubly linked lists
list<int > l1;
list<int > l2{1 , 2 , 3 , 4 , 5 };
list<int > l3 (l2) ;
list<int >l4 (5 , 0 );
list<int >l5;
l5 = l4;
l5 = {1 , 2 , 3 , 4 , 5 };
list<int >::iterator itr = l2.begin ();
list<int >l6 (l2.begin (), (advance (itr, 3 ), itr));
for (int i : l2)cout << i << " " ;
for (list<int >::iterator itr = l2.begin (); itr != l2.end (); ++itr)cout << *itr << " " ;
l2.empty ();
l2.size ();
l2.front ();
l2.back ();
l2.push_front (99 );
l2.pop_front ();
l2.push_back (99 );
l2.pop_back ();
l2.assign (3 ,0 );
l2.assign (l4.begin (),l4.end ());
l2.assign ({1 ,2 ,3 ,4 ,5 });
l2.insert ( (itr = l2.end (), advance (itr, -2 ), itr), 99 );
l2.insert (++l2.begin (), 3 , 0 );
l2.insert (++l2.begin (), l4.begin (), l4.end ());
l2.insert (++l2.begin (), {9 , 8 , 7 });
l2.erase (l2.begin ());
l2.erase (l2.begin (), (advance (itr, -1 ), itr));
l2.resize (5 );
l2.resize (8 , 99 );
l2.resize (5 );
l2.clear ();
l2.splice (++l2.begin (), l5);
l2.splice (++l2.begin (), l5, l5.begin (), --l5.end ());
l2.splice (++l2.begin (), l5, ++l5.begin ());
l2.remove (0 );
bool is_even (const int & val) {
return (val & 1 ) == 0 ? true : false ;
}
class is_odd {
public :
bool operator () (const int & val) {
return (val & 1 ) == 1 ? true : false ;
}
};
l2.remove_if (is_even);
l2.remove_if (is_odd ());
l2 = {1 , 2 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 7 , 8 , 8 , 9 };
l2.unique ();
bool is_even (const int & val1, const int & val2) {
return ((val1 == val2) && (!(val1 & 1 ) && !(val2 & 1 ))) ? true : false ;
}
class is_odd {
public :
bool operator () (const int & val1, const int &val2) {
return ((val1 == val2) && ((val1 & 1 ) && (val2 & 1 ))) ? true : false ;
}
};
l2.unique (is_even);
l2.unique (is_odd ());
l1 = {1 , 3 , 5 , 7 };
l2 = {2 , 4 , 5 , 6 , 8 , 9 };
l2.merge (l1);
l2.merge (l1, binaryPredicate);
l2.reverse ();
l2.sort ();
l2.sort (binaryPredicate);
<deque>
View Index
Generally implemented as dynamic arrays
Not guaranteed to store all the elements in contiguous
manner
Elements are scattered in different chunks of storage
int arr[] {1 , 2 , 3 };
vector<int > v{1 , 2 , 3 , 4 , 5 };
deque<int > dq;
deque<int > dq1 (3 ) ;
deque<int > dq2 (3 , 99 ) ;
deque<int > dq3{1 , 2 , 3 , 4 , 5 };
deque<int > dq4 (dq3) ;
deque<int > dq5 (arr, arr + (sizeof (arr) / sizeof (int ))) ;
deque<int > dq6 (v.begin(), v.end() - 2 ) ;
dq=dq1;
dq={1 ,2 ,3 ,4 ,5 };
for (int i : dq)cout << i << " " ;
for (int i = 0 ; i < dq.size (); ++i)cout << dq[i] << " " ;
for (deque<int >::iterator itr = dq.begin (); itr != dq.end (); ++itr)cout << *itr << " " ;
dq.size ();
dq.empty ();
dq.resize (3 );
dq.resize (4 );
dq.resize (5 ,99 );
dq[3 ];
dq.at (3 );
dq.front ();
dq.back ();
dq.push_back (0 );
dq.pop_back ();
dq.push_front (0 );
dq.pop_front ();
dq.assign (dq1.begin (),dq1.end ()-1 );
dq.assign (arr,arr+3 );
dq.assign (3 ,1 );
dq.assign ({1 ,2 ,3 ,0 ,99 });
dq.erase (dq.begin () + 3 );
dq.erase (dq.begin () + 2 , dq.begin () + 4 );
dq.insert (dq.begin () + 2 , 99 );
dq.insert (dq.begin () + 1 , 3 , 0 );
dq.insert (dq.begin (), arr, arr + 2 );
dq.insert (dq.end (), {2 , 3 , 4 });
dq.clear ();
<forward_list>
View Index
Implemented as singly linked lists
int arr[] {1 , 2 , 3 , 4 , 5 };
forward_list<int > fl;
forward_list<int > fl1 (5 , 0 ) ;
forward_list<int > fl2 (arr, arr + 3 ) ;
forward_list<int > fl3 ({1 , 2 , 3 , 4 , 5 }) ;
forward_list<int > fl4 (fl3) ;
fl=fl1;
fl={1 ,2 ,3 ,4 ,5 };
for (int i : fl1)cout << i << " " ;
for (forward_list<int >::iterator itr = fl1.begin (); itr != fl1.end (); ++itr)cout << *itr << " " ;
fl.empty ();
fl.front ();
fl.push_front (99 );
fl.pop_front ();
fl.resize (3 );
fl.resize (4 );
fl.resize (5 ,99 );
fl.assign (arr, arr+3 );
fl.assign (5 ,0 );
fl.assign ({1 ,2 ,3 ,4 ,4 });
forward_list<int >::iterator itr = fl.before_begin ();
fl.insert_after (itr, 99 );
fl.insert_after (++itr, {0 , 0 , 0 });
fl.insert_after (itr, 2 , 1 );
fl.insert_after (itr, arr, arr + 3 );
fl.erase_after (itr);
fl.erase_after (fl.before_begin (), (advance (itr, 5 ), itr));
fl.clear ();
advance (itr, 7 );
fl.splice_after (itr, fl4);
fl.splice_after (itr, fl4, fl4.begin ());
forward_list<int >::iterator itr1 = fl4.begin ();
fl.splice_after (itr, fl4, fl4.before_begin (), (advance (itr1, 3 ), itr1));
fl.remove (0 );
bool removeEven (const int & val) {
return (val & 1 ) == 0 ? true : false ;
}
class removeOdd {
public :
bool operator () (const int & val) {
return (val & 1 ) == 1 ? true : false ;
}
} removeOddObj;
fl.remove_if (removeEven);
fl.remove_if (removeOddObj);
fl.remove_if (removeOdd ());
fl.unique ();
bool removeUniques (const int & val1, const int & val2) {
return ((val1 < 2 and val2 < 2 ) & (val1 == val2)) == true ? true : false ;
}
class removeUniques {
public :
bool operator () (const int & val1, const int & val2) {
return ((val1 >= 2 and val2 >= 2 ) & (val1 == val2)) == true ? true : false ;
}
} removeUniquesObj;
fl.unique (removeUniques);
fl.unique (removeUniquesObj);
fl.merge (fl1);
fl.merge (fl1,greater<int >());
fl.sort ();
fl.sort (greater<int >());
fl.reverse ();
<stack>
View Index
It is a container adaptor
By default the underlying container is
deque
but
vector
and
list
can also be used
vector<int > v{1 , 2 , 3 , 4 , 5 };
list<int > l{1 , 2 , 3 , 4 , 5 };
deque<int >d{1 , 2 , 3 , 4 , 5 };
stack<int > st1;
stack<int , vector<int >> st2 (v);
stack<int , list<int >> st3 (l);
stack<int , deque<int >> st4 (d);
st1.size ();
st.empty ();
st1.top ();
st1.push (99 );
st1.pop ();
<queue>
View Index
It is a container adaptor
By default the underlying container is
deque
but
list
can also be used
list<int > l{1 , 2 , 3 , 4 , 5 };
deque<int >d{1 , 2 , 3 , 4 , 5 };
queue<int > q1;
queue<int , list<int >> q2 (l);
queue<int , deque<int >> q3 (d);
q1.size ();
q1.empty ();
q1.push (99 );
q1.pop ();
q1.front ();
q1.back ();
<queue>->priority_queue
View Index
It is a container adaptor
By default the underlying container is
vector
but
dequeue
can also be used
vector<int > v{1 , 2 , 3 , 4 , 5 };
deque<int >d{1 , 2 , 3 , 4 , 5 };
priority_queue<int >pq1;
priority_queue<int , vector<int >, greater<int >>pq2 (v.begin (), v.end ());
priority_queue<int , deque<int >>pq3 (d.begin (), d.end ());
priority_queue<int , deque<int >, greater<int >>pq4 (d.begin (), d.end ());
priority_queue<int , vector<int >>pq5;
class comparator {
bool reverse;
public :
comparator (const bool & reverse = false ) {
this ->reverse = reverse;
}
bool operator () (const int &val1, const int & val2) {
return reverse ? (val1 > val2) : (val1 < val2);
}
};
priority_queue<int , vector<int >, comparator>pq7;
priority_queue<int , vector<int >, comparator>pq8 (comparator (true ));
pq1.empty ();
pq1.size ();
pq1.top ();
pq1.push (99 );
pq1.pop ();
<set>
View Index
Store unique elements following a
specific order
The value of an element is the key(must be unique)
Elements are always const - cannot be modified once in
container
Elements are sorted
Slower than unordered_set
Implemented as BST
class compareClass {
public :
bool operator () (const int & lhs, const int & rhs) const
{return lhs > rhs;}
} ;
bool compare (const int &val1, const int &val2) {
return val1 > val2 ? true : false ;
}
int arr[] {1 , 2 , 3 , 4 , 5 };
set<int > s;
set<int > s1{1 , 2 , 3 , 4 , 5 };
set<int > s2 (arr, arr + 5 ) ;
set<int > s3 (s2) ;
s=s1;
s={9 ,8 ,7 };
bool (*fnPtr)(const int &, const int &) = compare;
set<int , bool (*) (const int &, const int &) > s4 ({1 , 2 , 3 , 4 , 5 }, fnPtr) ;
set<int , compareClass> s5 ({4 , 5 , 6 , 7 , 8 }) ;
s.empty ();
s.size ();
s.insert (99 );
s.insert (s.begin (),22 );
s.insert (arr, arr+3 );
s.insert ({1 ,2 ,3 ,4 ,5 });
s.erase (2 );
s.erase (++s.begin ());
set<int >::iterator itr = s.begin ();
s.erase (s.begin (), (advance (itr, 3 ), itr));
s.clear ();
s.find (99 );
s.count (25 );
s.lower_bound (10 );
s.upper_bound (10 );
s.equal_pair (10 );
<set>->Multiset
View Index
Store elements following a specific order , and where
multiple elements can have equivalent values
Value of the element is the key; It is const and cannot
be modified once in the container
Elements are in sorted order
Slower than unorderd_multiset
Implemented as BST
class compareClass {
public :
bool operator () (const int & lhs, const int & rhs) const
{return lhs > rhs;}
} ;
bool compare (const int &val1, const int &val2) {
return val1 > val2 ? true : false ;
}
int arr[] {1 , 2 , 3 , 4 , 5 };
multiset<int > ms;
multiset<int > ms1{1 , 2 , 3 , 4 , 5 };
multiset<int > ms2 (arr, arr + 5 ) ;
multiset<int > ms3 (ms2) ;
bool (*fnPtr)(const int &, const int &) = compare;
multiset<int , bool (*) (const int &, const int &) > ms4 ({1 , 2 , 3 , 4 , 5 }, fnPtr) ;
multiset<int , compareClass> ms5 ({4 , 5 , 6 , 7 , 8 }) ;
ms.empty ();
ms.size ();
ms.insert (99 );
ms.insert (ms.begin (),22 );
ms.insert (arr, arr+3 );
ms.insert ({1 ,2 ,3 ,4 ,5 });
ms.erase (2 );
ms.erase (++ms.begin ());
set<int >::iterator itr = ms.begin ();
ms.erase (ms.begin (), (advance (itr, 3 ), itr));
ms.clear ();
ms.find (99 );
ms.count (25 );
ms.lower_bound (10 );
ms.upper_bound (10 );
ms.equal_pair (10 );
<map>
View Index
Associative containers (key-value mapping)
Follow a specific order
The elements are sorted by its key
Slower than unordered_map
Typically implemented as BST
class compareClass {
public :
bool operator () (const char & val1, const char & val2) const {
return val1 > val2;
}
} ;
bool compare (const char & val1, const char & val2) {
return val1 > val2;
}
pair<char , int >arr[] {{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
map<char , int >mp;
map<char , int >mp1 (arr, arr + 3 );
map<char , int >mp2 (mp1);
map<char , int >mp3 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }});
bool (*fnPtr)(const char &, const char &) = compare;
map<char , int , bool (*)(const char &, const char &)>mp4 (fnPtr);
mp4['a' ] = 1 ;
mp4['b' ] = 2 ;
mp4['c' ] = 3 ;
map<char , int , compareClass>mp5 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }});
for (pair<char , int > p : mp5)cout << p.first << " " << p.second << endll;
for (map<char , int >::iterator itr = mp5.begin (); itr != mp5.end (); ++itr)cout << "(" << (*itr).first << "," << (*itr).second << ") " ;
mp.empty ();
mp.size ();
mp['a' ];
mp.at ('a' );
map<char , int , compareClass>::iterator itr = mp5.find ('c' );
mp5.erase (--mp5.end ());
mp5.erase (itr, mp5.end ());
mp5.erase ('d' );
mp5.insert ({'a' , 1 });
mp5.insert (hint,first_itr,last_itr);
mp5.insert (first_itr,last_itr);
mp5.insert ({{'c' , 3 }, {'d' , 4 }});
mp.clear ();
mp.find ('a' );
mp.count ('a' );
mp.lower_bound ('a' );
mp.upper_bound ('a' );
typedef map<char , int , compareClass>::iterator itrType;
pair<itrType, itrType> itrRange;
itrRange = mp5.equal_range ('c' );
<map>->multimap
View Index
Associative containers (key-value mapping)
Follow a specific order
Multiple elements can have equivalent values
Elements always sorted by its key
Slower than unordered_multimap
Typically implemented as BST
class compareClass {
public :
bool operator () (const char & val1, const char & val2) const {
return val1 > val2;
}
} ;
bool compare (const char & val1, const char & val2) {
return val1 > val2;
}
pair<char , int >arr[] {{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
multimap<char , int >mmp;
multimap<char , int >mmp1 (arr, arr + 3 );
multimap<char , int >mmp2 (mmp1);
multimap<char , int >mmp3 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }});
bool (*fnPtr)(const char &, const char &) = compare;
multimap<char , int , bool (*)(const char &, const char &)>mmp4 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }}, fnPtr);
multimap<char , int , compareClass>mmp5 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }});
for (pair<char , int > p : mmp5)cout << p.first << " " << p.second << endll;
for (map<char , int ,compareClass>::iterator itr = mmp5.begin (); itr != mmp5.end (); ++itr)cout << "(" << (*itr).first << "," << (*itr).second << ") " ;
mmp.empty ();
mmp.size ();
multimap<char , int , compareClass>::iterator itr = mmp5.find ('c' );
mmp5.erase (--mmp5.end ());
mmp5.erase (itr, mmp5.end ());
mmp5.erase ('d' );
mmp5.insert ({'a' , 1 });
mmp5.insert (hint,first_itr,last_itr);
mmp5.insert (first_itr,last_itr);
mmp5.insert ({{'c' , 3 }, {'d' , 4 }});
mmp.clear ();
mmp.find ('a' );
mmp.count ('a' );
mmp.lower_bound ('a' );
mmp.upper_bound ('a' );
typedef multimap<char , int , compareClass>::iterator itrType;
pair<itrType, itrType> itrRange;
itrRange = mmp5.equal_range ('c' );
<unordered_map>
View Index
Associative containers (key-value mapping)
Fast retrieval based on keys
Elements are not sorted in any particular order
Elements are organized into buckets based on their
hash values
pair<char , int >arr[] {{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
unordered_map<char , int > um;
unordered_map<char , int > um1 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }}) ;
unordered_map<char , int > um2 (um1) ;
unordered_map<char , int > um3 (arr, arr + 3 ) ;
um=um1;
um={{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
um.empty ();
um.size ();
unordered_map<char , int >::iterator itr = um.find ('c' );
um.count ('c' );
typedef unordered_map<char , int >::iterator itrType;
pair <itrType, itrType> itrPair = um.equal_range ('b' );
um.clear ();
um.insert ({'e' , 5 });
unordered_map<char , int >::iterator itr = um1.begin ();
advance (itr, 2 );
um.insert (um1.begin (), itr);
um.insert ({{'f' , 6 }, {'g' , 7 }});
um.erase ('e' );
um.erase (um.begin ());
itr=um.begin ();
advance (itr,2 );
um.erase (um.begin (),itr);
int n = um.bucket_count ();
for (int i = 0 ; i < n; ++i) {
cout << "bucket #" << i << " contains: " ;
for (auto it = um.begin (i); it != um.end (i); ++it)
cout << "[" << it->first << ":" << it->second << "] " ;
cout << endll;
}
int buckets = um.bucket_count ();
for (int i = 0 ; i < buckets; ++i) {
cout << um.bucket_size (i) << endll;
}
um.bucket ('e' );
um.load_factor ();
um.max_load_factor ();
um.max_load_factor (0.5 )
um.rehash (20 );
um.reserve (20 );
unordered_map<char , int >::hasher hashFn = um.hash_function ();
<unordered_map> -> unordered_multimap
View Index
Associative containers (key-value mapping)
Keys can have equivalent values
Fast retrieval based on keys
Elements are not sorted in any particular order
Elements are organized into buckets based on their
hash values
pair<char , int >arr[] {{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
unordered_multimap<char , int > umm;
unordered_multimap<char , int > umm1 ({{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }}) ;
unordered_multimap<char , int > umm2 (umm1) ;
unordered_multimap<char , int > umm3 (arr, arr + 3 ) ;
umm = umm1;
umm = {{'a' , 1 }, {'b' , 2 }, {'c' , 3 }, {'d' , 4 }};
umm.empty ();
umm.size ();
unordered_multimap<char , int >::iterator itr = umm.find ('c' );
umm.count ('c' );
typedef unordered_multimap<char , int >::iterator itrType;
pair <itrType, itrType> itrPair = umm.equal_range ('b' );
umm.clear ();
umm.insert ({'e' , 5 });
unordered_multimap<char , int >::iterator itr = umm1.begin ();
advance (itr, 2 );
umm.insert (umm1.begin (), itr);
umm.insert ({{'f' , 6 }, {'g' , 7 }});
umm.erase ('e' );
umm.erase (umm.begin ());
itr=umm.begin ();
advance (itr,2 );
umm.erase (umm.begin (),itr);
int n = umm.bucket_count ();
for (int i = 0 ; i < n; ++i) {
cout << "bucket #" << i << " contains: " ;
for (auto it = umm.begin (i); it != umm.end (i); ++it)
cout << "[" << it->first << ":" << it->second << "] " ;
cout << endll;
}
int buckets = umm.bucket_count ();
for (int i = 0 ; i < buckets; ++i) {
cout << umm.bucket_size (i) << endll;
}
umm.bucket ('e' );
umm.load_factor ();
umm.max_load_factor ();
umm.max_load_factor (0.5 );
umm.rehash (20 );
umm.reserve (20 );
unordered_multimap<char , int >::hasher hashFn = umm.hash_function ();
<unordered_set>
View Index
Stores unique elements in
no particular order
Elements is its key; Elements are const , Cannot be
modified once inserted
Not sorted in any particular order
Elements are organized into buckets based on their
hash values
int arr[] {1 , 2 , 3 , 4 , 5 };
unordered_set<int > us;
unordered_set<int > us1 ({1 , 2 , 3 , 4 , 5 }) ;
unordered_set<int > us2 (us1) ;
unordered_set<int > us3 (arr, arr + 3 ) ;
us = us1;
us = {5 , 6 , 7 , 8 , 9 };
us.empty ();
us.size ();
unordered_set<int >::iterator itr = us.find (8 );
us.count (5 );
typedef unordered_set<int >::iterator itrType;
pair<itrType, itrType> itrRange = us.equal_range (6 );
us.clear ();
pair<itrType, bool > temp = us.insert (5 );
itrType itr = us.insert (us.begin (), 10 );
us.insert (arr, arr + 3 );
us.insert ({1 , 2 , 3 });
us.erase (2 );
us.erase (us.begin ());
us.erase (us.begin (), (advance (itr, 2 ), itr));
us.bucket_count ();
us.bucket_size (3 );
us.bucket (2 );
us.load_factor ();
us.max_load_factor ();
us.max_load_factor (2 );
us.rehash (10 );
us.reserve (10 );
unordered_set<int >::hasher hashFn = us.hash_function ();
<unordered_set> -> unordered_multiset
View Index
Stores elements in no particular order
Elements can have equivalent values
Elements is its key; Elements are const , Cannot be
modified once inserted
Not sorted in any particular order
Elements are organized into buckets based on their
hash values
int arr[] {1 , 2 , 3 , 4 , 5 };
unordered_multiset<int > ums;
unordered_multiset<int > ums1 ({1 , 2 , 3 , 4 , 5 }) ;
unordered_multiset<int > ums2 (ums1) ;
unordered_multiset<int > ums3 (arr, arr + 3 ) ;
ums = ums1;
ums = {5 , 6 , 7 , 8 , 9 };
ums.empty ();
ums.size ();
unordered_multiset<int >::iterator itr = ums.find (8 );
ums.count (5 );
typedef unordered_multiset<int >::iterator itrType;
pair<itrType, itrType> itrRange = ums.equal_range (6 );
ums.clear ();
itrType temp = ums.insert (5 );
itrType itr = ums.insert (ums.begin (), 10 );
ums.insert (arr, arr + 3 );
ums.insert ({1 , 2 , 3 });
ums.erase (2 );
ums.erase (ums.begin ());
ums.erase (ums.begin (), (advance (itr, 2 ), itr));
ums.bucket_count ();
ums.bucket_size (3 );
ums.bucket (2 );
ums.load_factor ();
ums.max_load_factor ();
ums.max_load_factor (2 );
ums.rehash (10 );
ums.reserve (10 );
unordered_multiset<int >::hasher hashFn = ums.hash_function ();
<algorithm>
View Index
int arr[] {6 , 3 , 4 , 5 , 3 , 2 , 3 , 1 };
sort (arr, arr + sizeof (arr) / sizeof (int ));
sort (arr, arr + sizeof (arr) / sizeof (int ),compare);
lower_bound (arr, arr + size, 5 );
lower_bound (arr, arr + size, 5 , compare);
upper_bound (arr, arr + size, 5 );
upper_bound (arr, arr + size, 5 , decrease);
pair<itrType, itrType> range = equal_range (arr, arr + size, 5 );
pair<itrType, itrType> range = equal_range (arr, arr + size, 5 ,compare);
binary_search (arr, arr + size, 0 );
binary_search (arr, arr + size, 0 ,compare);
vector<int > v{1 , 2 , 3 , 4 , 5 };
make_heap (v.begin (), v.end ());
v.push_back (20 );
push_heap (v.begin (), v.end (),compare);
sort_heap (v.begin (), v.end (),compare);
make_heap (v.begin (),v.end (),compare);
pop_heap (v.begin (), v.end (),compare);
v.pop_back ();
is_heap (v.begin (),v.end (),compare);
min (1 ,2 ,compare);
min ({1 , 2 , 3 , 4 }, compare);
max (1 ,2 ,compare);
max ({1 , 2 , 3 , 4 }, compare);
prev_permutation (arr, arr + size, compare);
next_permutation (arr, arr + size, compare);
<string>
View Index
string s0;
string s1{"Hello" };
string s2 (s1) ;
string s7 ("Hello" ,2 ) ;
string s3 (s1, 2 ) ;
string s4 (s1, 1 , 3 ) ;
string s5 (s1.begin(), s1.begin() + 3 ) ;
string s6 (3 , '*' ) ;
char ch=*s1.begin ();
char ch=*(s1.end () - 1 );
char ch=*s1.rbegin ();
char ch=*(s1.rend () - 1 ) ;
char ch=*s1.cbegin ();
char ch=*(s1.cend () - 1 ) ;
char ch=*s1.crbegin ();
char ch=*(s1.crend () - 1 );
for (string::iterator itr = s1.begin (); itr != s1.end (); ++itr) {
cout << *itr;
}
for (int i = 0 ; i < s1.size (); ++i) {
cout << s1[i];
}
for (char ch : s1) {
cout << ch;
}
s1.length ();
s1.size ();
s1.resize (10 ,'x' );
s1.resize (3 );
s1.resize (4 );
s1.capacity ();
s1.reserve (28 );
s1.clear ();
s1.empty ();
s1.front ();
s1.back ();
s1.push_back ('x' );
s1.pop_back ();
const char * str = s1.c_str ();
const char * str2 = s1.data ();
s1.copy (to,no_of_char,start_posn);
s1.find ("*" );
s1.find (string,start_pos,no_of_chars_to_match);
s1.find_first_of (string);
s1.find_last_of (string);
string arr = s1.substr (2 );
string arr2 = s1.substr (2 , 2 );
s1.compare (0 , 3 , "Hi Hello" , 3 , 3 );
s1.append (s1);
s1.append (s1, 2 , 3 );
s1.append ("Hello" , 2 );
s1.append ("world" );
s1.append (3 , '*' );
s1.assign (s2);
s1.assign ("world" );
s1.assign ("world" , 4 );
s1.assign ("world" , 2 , 3 );
s1.assign (3 , '*' );
s1.assign (s2.begin (), s2.begin () + 2 );
s1.erase (2 , 2 );
s1.erase (2 );
s1.erase (s1.begin () + 1 );
s1.erase (s1.begin (), s1.begin () + 2 );
s1.insert (2 , "***" );
s1.insert (2 , s2);
s1.insert (2 , "World" , 4 );
s1.insert (2 , "World" , 2 , 3 );
s1.insert (2 , 3 , '*' );
s1.insert (s1.begin () + 2 , s2.begin (), s2.end () );
s1.replace (0 , 2 , "xyz" );
s1.replace (0 , 2 , s2);
s1.replace (0 , 2 , "world" , 3 );
s1.replace (0 , 2 , "world" , 2 , 3 );
s1.replace (0 , 2 , 3 , '*' );
<iomanip>
View Index
cout << setbase (16 );
cout << setw (10 );
cout<<setfill ('x' );
cout << setfill ('x' ) << setw (5 );
cout << setprecision (4 );
cout << 100.12345 << endll;
cout << fixed;
cout << setprecision (2 );
cout << 100.12345 << endll;
<limits>
View Index
numeric_limits<T>::max ();
numeric_limits<T>::min ();
numeric_limits<T>::digits;
numeric_limits<T>::digits10;
<utility>
View Index
pair<int , int > p;
pair<int , int > p1 (1 , 10 ) ;
pair<int , int > p2 (p1) ;
p = make_pair (10 , 99 );
cout << p.first << " " << p.second << endll;
<tuple>
View Index
tuple<int , int > t (10 , 20 ) ;
tuple<int , int , int > t1 (1 , 2 , 3 ) ;
tuple<int , int , int , int >t2 (1 , 2 , 3 , 4 );
tuple<int ,int ,int >t3 (t1);
t2 = make_tuple (7 , 8 , 9 , 0 );
get<2 >(t1);
int a, b, c;
tie (a, b, c) = t4;
<bitset>
View Index
bitset<10> b;
bitset<10> b1 (12345 ) ;
bitset<10>b2 ("10101010" );
cin>>b;
cout<<b;
b1[0 ]=1 ;
b.test (0 );
b.count ();
b.size ();
b.any ();
b.none ();
b.all ();
b.set ();
b.set (2 ,0 );
b.reset ();
b.reset (2 );
b.flip ();
b.flip (2 );
string s;
s=b.to_string ();
unsigned long int uli;
uli = b1.to_ulong ();
unsigned long long ulli;
ulli = b1.to_ullong ();
Functors
View Index
Function objects - Functions wrapped in a class
Function objects are objects specifically designed to be used with a
syntax similar to that of functions .
Achieved by defining member function operator() in
their class
class functor {
int n;
public :
functor () {
n = 10 ;
}
functor (int n) {
this ->n = n;
}
bool operator () (const int & a, const int & b) {
if (a<=n and b<=n)
return a > b;
return false ;
}
} compare;
bool compareFn (const int & a, const int & b) {
return a > b;
}
int arr[] {1 , 2 , 3 , 4 , 5 };
sort (arr, arr + 5 , functor (3 ));
sort (arr, arr + 5 , functor ());
sort (arr, arr + 5 , compare);
sort (arr, arr + 5 , compareFn);
<functional>
View Index
int arr[] {4 , 3 , 2 , 1 , 6 , 7 , 9 };
sort (arr, arr + 7 , bit_and<int >());
sort (arr, arr + 7 , bit_or<int >());
sort (arr, arr + 7 , bit_xor<int >());
sort (arr, arr + 7 , divides<int >());
sort (arr, arr + 7 , equal_to<int >());
sort (arr, arr + 7 , greater<int >());
sort (arr, arr + 7 , greater_equal<int >());
sort (arr, arr + 7 , less<int >());
sort (arr, arr + 7 , less_equal<int >());
sort (arr, arr + 7 , logical_and<int >());
logical_not<int >();
sort (arr, arr + 7 , logical_or<int >());
sort (arr, arr + 7 , minus<int >());
sort (arr, arr + 7 , modulus<int >());
sort (arr, arr + 7 , multiplies<int >());
negate<int >();
sort (arr, arr + 7 , not_equal_to<int >());
sort (arr, arr + 7 , plus<int >());
sort (arr, arr + 7 , logical_and<int >());
<cmath>
View Index
double multiplier = PI / 180 ;
sin (90 * multiplier);
cos (90 * multiplier);
tan (45 * multiplier);
exp (5 );
log (exp (1 ));
log (2 );
log10 (10 );
log10 (2 );
log2 (2 );
double fractPart, intPart;
fractPart = modf (3.1415 , &intPart);
exp2 (10 );
pow (10 , 3 );
sqrt (36 );
cbrt (8 );
ceil (2.99 );
ceil (-2.99 );
ceil (3.01 );
ceil (-3.01 );
floor (2.99 );
floor (-2.99 );
floor (3.01 );
floor (-3.01 );
round (3.49 );
round (-3.49 );
round (3.50 );
round (-3.50 );
trunc (2.25 );
trunc (-2.25 );
abs (-3.14 );
abs (3.14 );