Numerous concepts were simplified in this quick reference. For example, in the Standard, string is actually a typedef for basic_string<char, char_traits<char>, allocator<char> >. If you look up a function in the standard and find that it has different arguments or a different return type than I've specified here, it's probably because I wanted to make it simpler. Also, there are defects in the standard. For situations I was aware of, I tried to present the likely resolution to the defect, rather than the original defective definition.
Important Note: Except where otherwise noted, all of the components
in this quick reference are in namespace std.
The single argument pos or pos1 signifies a position within the this string.
The two arguments, pos and len, signify a substring of the this string starting at pos with length len. If len is npos or is more than size(), then the substring goes from pos to the end of the string.
The arguments, pos1 and len1 have the same meaning as pos and len and are used in functions where there is a second string involved.
The arguments, s2, pos2 and len2 signify a substring of s2 starting at pos2 with length len2.
The argument, nts, signifies a Null-Terminated String (a C-style string).
The arguments, buf and buflen signify a character buffer, buf, of length buflen. (A null character, is treated like any other character).
The arguments, repetitions and c signify repetitions copies of the signal character, c.
The two arguments, start and finish signify an iterator
range, [start, finish), comprising a substring of the this
string.
size_type difference_type iterator const_iterator reverse_iterator const_reverse_iterator
const size_type npos; // Maximum size_type
string(); string(const string& s2); // Copy constructor string(const string& s2, size_type pos2, size_type len2); string(const char* nts); // convert from C-string string(const char* buf, size_type bufsize); string(size_type repetitions, char c);
string& operator=(const string& s2); // normal assignment string& operator=(const char* nts); // convert from C-string string& operator=(char c); // assign a single character
string& assign(const string& s2); string& assign(const string& s2, size_type pos2, size_type len2); string& assign(const char* nts); string& assign(const char* buf, size_type buflen); string& assign(size_type repetitions, char c);
void swap(string& s2);
iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; reverse_iterator rbegin(); reverse_iterator rend(); const_reverse_iterator rbegin() const; const_reverse_iterator rend() const;
size_type size() const; size_type length() const; // same as size() size_type max_size() const; void resize(size_type size, char c = '\0'); void clear(); bool empty() const;
size_type capacity() const; void reserve(size_type capacity = 0);
char& operator[](size_type pos); const char& operator[](size_type pos) const; char& at(size_type pos); const char& at(size_type pos) const;
string& append(const string& s2); string& append(const string& s2, size_type pos2, size_type len2); string& append(const char* nts); string& append(const char* buf, size_type buflen); string& append(size_type repetitions, char c);
string& operator+=(const string& s2); string& operator+=(const char* nts); string& operator+=(char c);
string& insert(size_type pos1, const string& s2); string& insert(size_type pos1, const string& s2, size_type pos2, size_type len2); string& insert(size_type pos1, const char* nts); string& insert(size_type pos1, const char* buf, size_type buflen); string& insert(size_type pos1, size_type repetitions, char c);
iterator insert(iterator pos1, char c); void insert(iterator pos1, size_type repetitions, char c);
string& erase(size_type pos = 0, size_type len = npos); iterator& erase(iterator pos); iterator& erase(iterator start, iterator finish);
string& replace(size_type pos1, size_type len1, const string& s2); string& replace(size_type pos1, size_type len1, const string& s2, size_type pos2, size_type len2); string& replace(size_type pos1, size_type len1, const char* nts); string& replace(size_type pos1, size_type len1, const char* buf, size_type buflen); string& replace(size_type pos1, size_type len1, size_type repetitions, char c);
string& replace(iterator start, iterator finish, const string& s2); string& replace(iterator start, iterator finish, const char* nts); string& replace(iterator start, iterator finish, const char* buf, size_type buflen); string& replace(iterator start, iterator finish, size_type repetitions, char c);
// compare function returns -1 if *this < s2, 0 if *this == s2 // and +1 if *this > s2. int compare(const string& s2) const; int compare(size_type pos1, size_type len1, const string& s2); int compare(size_type pos1, size_type len1, const string& s2, size_type pos2, size_type len2); int compare(const char* nts); int compare(size_type pos1, size_type len1, const char* nts); int compare(size_type pos1, size_type len1, const char* buf, size_type buflen);
string substr(pos = 0, len = npos) const;
// This constructor is repeated here because it creates substring string(const string& s2, size_type pos2, size_type len2);
// This constructor is repeated here because it converts // a C string to a C++ string. string(const char* nts);
const char* c_str() const; // Returns a null-terminated string const char* data() const; // Not null-terminated
// Copy characters to an array. Does not append a null-terminator size_type copy(char* buf, size_type bufsize, size_type pos1 = 0) const;
size_type find(const string& s2, size_type pos1); size_type find(const char* nts, size_type pos1); size_type find(const char* buf, size_type pos1, size_type bufsize); size_type find(char c, size_type pos1);
size_type rfind(const string& s2, size_type pos1); size_type rfind(const char* nts, size_type pos1); size_type rfind(const char* buf, size_type pos1, size_type bufsize); size_type rfind(char c, size_type pos1);
size_type find_first_of(const string& s2, size_type pos1); size_type find_first_of(const char* nts, size_type pos1); size_type find_first_of(const char* buf, size_type pos1, size_type bufsize); size_type find_first_of(char c, size_type pos1);
size_type find_last_of(const string& s2, size_type pos1); size_type find_last_of(const char* nts, size_type pos1); size_type find_last_of(const char* buf, size_type pos1, size_type bufsize); size_type find_last_of(char c, size_type pos1);
size_type find_first_not_of(const string& s2, size_type pos1); size_type find_first_not_of(const char* nts, size_type pos1); size_type find_first_not_of(const char* buf, size_type pos1, size_type bufsize); size_type find_first_not_of(char c, size_type pos1);
size_type find_last_not_of(const string& s2, size_type pos1); size_type find_last_not_of(const char* nts, size_type pos1); size_type find_last_not_of(const char* buf, size_type pos1, size_type bufsize); size_type find_last_not_of(char c, size_type pos1);
// String concatenation: string operator+(const string& s1, const string& s2); string operator+(const char* nts, const string& s2); string operator+(char c, const string& s2); string operator+(const string& s1, const char* nts); string operator+(const string& s1, char c);
// Equal bool operator==(const string& s1, const string& s2); bool operator==(const char* nts, const string& s2); bool operator==(const string& s1, const char* nts);
// Not-equal bool operator!=(const string& s1, const string& s2); bool operator!=(const char* nts, const string& s2); bool operator!=(const string& s1, const char* nts);
// Ordering: bool operator<(const string& s1, const string& s2); bool operator<(const char* nts, const string& s2); bool operator<(const string& s1, const char* nts);
// Greater than: bool operator>(const string& s1, const string& s2); bool operator>(const char* nts, const string& s2); bool operator>(const string& s1, const char* nts);
// Less-than or equal-to: bool operator<=(const string& s1, const string& s2); bool operator<=(const char* nts, const string& s2); bool operator<=(const string& s1, const char* nts);
// Greater-than or equal-to: bool operator>=(const string& s1, const string& s2); bool operator>=(const char* nts, const string& s2); bool operator>=(const string& s1, const char* nts);
// Input and output ostream& operator<<(ostream& os, const string& s); istream& operator>>(istream& is, string& s); istream& getline(istream& is, string& s, char delimiter = '\n');
Class definition | Header file |
template <class T> class vector; | <vector> |
template <class T> class deque; | <deque> |
template <class T> class list; | <list> |
class string; (see also separate section
on strings)
(A string can be used as a sequence container with element type char) |
<string> |
Class definition | Header file |
template <class T> class set; | <set> |
template <class T> class multiset; | <set> |
template <class K, class T, class Comp> class map; | <map> |
template <class K, class T, class Comp> class multimap; | <map> |
value_type iterator const_iterator reverse_iterator const_reverse_iterator size_type difference_type
Function | Performance |
---|---|
container (); (Default Constructor) | constant |
container (const container & c2); (Copy constructor) | linear |
~container (); (Destructor) | linear |
container operator=(const container& c2); (Assignment operator) | linear |
void swap(const container & c2); | constant |
iterator begin();
const_iterator begin() const; |
constant |
iterator end();
const_iterator end() const; |
constant |
reverse_iterator rbegin();
const_reverse_iterator rbegin() const; |
constant |
reverse_iterator rend();
const_reverse_iterator rend() const; |
constant |
size_type size() const; | constant |
size_type max_size() const; | constant |
bool empty() const; | constant |
void clear(); | linear |
bool operator==(const container & c1, const
container & c2);
bool operator!=(const container & c1, const
container
& c2);
bool operator<(const container & c1, const
container & c2);
bool operator>(const container & c1, const
container & c2);
bool operator<=(const container & c1, const
container & c2);
bool operator>=(const container & c1, const
container & c2);
The InIter parameters to the template member functions are not necessarily of the same type as iterator. These functions copy elements from another container that may not be of the same type. Compilers that do not support template members will not support these functions.
Function | Performance | containers |
---|---|---|
container (size_type num, const T& val = T()); (construct n copies of t) | linear | VDLS |
template<class InIter>
container(InIter start, InIter finish); (construct from iterator range) |
linear | VDLS |
void assign(size_type num, const T& val); | linear | VDLS |
template<class InIter>
void assign(InIter start, InIter finish); |
linear | VDLS |
void resize(size_type num, const T& val = T()); | linear | VDLS |
iterator insert(iterator pos, const T& t); | constant for list,
linear for others |
VDLS |
void insert(iterator pos, size_type num, const T& t); | constant for list,
linear for others |
VDLS |
template<class InIter>
void insert(iterator pos, InIter start, InIter finish); |
linear | VDLS |
iterator erase(iterator pos); | constant for list,
linear for others |
VDLS |
iterator erase(iterator start, iterator finish); | linear | VDLS |
T& front();
const T& front() const; |
constant | VDL- |
T& back();
const T& back() const; |
constant | VDL- |
void push_front(const T& t); | constant | -DL- |
void push_back(const T& t); | constant | VDLS |
void pop_front(); | constant | -DL- |
void pop_back(); | constant | VDL- |
T& operator[](size_type index);
const T& operator[](size_type index) const; |
constant | VD-S |
T& at(size_type index);
const T& at(size_type index) const; |
constant | VD-S |
size_type capacity() const; | constant | V--S |
void reserve(size_type num); | linear | V--S |
set<T> and multiset<T> | map<K,T> and multimap<K,T> |
typedef T key_type; | typedef K key_type; |
typedef T value_type; | typedef pair<const K, t> value_type; |
typedef Comp key_compare; | typedef Comp key_compare; |
The InIter parameters to the template member functions are not necessarily of the same type as iterator. These functions copy elements from another container that may not be of the same type. Compilers that do not support template members will not support these functions.
Function | Performance | containers |
---|---|---|
container (Comp c = Comp()); (Constructor) | constant | sSmM |
template <class InIter>
container(InIter start, InIter finish, Comp c = Comp()); |
N log(N) | sSmM |
key_compare key_comp() const; | constant | sSmM |
value_compare value_comp() const; | constant | sSmM |
pair<iterator, bool> insert(const value_type&
v);
(unique insert) |
logarithmic | s-m- |
iterator insert(const value_type& v); (non-unique insert) | logarithmic | -S-M |
template <class InIter>
insert(InIter start, InIter finish) |
N Log(N)
(approx.) |
sSmM |
iterator find(const K& k);
const_iterator find(const K& k) const; |
logarithmic | sSmM |
iterator lower_bound(const K& k);
const_iterator lower_bound(const K& k) const; |
logarithmic | sSmM |
iterator upper_bound(const K& k);
const_iterator upper_bound(const K& k) const; |
logarithmic | sSmM |
pair<iterator,iterator> equal_range(const K&
k);
pair<const_iterator,const_iterator> equal_range(const K& k) const; |
logarithmic | sSmM |
T& operator[](const K& k); | logarithmic | --m- |
size_type erase(const K& k); | logarithmic | sSmM |
size_type count(const K& k) const; | logarithmic | sSmM |
void splice(iterator pos, list& list2); // Move all elements of list2 void splice(iterator pos, list& list2, iterator i2); void splice(iterator pos, list& list2, iterator start2, iterator end2);
The splice functions removes elements from list2 and inserts them into *this. The data structures are modified in such a way that the elements are simply moved between the lists, not copied or deleted.
void remove(const T& value); template <class UnaryPred> void remove(UnaryPred pred);
Remove all elements that match the specified value or predicate.
void unique(); template <class BinPred> unique(BinPred eq);
For each sequence of equal elements within the list, removes all but the first. If a predicate argument is supplied, it is used instead of == to compare elements.
void sort(); template <class Compare> sort(Compare cmp);
Sorts the list. If a Compare predicate is supplied, it is used to compare the elements instead of the < operator.
void merge(list& list2); template <class Compare> merge(list& list2, Compare cmp);
Merges two sorted lists into one. The elements are removed from list2 and added to *this. If a Compare predicate is supplied, it is used to compare the elements instead of the < operator.
void reverse();
Reverses the order of elements in the list.
#include <stack>
template <class T, class Container = deque<T> > class stack { public: // Constructor explicit stack(const Container& c = Container());
bool empty() const; size_type size() const; value_type& top(); const value_type& top() const; void push(const value_type& t); void pop(); };
#include <queue>
template <class T, class Container = deque<T> > class queue { public: // Constructor explicit queue(const Container& c = Container());
bool empty() const; size_type size() const; value_type& front(); const value_type& front() const; value_type& back(); const value_type& back() const; void push(const value_type& t); void pop(); };
#include <queue>
template <class T, class Container = vector<T>, class Compare = less<Container::value_type> > class priority_queue { public: // Constructors explicit priority_queue(const Compare& comp = Compare(), const Container& c = Container()); template <class InIter> priority_queue(InIter start, InIter finish, const Compare& comp = Compare(), const Container& c = Container());
bool empty() const; size_type size() const; const value_type& top() const; // const version only void push(const value_type& t); void pop(); };
#include <iterator>output-iterator back_inserter(Container& c);
#include <iterator>
template<class Iter, difference_type> void advance(Iter& i, difference_type d);
template<class Iter> difference_type distance(Iter start, Iter finish);
#include <functional>
template <class arg, class ret> struct unary_function { typedef arg argument_type; typedef ret result_type; };
template <class arg1, class arg2, class ret> struct binary_function { typedef arg1 first_argument_type; typedef arg2 second_argument_type; typedef ret result_type; };
#include <functional>
// Arithmetic function objects template <class T> struct plus; // binary + template <class T> struct minus; // binary - template <class T> struct multiplies; // binary * template <class T> struct divides; // binary / template <class T> struct modulus; // binary % template <class T> struct negate; // unary -
// Comparisons template <class T> struct equal_to; // == template <class T> struct not_equal_to; // != template <class T> struct less; // < template <class T> struct greater; // > template <class T> struct less_equal; // <= template <class T> struct greater_equal; // >=
// Logical operations template <class T> struct logical_and; // binary && template <class T> struct logical_or; // binary || template <class T> struct logical_not; // unary !
#include <functional>template <class UnaryPred>
template <class BinaryPred>
binary-function-object not2(const BinaryPred& pred);
template <class BinaryPred, class T>
unary-function-object bind1st(const BinaryPred& pred,
const T& val);
template <class BinaryPred, class T>
unary-function-object bind2nd(const BinaryPred& pred,
const T& val);
template <class ret, class Arg>
unary-function-object ptr_fun(ret (*func)(Arg));
template <class ret, class Arg1, class Arg2>
binary-function-object ptr_fun(ret (*func)(Arg1, arg2));
// Returns a unary function object taking a T* argument
template <class ret, class T>
unary-function-object mem_fun(ret (T::*f)());
// Returns a binary function object taking T* and Arg arguments
template <class ret, class T, class Arg>
binary-function-object mem_fun(ret (T::*f)(Arg));
// Returns a unary function object taking a T& argument
template <class ret, class T>
unary-function-object mem_fun_ref(ret (T::*f)());
// Returns a binary function object taking T& and Arg arguments
template <class ret, class T, class Arg>
binary-function-object mem_fun_ref(ret (T::*f)(Arg));
// Returns a unary function object taking a const T* argument
template <class ret, class T>
unary-function-object mem_fun(ret (T::*f)() const);
// Returns a binary function object taking const T* and Arg arguments
template <class ret, class T, class Arg>
binary-function-object mem_fun(ret (T::*f)(Arg) const);
// Returns a unary function object taking a const T& argument
template <class ret, class T>
unary-function-object mem_fun_ref(ret (T::*f)() const);
// Returns a binary function object taking const T& and Arg
arguments
template <class ret, class T, class Arg>
binary-function-object mem_fun_ref(ret (T::*f)(Arg) const);
http://www.cs.rpi.edu/projects/STL/htdocs/stl.html
http://www.sgi.com/Technology/STL/
The STL algorithms changed a bit when they were incorporated into the C++ Standard Library, one notable change from the above sources is in the count and count_if algorithms. Instead of passing the result in by reference, the new versions return the result directly. The return type, difference_type, is potentially different for each iterator type. In practice, however, long int will suffice for almost all cases. The new definitions are as follows:
template <class InIter, class T> difference_type count(InIter start, InIter finish, const T& value);
template <class InIter, class UnaryPred> difference_type count_if(InIter start, InIter finish, UnaryPred pred);
#include <iostream> class ios; class istream : virtual public ios; class ostream : virtual public ios; class iostream : public istream, public ostream;
#include <sstream> class stringstream : public iostream; class istringstream : public istream; class ostringstream : public ostream;
#include <fstream> class fstream : public iostream; class ifstream : public istream; class ofstream : public ostream;
#include <strstream> // (deprecated) class strstream : public iostream class istrstream : public istream class ostrstream : public ostream
// Bitmask types: value can be the bit-wise OR of one or more of // the constants on the right ios::fmtflags = (see below) ios::iostate = ios::goodbit = 0, ios::badbit, ios::eofbit, ios::failbit ios::openmode = ios::in, ios::out, ios::trunc, ios::app, ios::ate, ios::binary
// Enumerated type: value can be one of the constants on the right ios::seekdir = ios::beg, ios::cur, ios::end
// The value of a fmtflags object is the bitwise OR of one or more // of the following flags: ios::boolalpha // show bools as "true" and "false" rather than 1 and 0 ios::showbase // show "0x" or "0" in front of hex or octal numbers ios::showpos // put a "+" in front of positive numbers ios::showpoint // show decimal point on floats that have no fraction ios::skipws // skip whitespace on input (default) ios::unitbuf // flush buffer after each operation ios::uppercase // Use uppercase hex digits, floating-point exponents, etc.
// Only one of the following justification adjustment flags should be set ios::left // left justify output in field width ios::right // right justify output in field width ios::internal // right justify but put prefix (sign, base, currency) at left
// Only one of the following base flags should be set ios::dec // show integral values in decimal ios::hex // show integral values in hexadecimal ios::oct // show integral values in octal
// Only one of the following floating point format flags should be set ios::fixed // fixed-point notation ios::scientific // scientific notation
// The following masks are used to group mutually-exclusive flags ios::adjustfield = ios::left | ios::right | ios::internal ios::basefield = ios::dec | ios::hex | ios::oct ios::floatfield = ios::fixed | ios::scientific
// Get/set format flags fmtflags flags() const; // get all format flags fmtflags flags(fmtflags f); // set all format flags fmtflags setf(fmtflags f); // set one or more format flags fmtflags setf(fmtflags f, fmtflags g); // Set one format flag of a // mutually-exclusive group void unsetf(fmtflags mask); // clear one or more format flags
// Get/set format variables (set functions return old value) int precision() const; // get floating-point precision int precision(int p); // set floating-point precision int width() const; // get field width int width(int w); // set field width char fill() const; // get fill character char fill(char f); // set fill character
// Get/set error state bool good(); // stream is in a good state bool eof(); // end of file (end of stream) detected bool fail(); // Last operation failed (e.g. format error) bool bad(); // I/O error void clear(iostate = ios::goodbit); // Clear/set all error bits void setstate(iostate state); // set one or more error bits
ostream& put(char c); ostream& write(const char*, streamsize); ostream& flush();
int get(); istream& get(char& c); istream& get(char*, streamsize, term = '\n'); // doesn't read term istream& getline(char*, streamsize, term = '\n'); // discards term istream& ignore(streamsize, term = eof); istream& read(char*, streamsize);
// Constructors (same constructors available for ifstream and ofstream) fstream(); explicit fstream(const char* filename, ios::openmode mode = default-mode);
// Functions to open and close files void open(const char* filename, ios::openmode mode = default-mode); void close(); bool is_open();
// Constructors (same constructors available for istringstream & ostringstream) explicit stringstream(ios::openmode mode = default-mode); explicit stringstream(const string& s, ios::openmode mode = default-mode);
// Get/set string string str() const; void str(const string& s);
// istrstream constructors: explicit istrstream(const char* s); // null-terminated istrstream(const char* s, streamsize size);
// ostrstream constructors: ostrstream(); // dynamically-allocated string // string allocated by caller: ostrstream(char* s, streamsize size, ios::openmode = ios::out);
// strstream constructors: strstream(); // dynamically-allocated string // string allocated by caller: ostrstream(char* s, streamsize size, ios::openmode = ios::in|ios::out);
// istrstream, ostrstream and strstream members: char* str(); // Calls freeze(true) & returns buffer. Call freeze(false) to thaw
// ostream and strstream (but not istrsteam) members: int pcount() const; // Number of chars in output buffer void freeze(bool f = true); // prevent stream from freeing or changing buffer
// fmtflags manipulators that take no arguments boolalpha, noboolalpha // set/clear ios::boolalpha showbase, noshowbase // set/clear ios::showbase showpoint, noshowpoint // set/clear ios::showpoint showpos, noshowpos, // set/clear ios::showpos skipws, noskipws, // set/clear ios::skipws uppercase, nouppercase // set/clear ios::uppercase unitbuf, nounitbuf // set/clear ios::unitbuf left, right, internal // set ios::left, ios::right or ios::internal dec, hex, oct // set ios::dec, ios::hex or ios::oct fixed, scientific // set ios::fixed or ios::scientific
// Manipulators that take an argument require <iomanips> header #include <iomanips> setiosflags(fmtflags f) // Set specified flags resetiosflags(fmtflags f) // Clear specified flags setbase(int b) // b may be 10 (dec), 16 (hex), or 8 (oct) setfill(char f) // Set fill character setprecision(int p) // Set floating point output precision setw(int w) // set field width for next output operation
// null-terminated string copy
char* strcpy(char* to, const char* from);
char* strncpy(char* to, const char* from, size_t maxlen);
// null-terminated string comparison.
// Returns -1 (s1<s2), 0 (s1==s2), or 1(s1>s2)
int strcmp(const char* s1, const char* s2);
// null-terminated string length
size_t strlen(char* s);
// Case conversion
char toupper(char c);
char tolower(char c);
// Character tests
bool isalnum(char c);
bool isalpha(char c);
bool isctrl(char c);
bool isdigit(char c);
bool isgraph(char c);
bool islower(char c);
bool isprint(char c);
bool ispunct(char c);
bool isspace(char c);
bool isupper(char c);
bool isxdigit(char c);
// Random-number generation
int rand();
void srand(int seed);
struct tm { int tm_sec; /* seconds after the minute -- [0,60] */ int tm_min; /* minutes after the hour -- [0,59] */ int tm_hour; /* hours since midnight -- [0,23] */ int tm_mday; /* day of the month -- [1,31] */ int tm_mon; /* months since January -- [0,11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday -- [0,6] */ int tm_yday; /* days since January 1 -- [0,365] */ int tm_isdst; /* Daylight Savings Time flag */ };// Get system time
// Compose time
time_t mktime(struct tm* timeptr);
// Decompose time
struct tm* gmtime(const time_t* timeval);
struct tm* localtime(const time_t* timeval);
// format time into a null-terminated string size_t strftime(char* nts, size_t ntsmax, const char* format, struct tm* timeptr); // Format string contains the following conversion codes: // %a abbreviated weekday name // %A full weekday name // %b abbreviated month name // %B full month name // %c locale's date and time representation // %d month as a number (01-31) // %H hour in 24-hour format (00-23) // %I hour in 12-hour format (01-12) // %j day of the year (001-366) // %m month as a number (01-12) // %M minute (00-59) // %p AM or PM (locale-specific) // %S second (00-59) // %U week of the year starting Sunday (0-53) // %w weekday as a number (0=Sunday to 6=Saturday) // %W week of the year starting Monday (0-53) // %x locale's date representation // %X locale's time representation // %y two-digit year (00-99) // %Y four-digit year (1900+) // %z time zone name // %% The percent symbol (%)
#include <cassert>
// NOTE: since assert is a macro, it is not in namespace std #define assert(cond) ...