![]() |
![]() |
![]() |
<string>string
· char_allocator
· char_traits
· getline
· operator+
· operator!=
· operator==
· operator<
· operator<<
· operator<=
· operator>
· operator>=
· operator>>
· swap
Include the standard header <string>
to define the
class
string and various
supporting classes and functions.
// DECLARATIONS
class char_allocator;
class char_traits;
class string;
// FUNCTIONS
string operator+(
const string& left,
const string& right);
string operator+(
const string& left,
const char *right);
string operator+(
const string& left,
char right);
string operator+(
const char *left,
const string& right);
string operator+(
char left,
const string& right);
bool operator==(
const string& left,
const string& right);
bool operator==(
const string& left,
const char *right);
bool operator==(
const char *left,
const string& right);
bool operator!=(
const string& left,
const string& right);
bool operator!=(
const string& left,
const char *right);
bool operator!=(
const char *left,
const string& right);
bool operator<(
const string& left,
const string& right);
bool operator<(
const string& left,
const char *right);
bool operator<(
const char *left,
const string& right);
bool operator>(
const string& left,
const string& right);
bool operator>(
const string& left,
const char *right);
bool operator>(
const char *left,
const string& right);
bool operator<=(
const string& left,
const string& right);
bool operator<=(
const string& left,
const char *right);
bool operator<=(
const char *left,
const string& right);
bool operator>=(
const string& left,
const string& right);
bool operator>=(
const string& left,
const char *right);
bool operator>=(
const char *left,
const string& right);
void swap(
string& left,
string& right);
ostream& operator<<(
ostream& ostr,
const string& str);
istream& operator>>(
istream& istr,
string& str);
istream& getline(
istream& istr,
string& str);
istream& getline(
istream& istr,
string& str,
char delim);
// END OF DECLARATIONS
stringstring
· allocator_type
· append
· assign
· at
· begin
· c_str
· capacity
· clear
· compare
· const_iterator
· const_pointer
· const_reference
· const_reverse_iterator
· copy
· data
· difference_type
· empty
· end
· erase
· find
· find_first_not_of
· find_first_of
· find_last_not_of
· find_last_of
· get_allocator
· insert
· iterator
· length
· max_size
· npos
· operator+=
· operator=
· operator[]
· pointer
· push_back
· rbegin
· reference
· rend
· replace
· reserve
· resize
· reverse_iterator
· rfind
· size
· size_type
· substr
· swap
· traits_type
· value_type
class string {
public:
typedef char_traits traits_type;
typedef char_allocator allocator_type;
typedef T0 iterator;
typedef T1 const_iterator;
typedef T2 size_type;
typedef T3 difference_type;
class const_reverse_iterator;
class reverse_iterator;
typedef allocator_type::pointer
pointer;
typedef allocator_type::const_pointer
const_pointer;
typedef allocator_type::reference
reference;
typedef allocator_type::const_reference
const_reference;
typedef allocator_type::value_type
value_type;
static const size_type npos = -1;
string();
explicit string(const allocator_type& al);
string(const string& right);
string(const string& right, size_type roff,
size_type count = npos);
string(const string& right, size_type roff,
size_type count, const allocator_type& al);
string(const value_type *ptr, size_type count);
string(const value_type *ptr, size_type count,
const allocator_type& al);
string(const value_type *ptr);
string(const value_type *ptr,
const allocator_type& al);
string(size_type count, value_type ch);
string(size_type count, value_type ch,
const allocator_type& al);
string(const_iterator first,
const_iterator last);
string(const_iterator first,
const_iterator last,
const allocator_type& al);
allocator_type get_allocator() const;
string& operator=(const string& right);
string& operator=(const value_type *ptr);
string& operator=(value_type ch);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
const_reference at(size_type off) const;
reference at(size_type off);
const_reference operator[](size_type off) const;
reference operator[](size_type off);
const value_type *c_str() const;
const value_type *data() const;
size_type length() const;
size_type size() const;
size_type max_size() const;
void resize(size_type newsize, value_type ch = value_type());
size_type capacity() const;
void reserve(size_type count = 0);
bool empty() const;
string& operator+=(const string& right);
string& operator+=(const value_type *ptr);
string& operator+=(value_type ch);
void push_back(value_type ch);
string& append(const string& right);
string& append(const string& right,
size_type roff, size_type count);
string& append(const value_type *ptr,
size_type count);
string& append(const value_type *ptr);
string& append(size_type count, value_type ch);
string& append(const_iterator first,
const_iterator last);
string& assign(const string& right);
string& assign(const string& right,
size_type roff, size_type count);
string& assign(const value_type *ptr,
size_type count);
string& assign(const value_type *ptr);
string& assign(size_type count, value_type ch);
string& assign(const_iterator first,
const_iterator last);
string& insert(size_type off,
const string& right);
string& insert(size_type off,
const string& right, size_type roff,
size_type count);
string& insert(size_type off,
const value_type *ptr, size_type count);
string& insert(size_type off,
const value_type *ptr);
string& insert(size_type off,
size_type count, value_type ch);
iterator insert(iterator where,
value_type ch = value_type());
void insert(iterator where, size_type count, value_type ch);
void insert(iterator where,
const_iterator first, const_iterator last);
string& erase(size_type off = 0,
size_type count = npos);
iterator erase(iterator where);
iterator erase(iterator first, iterator last);
void clear();
string& replace(size_type off, size_type n0,
const string& right);
string& replace(size_type off, size_type n0,
const string& right, size_type roff,
size_type count);
string& replace(size_type off, size_type n0,
const value_type *ptr, size_type count);
string& replace(size_type off, size_type n0,
const value_type *ptr);
string& replace(size_type off, size_type n0,
size_type count, value_type ch);
string& replace(iterator first, iterator last,
const string& right);
string& replace(iterator first, iterator last,
const value_type *ptr, size_type count);
string& replace(iterator first, iterator last,
const value_type *ptr);
string& replace(iterator first, iterator last,
size_type count, value_type ch);
string& replace(iterator first, iterator last,
const_iterator first2, const_iterator last2);
size_type copy(value_type *ptr, size_type count,
size_type off = 0) const;
void swap(string& right);
size_type find(const string& right,
size_type off = 0) const;
size_type find(const value_type *ptr, size_type off,
size_type count) const;
size_type find(const value_type *ptr,
size_type off = 0) const;
size_type find(value_type ch, size_type off = 0) const;
size_type rfind(const string& right,
size_type off = npos) const;
size_type rfind(const value_type *ptr, size_type off,
size_type count = npos) const;
size_type rfind(const value_type *ptr,
size_type off = npos) const;
size_type rfind(value_type ch,
size_type off = npos) const;
size_type find_first_of(const string& right,
size_type off = 0) const;
size_type find_first_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_first_of(const value_type *ptr,
size_type off = 0) const;
size_type find_first_of(value_type ch,
size_type off = 0) const;
size_type find_last_of(const string& right,
size_type off = npos) const;
size_type find_last_of(const value_type *ptr,
size_type off, size_type count = npos) const;
size_type find_last_of(const value_type *ptr,
size_type off = npos) const;
size_type find_last_of(value_type ch,
size_type off = npos) const;
size_type find_first_not_of(const string& right,
size_type off = 0) const;
size_type find_first_not_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_first_not_of(const value_type *ptr,
size_type off = 0) const;
size_type find_first_not_of(value_type ch,
size_type off = 0) const;
size_type find_last_not_of(const string& right,
size_type off = npos) const;
size_type find_last_not_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_last_not_of(const value_type *ptr,
size_type off = npos) const;
size_type find_last_not_of(value_type ch,
size_type off = npos) const;
string substr(size_type off = 0,
size_type count = npos) const;
int compare(const string& right) const;
int compare(size_type off, size_type n0,
const string& right) const;
int compare(size_type off, size_type n0,
const string& right, size_type roff, size_type count) const;
int compare(const value_type *ptr) const;
int compare(size_type off, size_type n0,
const value_type *ptr) const;
int compare(size_type off, size_type n0,
const value_type *ptr, size_type roff) const;
};
The class describes an object that controls a
varying-length sequence of elements of type char,
also known as
value_type.
Various important properties of the elements
in a string
are described by the class
char_traits,
also known as
traits_type.
The object allocates and frees storage for the sequence it controls
through a stored allocator object of class
char_allocator, also known as
allocator_type.
Note that the stored allocator object is not copied when the container
object is assigned.
The sequences controlled by an object of class
string are usually called
strings. These objects should not be
confused, however, with the null-terminated
C strings used throughout the
Standard C++ library.
Many member functions require an operand sequence of elements. You can specify such an operand sequence several ways:
ch -- one element
with value chcount, ch -- a repetition of count elements each
with value chptr -- a null-terminated sequence
(such as a C string) beginning at
ptr (which must not be a null pointer),
where the terminating element is the value
value_type() and is not part of
the operand sequenceptr, count -- a sequence of count elements
beginning at ptr (which must not be a null pointer)right -- the sequence specified by the
string object rightright, roff, count -- the substring of the
string object right with up to count
elements (or through the end of the string, whichever comes first)
beginning at position rofffirst, last -- a sequence of elements delimited
by the iterators first and last, in the
range [first, last), which may overlap
the sequence controlled by the string object whose member function
is being calledIf a position argument
(such as roff above) is beyond the end of the string on a
call to a string member function, the function
reports an
out-of-range error by
throwing an object of class
out_of_range.
If a function is asked to generate a sequence longer than
max_size() elements,
the function reports a
length error by
throwing an object of class
length_error.
References, pointers, and iterators that designate elements of the
controlled sequence can become invalid after any call to a function
that alters the controlled sequence, or after the first call to the
non-const member functions
at,
begin,
end,
operator[],
rbegin, or
rend.
(The idea is to permit (but not require)
multiple strings to share the same representation
until one string becomes a candidate for change, at which point that string
makes a private copy of the representation, using a discipline called
copy on write.)
string::allocator_typetypedef char_allocator allocator_type;
The type is a synonym for
char_allocator.
string::appendstring& append(const value_type *ptr);
string& append(const value_type *ptr,
size_type count);
string& append(const string& right,
size_type roff, size_type count);
string& append(const string& right);
string& append(size_type count, value_type ch);
string& append(const_iterator first,
const_iterator last);
The
member functions each append the
operand sequence to the end of the
sequence controlled by *this,
then return *this.
string::assignstring& assign(const value_type *ptr);
string& assign(const value_type *ptr,
size_type count);
string& assign(const string& right,
size_type roff, size_type count);
string& assign(const string& right);
string& assign(size_type count, value_type ch);
string& assign(const_iterator first,
const_iterator last);
The
member functions each replace
the sequence controlled by *this with the
operand sequence, then return *this.
string::atconst_reference at(size_type off) const; reference at(size_type off);
The member functions each return a reference to the element of the
controlled sequence at position off,
or report an out-of-range error.
string::stringstring(const value_type *ptr);
string(const value_type *ptr,
const allocator_type& al);
string(const value_type *ptr, size_type count);
string(const value_type *ptr, size_type count,
const allocator_type& al);
string(const string& right);
string(const string& right, size_type roff,
size_type count = npos);
string(const string& right, size_type roff,
size_type count, const allocator_type& al);
string(size_type count, value_type ch);
string(size_type count, value_type ch,
const allocator_type& al);
string();
explicit string(const allocator_type& al);
string(const_iterator first, const_iterator last);
string(const_iterator first, const_iterator last,
const allocator_type& al);
All constructors store an
allocator object and
initialize the controlled sequence. The allocator object is the argument
al, if present. For the copy constructor, it is
right.get_allocator().
Otherwise, it is Alloc().
The controlled sequence is initialized to a copy of the operand sequence specified by the remaining operands. A constructor with no operand sequence specifies an empty initial controlled sequence.
string::beginconst_iterator begin() const; iterator begin();
The member functions each return a random-access iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).
string::c_strconst value_type *c_str() const;
The member function returns a pointer to a non-modifiable
C string constructed by adding a
terminating null element
(value_type()) to the controlled
sequence. Calling any non-const member function for
*this can invalidate the pointer.
string::capacitysize_type capacity() const;
The member function returns the storage currently allocated to hold
the controlled sequence, a value at least as large as
size().
string::clearvoid clear();
The member function calls
erase(
begin(),
end()).
string::compareint compare(const string& right) const;
int compare(size_type off, size_type n0,
const string& right) const;
int compare(size_type off, size_type n0,
const string& right, size_type roff, size_type count) const;
int compare(const value_type *ptr) const;
int compare(size_type off, size_type n0,
const value_type *ptr) const;
int compare(size_type off, size_type n0,
const value_type *ptr, size_type roff) const;
The member functions each compare up to n0 elements of the
controlled sequence beginning with position off, or the
entire controlled sequence if these arguments are not supplied,
to the operand sequence.
Each function returns:
traits_type::compare), or if the
two have a common prefix but the operand sequence is longerstring::const_iteratortypedef T1 const_iterator;
The type describes an object that can serve as a constant
random-access iterator for the controlled sequence.
It is described here as a
synonym for the implementation-defined type T1.
string::const_pointertypedef allocator_type::const_pointer
const_pointer;
The type is a synonym for allocator_type::const_pointer.
string::const_referencetypedef allocator_type::const_reference
const_reference;
The type is a synonym for allocator_type::const_reference.
string::const_reverse_iteratorclass const_reverse_iterator;
The type describes an object that can serve as a constant reverse iterator for the controlled sequence. You can, for example, access each of the elements in the controlled sequence in reverse order by writing:
string::const_reverse_iterator rit;
for (rit = rbegin(); rit != rend(); ++rit)
process *rit
string::copysize_type copy(value_type *ptr, size_type count,
size_type off = 0) const;
The member function copies up to count elements from the
controlled sequence, beginning at position off, to the
array of value_type beginning at ptr. It returns the
number of elements actually copied.
string::dataconst value_type *data() const;
The member function returns a pointer to the first element of the sequence (or, for an empty sequence, a non-null pointer that cannot be dereferenced).
string::difference_typetypedef T3 difference_type;
The signed integer type describes an object that can represent the
difference between the addresses of any two elements in the controlled
sequence. It is described here as a
synonym for the implementation-defined type T3.
string::emptybool empty() const;
The member function returns true for an empty controlled sequence.
string::endconst_iterator end() const; iterator end();
The member functions each return a random-access iterator that points just beyond the end of the sequence.
string::eraseiterator erase(iterator first, iterator last);
iterator erase(iterator where);
string& erase(size_type off = 0,
size_type count = npos);
The first member function removes the elements of the controlled
sequence in the range [first, last).
The second member function removes the element of the controlled
sequence pointed to by where.
Both return an iterator that designates the first element remaining
beyond any elements removed, or
end()
if no such element exists.
The third member function removes up to count elements of
the controlled sequence beginning at position off, then
returns *this.
string::findsize_type find(value_type ch, size_type off = 0) const;
size_type find(const value_type *ptr,
size_type off = 0) const;
size_type find(const value_type *ptr, size_type off,
size_type count) const;
size_type find(const string& right,
size_type off = 0) const;
The member functions each find the first (lowest beginning position)
subsequence in the controlled sequence, beginning on or after position
off, that matches the
operand sequence specified by the
remaining operands. If it succeeds, it returns the position where the
matching subsequence begins. Otherwise, the function returns
npos.
string::find_first_not_ofsize_type find_first_not_of(value_type ch,
size_type off = 0) const;
size_type find_first_not_of(const value_type *ptr,
size_type off = 0) const;
size_type find_first_not_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_first_not_of(const string& right,
size_type off = 0) const;
The member functions each find the first (lowest position) element of the
controlled sequence, at or after position off, that
matches none of the elements in the
operand sequence specified by the
remaining operands. If it succeeds, it returns the position. Otherwise,
the function returns
npos.
string::find_first_ofsize_type find_first_of(value_type ch,
size_type off = 0) const;
size_type find_first_of(const value_type *ptr,
size_type off = 0) const;
size_type find_first_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_first_of(const string& right,
size_type off = 0) const;
The member functions each find the first (lowest position) element of the
controlled sequence, at or after position off, that
matches any of the elements in the
operand sequence specified by the
remaining operands. If it succeeds, it returns the position. Otherwise,
the function returns
npos.
string::find_last_not_ofsize_type find_last_not_of(value_type ch,
size_type off = npos) const;
size_type find_last_not_of(const value_type *ptr,
size_type off = npos) const;
size_type find_last_not_of(const value_type *ptr,
size_type off, size_type count) const;
size_type find_last_not_of(const string& right,
size_type off = npos) const;
The member functions each find the last (highest position) element of the
controlled sequence, at or before position off, that
matches none of the elements in the
operand sequence specified by the
remaining operands. If it succeeds, it returns the position. Otherwise,
the function returns
npos.
string::find_last_ofsize_type find_last_of(value_type ch,
size_type off = npos) const;
size_type find_last_of(const value_type *ptr,
size_type off = npos) const;
size_type find_last_of(const value_type *ptr,
size_type off, size_type count = npos) const;
size_type find_last_of(const string& right,
size_type off = npos) const;
The member functions each find the last (highest position) element of the
controlled sequence, at or before position off, that
matches any of the elements in the
operand sequence specified by the
remaining operands. If it succeeds, it returns the position. Otherwise,
the function returns
npos.
string::get_allocatorallocator_type get_allocator() const;
The member function returns the stored allocator object.
string::insertstring& insert(size_type off, const value_type *ptr);
string& insert(size_type off, const value_type *ptr,
size_type count);
string& insert(size_type off,
const string& right);
string& insert(size_type off,
const string& right, size_type roff, size_type count);
string& insert(size_type off,
size_type count, value_type ch);
iterator insert(iterator where,
value_type ch = value_type());
void insert(iterator where,
const_iterator first, const_iterator last);
void insert(iterator where, size_type count, value_type ch);
The member functions each insert, before position off or
before the element pointed to by where in the controlled
sequence, the
operand sequence specified by the
remaining operands. A function that returns a value returns
*this.
string::iteratortypedef T0 iterator;
The type describes an object that can serve as a random-access
iterator for the controlled sequence.
It is described here as a
synonym for the implementation-defined type T0.
string::lengthsize_type length() const;
The member function returns the length of the controlled sequence
(same as size()).
string::max_sizesize_type max_size() const;
The member function returns the length of the longest sequence that the object can control.
string::nposstatic const size_type npos = -1;
The constant is the largest representable value of type
size_type. It is
assuredly larger than
max_size(), hence
it serves as either a very large value or as a special code.
string::operator+=string& operator+=(value_type ch); string& operator+=(const value_type *ptr); string& operator+=(const string& right);
The operators each append the
operand sequence to the end of the
sequence controlled by *this, then return *this.
string::operator=string& operator=(value_type ch); string& operator=(const value_type *ptr); string& operator=(const string& right);
The operators each replace the sequence controlled by *this
with the
operand sequence,
then return *this.
string::operator[]const_reference operator[](size_type off) const; reference operator[](size_type off);
The member functions each return a reference to the element of the
controlled sequence at position off. If that position is
invalid, the behavior is undefined. Note, however, that
cstr[cstr.size()] == 0 for the first member function.
string::pointertypedef allocator_type::pointer
pointer;
The type is a synonym for allocator_type::pointer.
string::push_backvoid push_back(value_type ch);
The member function effectively calls
insert(
end(), ch).
string::rbeginconst_reverse_iterator rbegin() const; reverse_iterator rbegin();
The member function returns a reverse iterator that points just beyond the end of the controlled sequence. Hence, it designates the beginning of the reverse sequence.
string::referencetypedef allocator_type::reference
reference;
The type is a synonym for allocator_type::reference.
string::rendconst_reverse_iterator rend() const; reverse_iterator rend();
The member functions each return a reverse iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). Hence, the function designates the end of the reverse sequence.
string::replacestring& replace(size_type off, size_type n0,
const value_type *ptr);
string& replace(size_type off, size_type n0,
const value_type *ptr, size_type count);
string& replace(size_type off, size_type n0,
const string& right);
string& replace(size_type off, size_type n0,
const string& right, size_type roff, size_type count);
string& replace(size_type off, size_type n0,
size_type count, value_type ch);
string& replace(iterator first, iterator last,
const value_type *ptr);
string& replace(iterator first, iterator last,
const value_type *ptr, size_type count);
string& replace(iterator first, iterator last,
const string& right);
string& replace(iterator first, iterator last,
size_type count, value_type ch);
string& replace(iterator first, iterator last,
const_iterator first2, const_iterator last2);
The member functions each replace up to n0 elements of the
controlled sequence beginning with position off, or the
elements of the controlled sequence beginning with the one pointed to by
first, up to but not including last. The
replacement is the
operand sequence specified by the
remaining operands. The function then returns
*this.
string::reservevoid reserve(size_type count = 0);
The member function ensures that
capacity()
henceforth returns at least count.
string::resizevoid resize(size_type newsize, value_type ch = value_type());
The member function ensures that
size() henceforth
returns newsize. If it must make the controlled sequence longer,
it appends elements with value ch.
To make the controlled sequence shorter, the member function effectively calls
erase(begin() + newsize, end()).
string::reverse_iteratorclass reverse_iterator;
The type describes an object that can serve as a reverse iterator for the controlled sequence. You can, for example, access each of the elements in the controlled sequence in reverse order by writing:
string::reverse_iterator rit;
for (rit = rbegin(); rit != rend(); ++rit)
process *rit
string::rfindsize_type rfind(value_type ch, size_type off = npos) const;
size_type rfind(const value_type *ptr,
size_type off = npos) const;
size_type rfind(const value_type *ptr,
size_type off, size_type count = npos) const;
size_type rfind(const string& right,
size_type off = npos) const;
The member functions each find the last
(highest beginning position) subsequence in
the controlled sequence, beginning on or before position off,
that matches the
operand sequence specified by the
remaining operands. If it succeeds, the function returns the position where the
matching subsequence begins. Otherwise, it returns
npos.
string::sizesize_type size() const;
The member function returns the length of the controlled sequence.
string::size_typetypedef T2 size_type;
The unsigned integer type describes an object that can represent the
length of any controlled sequence. It is described here as a
synonym for the implementation-defined type T2.
string::substrstring substr(size_type off = 0,
size_type count = npos) const;
The member function returns an object whose controlled sequence is a
copy of up to count elements of the controlled sequence
beginning at position off.
string::swapvoid swap(string& right);
The member function swaps the controlled sequences between
*this and str. If
get_allocator()
== right.get_allocator(), it does so in constant time,
it throws no exceptions, and it invalidates no references, pointers,
or iterators that designate elements in the two controlled sequences.
Otherwise, it performs a number of element assignments and constructor calls
proportional to the number of elements in the two controlled sequences.
string::traits_typetypedef char_traits traits_type;
The type is a synonym for
char_traits.
string::value_typetypedef allocator_type::value_type
value_type;
The type is a synonym for allocator_type::value_type.
char_allocatorclass char_allocator {
public:
char_allocator();
pointer allocate(size_type count, const void *hint);
void deallocate(pointer ptr, size_type count);
size_type max_size() const;
bool operator==(char_allocator& left,
char_allocator& right) const;
};
The class describes an object that manages
storage allocation and freeing for arrays of objects of type char.
An object of class char_allocator is the
allocator object
used by class
string.
It is used here primarily to minimize differences with full Standard C++.
char_allocator::allocatepointer allocate(size_type count, const void *hint);
The member function allocates storage for
an array of count elements of type char, by calling
operator new(count).
It returns a pointer to the allocated object.
The hint argument is unused here. To supply no
hint, use a null pointer argument instead.
char_allocator::char_allocatorchar_allocator();
The constructor does nothing.
char_allocator::deallocatevoid deallocate(pointer ptr, size_type count);
The member function frees storage for
the array of count objects of type
char beginning at ptr, by calling
operator delete(ptr).
The pointer ptr must have been earlier returned by a call to
allocate for an allocator
object that compares equal to *this, allocating an array object
of the same size and type.
char_allocator::max_sizesize_type max_size() const;
The member function returns the length of the longest sequence
of elements of type char that an object of class
char_allocator might be able to allocate.
char_allocator::operator==bool operator==(char_allocator& left,
char_allocator& right) const;
The operator returns true. (Two allocator objects should compare equal only if an object allocated through one can be deallocated through the other. If the value of one object is determined from another by assignment or by construction, the two object should compare equal.)
char_traitsclass char_traits {
public:
typedef char char_type;
typedef int int_type;
typedef streampos pos_type;
typedef streamoff off_type;
typedef mbstate_t state_type;
static void assign(char_type& left, const char_type& right);
static char_type *assign(char_type *first, size_t count,
char_type ch);
static bool eq(const char_type& left,
const char_type& right);
static bool lt(const char_type& left,
const char_type& right);
static int compare(const char_type *first1,
const char_type *first2, size_t count);
static size_t length(const char_type *first);
static char_type *copy(char_type *first1,
const char_type *first2, size_t count);
static char_type *move(char_type *first1,
const char_type *first2, size_t count);
static const char_type *find(const char_type *first,
size_t count, const char_type& ch);
static char_type to_char_type(const int_type& meta);
static int_type to_int_type(const char_type& ch);
static bool eq_int_type(const int_type& left,
const int_type& right);
static int_type eof();
static int_type not_eof(const int_type& meta);
};
The class describes various
character traits
for type char.
The class
string
as well as several iostreams classes, including
ios, use this information
to manipulate elements of type char.
None of the member functions of class char_traits may
throw exceptions.
char_traits::assignstatic void assign(char_type& left, const char_type& right);
static char_type *assign(char_type *first, size_t count,
char_type ch);
The first static member function assigns right
to left. The second static member function assigns ch
to each element X[N] for N
in the range [0, count), then returns first
char_traits::char_typetypedef char char_type;
The type is a synonym for char.
char_traits::comparestatic int compare(const char_type *first1,
const char_type *first2, size_t count);
The static member function compares the sequence of length count
beginning at first1to the sequence of the same length beginning
at first2. The function returns:
first1
(as determined by
eq) compares less
than the corresponding element in first2 (as determined by
lt)char_traits::copystatic char_type *copy(char_type *first1, const char_type *first2,
size_t count);
The static member function copies the sequence of count
elements beginning at first2 to the array beginning at first1,
then returns first1. The source and destination
must not overlap.
char_traits::eofstatic int_type eof();
The static member function returns a value that represents
end-of-file (EOF).
char_traits::eqstatic bool eq(const char_type& left, const char_type& right);
The static member function returns true if left compares
equal to right.
char_traits::eq_int_typestatic bool eq_int_type(const int_type& left,
const int_type& right);
The static member function returns true if
left compares equal to right.
char_traits::findstatic const char_type *find(const char_type *first,
size_t count, const char_type& ch);
The static member function determines the lowest N
in the range [0, count) for which
eq(first[N], ch)
is true. If successful, it returns first + N. Otherwise,
it returns a null pointer.
char_traits::int_typetypedef int int_type;
The type is a synonym for int.
char_traits::lengthstatic size_t length(const char_type *first);
The static member function returns the number of elements
N in the sequence beginning at first
up to but not including the element first[N] which
compares equal to char_type().
char_traits::ltstatic bool lt(const char_type& left, const char_type& right);
The static member function returns true if left compares
less than right.
char_traits::movestatic char_type *move(char_type *first1, const char_type *first2,
size_t count);
The static member function copies the sequence of count
elements beginning at first2 to the array beginning at first1,
then returns first1. The source and destination may overlap.
char_traits::not_eofstatic int_type not_eof(const int_type& meta);
If
!eq_int_type(
eof(), meta),
the static member function returns meta.
Otherwise, it returns a value other than
eof().
char_traits::off_typetypedef streamoff off_type;
The type is a synonym for
streamoff.
char_traits::pos_typetypedef streampos pos_type;
The type is a synonym for
streampos.
char_traits::state_typetypedef mbstate_t state_type;
The type is a synonym for
mbstate_t.
char_traits::to_char_typestatic char_type to_char_type(const int_type& meta);
The static member function returns meta represented as
type Elem. A value of meta that cannot be so
represented yields an unspecified result.
char_traits::to_int_typestatic int_type to_int_type(const char_type& ch);
The static member function returns ch represented as
type int_type. It must be possible to convert any value ch of type
Elem to int_type (by evaluating
meta = to_int_type(ch))
then back to Elem (by evaluating
ch = to_char_type(meta))
and obtain a value that compares equal to ch.
getlineistream& getline(istream& istr,
string& str);
istream& getline(istream& istr,
string& str, char delim);
The first function returns getline(istr, str, istr.widen('\n')).
The second function replaces the sequence controlled by
str with a sequence of elements extracted from the stream
istr. In order of testing, extraction stops:
delim, in which case the element is neither put back nor
appended to the controlled sequencestr.max_size()
elements, in which case the function calls
setstate(ios_base::failbit).If the function extracts no elements, it calls
setstate(failbit).
In any case, it returns istr.
operator+string operator+(
const string& left,
const string& right);
string operator+(
const string& left,
const char *right);
string operator+(
const string& left,
char right);
string operator+(
const char *left,
const string& right);
string operator+(
char left,
const string& right);
The functions each overload operator+ to
concatenate two objects of class
string.
All effectively return
string(left).append(right).
operator!=bool operator!=(
const string& left,
const string& right);
bool operator!=(
const string& left,
const char *right);
bool operator!=(
const char *left,
const string& right);
The functions each overload operator!= to compare
two objects of class
string. All effectively
return string(left).compare(right)
!= 0.
operator==bool operator==(
const string& left,
const string& right);
bool operator==(
const string& left,
const char *right);
bool operator==(
const char *left,
const string& right);
The functions each overload operator== to compare
two objects of class
string. All effectively
return string(left).compare(right)
== 0.
operator<bool operator<(
const string& left,
const string& right);
bool operator<(
const string& left,
const char *right);
bool operator<(
const char *left,
const string& right);
The functions each overload operator< to
compare two objects of class
string. All effectively
return string(left).compare(right)
< 0.
operator<<ostream& operator<<(
ostream& ostr,
const string& str);
The function is a
formatted output functions
that overloads operator<< to
determine the length len =
str.size()
of the sequence controlled by str, and insert the sequence. If
len < ostr.width(),
then the function also inserts a repetition of ostr.width() - len
fill characters.
The repetition precedes the sequence if
(ostr.flags() &
adjustfield !=
left.
Otherwise, the repetition follows the sequence.
The function returns ostr.
operator<=bool operator<=(
const string& left,
const string& right);
bool operator<=(
const string& left,
const char *right);
bool operator<=(
const char *left,
const string& right);
The functions each overload operator<= to
compare two objects of class
string. All effectively
return string(left).compare(right)
<= 0.
operator>bool operator>(
const string& left,
const string& right);
bool operator>(
const string& left,
const char *right);
bool operator>(
const char *left,
const string& right);
The functions each overload operator> to
compare two objects of class
string. All effectively
return string(left).compare(right)
> 0.
operator>=bool operator>=(
const string& left,
const string& right);
bool operator>=(
const string& left,
const char *right);
bool operator>=(
const char *left,
const string& right);
The functions each overload operator>= to
compare two objects of class
string. All effectively
return string(left).compare(right)
>= 0.
operator>>istream& operator>>(
istream& istr,
string& str);
The template function overloads operator>> to
replace the sequence controlled by str with a sequence of
elements extracted from the stream istr. Extraction stops:
istr.width()
elements, if that value is nonzeroistr.max_size()
elementsch for which
isspace(ch)
is true, in which case the character is put backIf the function extracts no elements, it calls
setstate(ios_base::failbit).
In any case, it calls istr.width(0) and
returns *this.
swaptemplate<class Tr, class Alloc>
void swap(
string& left,
string& right);
The template function executes
left.swap(right).
See also the Table of Contents and the Index.
Copyright © 1992-2006 by P.J. Plauger. All rights reserved.
![]() |
![]() |
![]() |