C++ Standard Library Quick Reference (original) (raw)
Introduction
This quick reference is intended for readers of book, The C++ Standard Library from Scratch. It is not a full reference to the standard library. Descriptions, where provided are terse and are not intended to replace the more complete descriptions found in the book. Some elements are listed using actual class or function declarations; other elements are listed using a shorthand to make things simpler and/or more concise. Some elements of the standard library were omitted entirely because they are not included in the book. Conversely, many of the topics covered in the book are covered in even more detail here.
Numerous concepts were simplified in this quick reference. For example, in the Standard, string is actually a typedef for basic_string<char, char_traits, allocator >. 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.
Table of Contents
- Strings
- Containers
- Iterators
- Function Objects
- Algorithms
- Input/Output
- C Library Functions Used in the Book
Strings
String Argument Conventions
The member functions in this section take arguments with the following names:
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.
The string Class
#include
class string;
String Nested Types and Constants
The following types are nested within the string class:
Types
size_type difference_type iterator const_iterator reverse_iterator const_reverse_iterator
Constants
const size_type npos; // Maximum size_type
String Member Functions
Constructors
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);
Assignment Operators and Functions
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 Functions
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 and Capacity
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);
Element Access
char& operator[](size_type pos); const char& operator[](size_type pos) const; char& at(size_type pos); const char& at(size_type pos) const;
Append Functions and Operators
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);
Insert Functions
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);
Erase Functions
string& erase(size_type pos = 0, size_type len = npos); iterator& erase(iterator pos); iterator& erase(iterator start, iterator finish);
Replace Functions
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);
Comparison Functions
// 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);
Substrings
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);
Conversions
// 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;
Search Functions
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-Related Global Operators and Functions
The following functions and operators are related to the stringclass but are not members of string. Along with the stringclass, the following definitions are in the header:
// 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');
Containers
Container Types
Note: All container classes have an additional allocatortemplate parameter that defaults to class allocator<pair<const K, T> > for map and multimap and allocatorfor all other containers. The allocator parameter is omitted here for clarity.
Sequence Containers
Class definition | Header file |
---|---|
template class vector; | |
template class deque; | |
template class list; | |
class string; (see also separate section on strings)(A string can be used as a sequence container with element type char) |
Sorted Associative Containers
Class definition | Header file |
---|---|
template class set; | |
template class multiset; | |
template <class K, class T, class Comp> class map; | |
template <class K, class T, class Comp> class multimap; |
Nested Types for All Containers
The following types are defined within all container classes.
value_type iterator const_iterator reverse_iterator const_reverse_iterator size_type difference_type
Member Functions for All Containers
The following operations are common for all container types unless otherwise noted. The word container means one of the eight container classes listed above. Performance characteristics are in terms of the number of element operations involved. For example, the performance of insertdepends on the number of elements that must be moved to perform the insertion.
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 |
Container-Related Global Operators
The following operators are not members of any container class, but are implemented for all standard containers. The word container means one of the eight standard container classes. Both arguments must be of the same type.. They all take time that is proportional to the number of elements in the smaller argument (i.e. linear time).
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);
Sequence Containers Member Functions
Not all of the following operations are available for all sequence containers. The containers column contains a combination of the following codes to indicate which containers support the specified operation: V = vector, D = deque, L = list, S = string.
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 container(InIter start, InIter finish); (construct from iterator range) | linear | VDLS |
void assign(size_type num, const T& val); | linear | VDLS |
template 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 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 |
Sorted Associative Containers
Nested Types
set and multiset | 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; |
Member Functions
Not all of the following operations are available for all associative containers. The containers column contains a combination of the following codes to indicate which containers support the specified operation: s = set, S = multiset, m = map, M = multimap.
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 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 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 |
Special List Functions
The list container provides special functions to manipulate the contents of the list without actually copying the elements. This can provide a significant efficiency gain in the (relatively few) cases where this kind of manipulation is needed. Here is a concise description of these special functions:
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 list2and 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 void remove(UnaryPred pred);
Remove all elements that match the specified value or predicate.
void unique(); template 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 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 merge(list& list2, Compare cmp);
Merges two sorted lists into one. The elements are removed from list2 and added to *this. If a Comparepredicate is supplied, it is used to compare the elements instead of the< operator.
void reverse();
Reverses the order of elements in the list.
Container Adaptors
Stack
#include
template <class T, class Container = deque > 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(); };
Queue
#include
template <class T, class Container = deque > 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(); };
Priority_queue
#include
template <class T, class Container = vector, class Compare = lessContainer::value_type > class priority_queue { public: // Constructors explicit priority_queue(const Compare& comp = Compare(), const Container& c = Container()); template 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(); };
Iterators
Insertion Iterators
In each function prototype below, output-iterator represents a standard library classes that is not usually used directly. The result of calling one of these functions is generally passed to an algorithm.
#include
output-iterator back_inserter(Container& c);
output-iterator front_inserter(Container& c);
output-iterator inserter(Container& c, Iterator i);
Iterator Functions
#include
template<class Iter, difference_type> void advance(Iter& i, difference_type d);
template difference_type distance(Iter start, Iter finish);
Function Objects
Function Object Base Classes
#include
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; };
Standard Function Objects
#include
// Arithmetic function objects template struct plus; // binary + template struct minus; // binary - template struct multiplies; // binary * template struct divides; // binary / template struct modulus; // binary % template struct negate; // unary -
// Comparisons template struct equal_to; // == template struct not_equal_to; // != template struct less; // < template struct greater; // > template struct less_equal; // <= template struct greater_equal; // >=
// Logical operations template struct logical_and; // binary && template struct logical_or; // binary || template struct logical_not; // unary !
Negator and Binder Functions
The result of calling one of these adaptors is generally passed to an algorithm. In each prototype below, _unary-function-object_or _binary-function-object_represents a standard library template class that is not usually referenced directly but is just passed to another template function.
#include
template
unary-function-object not1(const UnaryPred& pred);
template
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);
Function Pointer Adaptors
These adaptors convert function pointers into function objects. The result of calling one of these adaptors is generally passed to an algorithm. In the prototypes below, _function-object_represents a standard library template class that is not usually referenced directly but is just passed to another template function
Pointer-to-Function Adaptors
The result of calling ptr_fun will be a unary function object if func takes one argument or a binary function object if functakes two arguments.
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));
Pointer-to-Member-Function Adaptors
The result of calling mem_fun is function object that takes a_pointer_ to T as its first argument. The result of callingmem_fun_ref is a function object that takes a _reference_to T as its first argument. The first argument is used as thethis value in the resulting member function call. If memfunctakes an argument, it is passed as the second argument to the function object. The memfunc pointer can point to a const or a non-const member function (although some non-conforming implementations do not yet support the const version).
// 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);
Algorithms
Others have already done a good job of creating references for the standard algorithms. You can see a few at these locations:
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 thecount 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);
Input/Output
Stream Classes
#include class ios; class istream : virtual public ios; class ostream : virtual public ios; class iostream : public istream, public ostream;
#include class stringstream : public iostream; class istringstream : public istream; class ostringstream : public ostream;
#include class fstream : public iostream; class ifstream : public istream; class ofstream : public ostream;
#include // (deprecated) class strstream : public iostream class istrstream : public istream class ostrstream : public ostream
ios Nested Types
The following types are defined within class ios and are inherited by all of the stream classes. They are all integral types that work as either bit masks or enumerations.
// 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
Format Flags
// 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
ios Member Functions
The following member functions of the ios base class are inherited by istream and ostream.
// 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
Unformatted Output Functions
The following functions are available in ostream, iostream, and derived classes:
ostream& put(char c); ostream& write(const char*, streamsize); ostream& flush();
Unformatted Input Functions
The following functions are available in istream, iostream, and derived classes:
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);
fstream Member Functions
In addition to the members it inherits from iostream and ios,fstream supplies the following member functions. In the constructors and open functions, default-mode is ios::in forifstream, ios::out for ofstream, and ios::in|ios::outfor fstream:
// 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();
stringstream Member Functions
In addition to the members it inherits from iostream and ios,stringstream supplies the following member functions. In the constructors,default-mode is ios::in for istringstream, ios::outfor ostringstream, and ios::in|ios::out for stringstream:
// 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);
strstream Member Functions
The strstream classes are deprecated, meaning that they may not be part of a future C++ standard. The stringstring classes are generally preferred. However, some legacy code still uses strstreamand some library implementations do not yet support stringstream.
// 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
I/O Manipulators
// 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 header #include 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
C Library Functions Used in the Book
Null-Terminated String Functions
#include
// 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);
Character Operations
#include
// 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 Numbers
#include
// Random-number generation
int rand();
void srand(int seed);
Time and Date
#include
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
time_t 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 (%)
Debugging Assertions
#include
// NOTE: since assert is a macro, it is not in namespace std #define assert(cond) ...