We're always interested in getting feedback. E-mail us if you like this guide, if you think that important material is omitted, if you encounter errors in the code examples or in the documentation, if you find any typos, or generally just if you feel like e-mailing. Send your email to Frank Brokken.Please state the document version you're referring to, as found in the title (in this document: 4.4.2).
C++ offers a large number of facilities to implement solutions for common problems. Most of these facilities are part of the Standard Template Library or they are implemented as generic algorithms (see chapter 11).
Among the facilities C++ programmers have developed over and over again (as reflected in the Annotations) are those for manipulating chunks of text, commonly called strings. The C programming language offers rudimentary string support: the ascii-z terminated series of characters is the foundation on which a large amount of code has been built.
Standard C++ now offers a string type of its own. In order to use
string-type objects, the header file string must be included in
sources.
Actually, string objects are class type variables, and the class
is introduced for the first time in chapter 5. However, in order to
use a string, it is not necessary to know what a class is. In this section the
operators that are available for strings and some other operations are
discussed. The operations that can be performed on strings take the form
stringVariable.operation(argumentList) For example, if string1 and string2 are variables of type string,
then
string1.compare(string2) can be used to compare both strings. A function like compare(), which is
part of the string-class is called a member function. The string
class offers a large number of these member functions, as well as extensions of
some well-known operators, like the assignment (=) and the comparison
operator (==). These operators and functions are discussed in the
following sections.
string::npos is returned. This value is a (symbolic) value
of type string::size_type, which is (for all practical purposes) an
int.
Note that in all operations where string objects can be used as arguments,
char const * values and variables can be used as well.
Some string-member functions use iterators. Iterators will be covered
in section 11.1. The member functions that use iterators are listed
in the next section (4.2), they are not further illustrated
below.
The following operations can be performed on strings:
ascii-z string, another string object, or an implicit initialization
can be used. In the example, note that the implicit initialization does not
have an argument, and does not use the function argumentlist notation.
#include <string>
int main()
{
string
stringOne("Hello World"), // using plain ascii-Z
stringTwo(stringOne), // using another string object
stringThree; // implicit initialization to ""
// do not use: stringThree();
return (0);
}
= operator) can be used, which accepts both a
string object and a C-style characterstring as its right-hand
argument:
#include <string>
int main()
{
string
stringOne("Hello World"),
stringTwo;
stringTwo = stringOne; // assign stringOne to stringTwo
stringTwo = "Hello world"; // assign a C-string to StringTwo
return (0);
}
string-object. The reverse conversion (converting a
string object to a standard C-string) is not performed
automatically. In order to obtain the C-string that is stored within the
string object itself, the member function c_str(), which returns a
char const *, can be used:
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello World");
char const
*Cstring = stringOne.c_str();
cout << Cstring << endl;
return (0);
}
[]) is available,
but not the pointer dereferencing operator (*). The subscript operator
does not perform range-checking. If range-checking is required, the at()
member function can be used instead of the subscript-operator:
#include <string>
int main()
{
string
stringOne("Hello World");
stringOne[6] = 'w'; // now "Hello world"
if (stringOne[0] == 'H')
stringOne[0] = 'h'; // now "hello world"
// THIS WON'T COMPILE:
// *stringOne = 'H';
// Now using the at() memberfunction:
stringOne.at(6) =
stringOne.at(0); // now "Hello Horld"
if (stringOne.at(0) == 'H')
stringOne.at(0) = 'W'; // now "Wello Horld"
return (0);
}
When an illegal index is passed to the at() member function, the
program aborts.
==, !=, <, <=, > and >= operators or the compare() member function
can be used. The compare() member function comes in different flavors, the
plain one (having another string object as argument) offers a bit more
information than the operators do. The returnvalue of the compare()
member function may be used for lexicographical ordering: a negative value is
returned if the string stored in the string object using the compare()
member function (in the example: stringOne) is located earlier in the
alphabet (based on the standard ascii-characterset) than the string stored in
the string object passed as argument to the compare()
member function.
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello World"),
stringTwo;
if (stringOne != stringTwo)
stringTwo = stringOne;
if (stringOne == stringTwo)
stringTwo = "Something else";
if (stringOne.compare(stringTwo) > 0)
cout << "stringOne after stringTwo in the alphabet\n";
else if (stringOne.compare(stringTwo) < 0)
cout << "stringOne before stringTwo in the alphabet\n";
else
cout << "Both strings are the same";
// Alternatively:
if (stringOne > stringTwo)
cout << "stringOne after stringTwo in the alphabet\n";
else if (stringOne < stringTwo)
cout << "stringOne before stringTwo in the alphabet\n";
else
cout << "Both strings are the same";
return (0);
}
There is no member function to perform a case insensitive comparison of
strings.
Overloaded forms of the compare() member function have one or
two extra arguments.
compare() member function is used with two arguments, then
the second argument is an index position in the current string-object. It
indicates the index position in the current string object where the
comparison should start.
compare() member function is used with three arguments,
then the third argument indicates the number of characters that
should be compared.
compare()
function.
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello World");
// comparing from a certain offset in stringOne
if (!stringOne.compare("ello World", 1))
cout << "comparing 'Hello world' from index 1"
" to 'ello World': ok\n";
// comparing from a certain offset in stringOne over a certain
// number of characters in "World and more"
if (!stringOne.compare("World and more", 6, 5))
cout << "comparing 'Hello World' from index 6 over 5 positions"
" to 'World and more': ok\n";
// The same, but this fails, as all of the chars in stringOne
// starting at index 6 are compared, not just 3 chars.
// number of characters in "World and more"
if (!stringOne.compare("World and more", 6, 3))
cout << "comparing 'Hello World' from index 6 over 3 positions"
" to 'World and more': ok\n";
else
cout << "Unequal (sub)strings\n";
return (0);
}
string can be appended to another string. For this the +=
operator can be used, as well as the append() member function. Like the
compare() function, the append() member function may have two extra
arguments. The first argument is the string to be appended, the second
argument specifies the index position of the first character that will be
appended. The third argument specifies the number of characters that will be
appended. If the first argument is of type char const *, only a second
argument may be specified. In that case, the second argument specifies the
number of characters of the first argument that are appended to the string
object. Furthermore, the + operator can be used to append two strings
within an expression:
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello"),
stringTwo("World");
stringOne += " " + stringTwo;
stringOne = "hello";
stringOne.append(" world");
// append only 5 characters:
stringOne.append(" ok. >This is not used<", 5);
cout << stringOne << endl;
string
stringThree("Hello");
// append " World":
stringThree.append(stringOne, 5, 6);
cout << stringThree << endl;
return (0);
}
The + operator can be used in cases where at least one term of the +
operator is a string object (the other term can be a string, char const
* or char).
When neither operand of the + operator is a string, at least one
operand must be converted to a string object first. An easy way
to do this is to use an anonymous string object:
string("hello") + " world"; append() member function is used to append characters at
the end of a string. It is also possible to insert characters
somewhere within a string. For this the member function insert() is
available.
The insert() member function to insert (parts of) a string has at least
two, and at most four arguments:
string object
where another string should be inserted.
string-argument that will be inserted.
char const *, the fourth argument is
not available. In that case, the third argument indicates the number of
characters of the provided char const * value that will be inserted.
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hell ok.");
stringOne.insert(4, "o "); // Insert "o " at position 4
string
world("The World of C++");
// insert "World" into stringOne
stringOne.insert(6, world, 4, 5);
cout << "Guess what ? It is: " << stringOne << endl;
return (0);
}
Several other variants of insert() are available. See section
4.2 for details.
string objects must be replaced by
other information. To replace parts of the contents of a string object by
another string the member function replace() can be used.
The member function has at least three and possibly five arguments, having
the following meanings
(see section 4.2 for overloaded versions of
replace(), using different types of arguments):
string or char const *).
string-argument that will be inserted.
char const *, the fifth argument is
not available. In that case, the fourth argument indicates the number of
characters of the provided char const * value that will be inserted.
The following example shows a very simple filechanger: it reads lines from
cin, and replaces occurrences of a `searchstring' by a
`replacestring'. Simple tests for the correct number of arguments and the
contents of the provided strings (they should be unequal) are implemented
using the assert() macro.
#include <iostream>
#include <string>
#include <cassert>
int main(int argc, char **argv)
{
assert(argc == 3 &&
"Usage: <searchstring> <replacestring> to process stdin");
string
line,
search(argv[1]),
replace(argv[2]);
assert(search != replace);
while (getline(cin, line))
{
while (true)
{
string::size_type
idx;
idx = line.find(search);
if (idx == string::npos)
break;
line.replace(idx, search.size(), replace);
}
cout << line << endl;
}
return (0);
}
swap() swaps the contents of two string-objects. For example:
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello"),
stringTwo("World");
cout << "Before: stringOne: " << stringOne << ", stringTwo: "
<< stringTwo << endl;
stringOne.swap(stringTwo);
cout << "After: stringOne: " << stringOne << ", stringTwo: "
<< stringTwo << endl;
return (0);
}
erase() is available. The standard
form has two optional arguments:
string() or string("")).
erase(). An
example of the use of erase() is given below:
#include <string>
int main()
{
string
stringOne("Hello Cruel World");
stringOne.erase(5, 6);
cout << stringOne << endl;
stringOne.erase();
cout << "'" << stringOne << "'\n";
return (0);
}
string the member function find() can
be used. This function looks for the string that is provided as its first
argument in the string object calling find() and returns the index of
the first character of the substring if found. If the string is not found
string::npos is returned. The member function rfind() looks for the
substring from the end of the string object back to its beginning. An
example using find() was given earlier.
string object, the member function
substr() is available. The returned string object contains a copy of
the substring in the string-object calling substr() The member function
has two optional arguments:
string itself is returned.
#include <string>
int main()
{
string
stringOne("Hello World");
cout << stringOne.substr(0, 5) << endl
<< stringOne.substr(6) << endl
<< stringOne.substr() << endl;
return (0);
}
find() is used to find a substring, the functions
find_first_of(), find_first_not_of(), find_last_of() and
find_last_not_of() can be used to find sets of characters
(Unfortunately, regular expressions are not supported here). The
following program reads a line of text from the standard input stream, and
displays the substrings starting at the first vowel, starting at the last
vowel, and not starting at the first digit:
#include <string>
int main()
{
string
line;
getline(cin, line);
string::size_type
pos;
cout << "Line: " << line << endl
<< "Starting at the first vowel:\n"
<< "'"
<< (
(pos = line.find_first_of("aeiouAEIOU")) != string::npos ?
line.substr(pos)
:
"*** not found ***"
) << "'\n"
<< "Starting at the last vowel:\n"
<< "'"
<< (
(pos = line.find_last_of("aeiouAEIOU")) != string::npos ?
line.substr(pos)
:
"*** not found ***"
) << "'\n"
<< "Not starting at the first digit:\n"
<< "'"
<< (
(pos = line.find_first_not_of("1234567890"))
!= string::npos ?
line.substr(pos)
:
"*** not found ***"
) << "'\n";
return (0);
}
size() member function, which, like the standard C function
strlen() does not include the terminating ascii-Z character. For example:
#include <iostream>
#include <string>
int main()
{
string
stringOne("Hello World");
cout << "The length of the stringOne string is "
<< stringOne.size() << " characters\n";
return (0);
}
resize() can be used to make it longer or shorter. Note
that operators like + automatically resize the string when needed.
size() member function can be used to determine whether a
string holds no characters as well. Alternatively, the empty()
member function can be used:
#include <iostream>
#include <string>
int main()
{
string
stringOne;
cout << "The length of the stringOne string is "
<< stringOne.size() << " characters\n"
"It is " << (stringOne.empty() ? "" : " not ")
<< "empty\n";
stringOne = "";
cout << "After assigning a \"\"-string to a string-object\n"
"it is " << (stringOne.empty() ? "also" : " not")
<< " empty\n";
return (0);
}
istream &getline(istream instream, string target, char
delimiter) member function may be used to read a line of text (up to
the first delimiter or the end of the stream) from instream.
The delimiter has a default value '\n'. It is removed from instream,
but it is not stored in target. The function getline() was used in
several earlier examples (e.g., with the replace()
member function).
string-initializers, the string-iterators, the
string-operators and the string-member functions.
The member functions are ordered alphabetically by the name of the
operation. Below, object is a string-object, and argument is
either a string or a char const *, unless overloaded versions tailored
to string and char const * parameters are explicitly
mentioned. Object is used in cases where a string object is
initialized or given a new value. Argument remains unchanged.
With member functions the types of the parameters are given in a function-prototypical way. With several member functions iterators are used. At this point in the Annotations it's a bit premature to discuss iterators, but for referential purposes they have to be mentioned nevertheless. So, a forward reference is used here: see section 11.1 for a more detailed discussion of iterators.
Finally, note that all string-member functions returning indices in
object return the predefined constant string::npos if no
suitable index could be found.
string object:
Initializes object to an empty string.
string object(string::size_type n, char c):
Initializesobjectwithncharactersc.
string object(string argument):
Initializesobjectwithargument.
string object(string argument, string::size_type idx,
string::size_type n = pos):
Initializesobjectwithargument, usingncharacters ofargument, starting at indexidx.
string object(InputIterator begin, InputIterator end):
Initializesobjectwith the range of characters implied by the providedInputIterators.
string-iteratorsbegin()
end()
rbegin()
rend()
string-operatorsobject = argument.
Assignment ofargumenttoobject. May also be used for initializingstringobjects.
object = c.
Assignment ofchar ctoobject. May not be used for initializingstringobjects.
object += argument.
Appendsargumenttoobject.Argumentmay also be acharvalue.
argument1 + argument2.
Within expressions,stringsmay be added. At least one term of the expression (the left-hand term or the right-hand term) should be astringobject. The other term may be astring, achar const *value or acharvalue, as illustrated by the following example:
void fun()
{
char const
*asciiz = "hello";
string
first = "first",
second;
// all expressions compile ok:
second = first + asciiz;
second = asciiz + first;
second = first + 'a';
second = 'a' + first;
}
object[string::size_type pos].
The subscript-operator may be used to assign individual characters ofobjector to retrieve these characters. There is no range-checking. If range checking is required, use theat()member function, summarized earlier.
argument1 == argument2.
The equality operator may be used to compare astringobject to anotherstringorchar const *value. The operator!=is available as well. The returnvalue is abool, which istrueif the two strings are equal (i.e., contain the same characters).!=returnsfalsein that case.
argument1 < argument2.
The less-than operator may be used to compare the ordering within the Ascii-character set ofargument1andargument2. The operators<=, >and>=are available as well.
ostream stream; stream << argument.
The insertion-operator may be used with string objects.
istream stream; stream >> object.
The extraction-operator may be used withstringobjects. It operates analogously to the extraction of characters into a character array, butobjectis automatically resized to the required number of characters.
char &object.at(string::size_type pos):
The character (reference) at the indicated position is returned (it may be reassigned). The member function performs range-checking, aborting the program if an invalid index is passed.
string &object.append(InputIterator begin, InputIterator end):
Using this member function the range of characters implied by thebeginandend InputIteratorsare appended toobject.
string &object.append(string argument, string::size_type pos = 0;
string::size_type n = string::npos):
If
- If only
argumentis given, it is appended toobject.- If
posis specified as well,argumentis appended from index positionposuntil the end ofargument.- If all three arguments are provided,
ncharacters ofargument, starting at index positionposare appended toobject.argumentis of typechar const *, parameterposis not available. So, withchar const *arguments, either all characters or an initial subset of the characters of the providedchar const *argument are appended toobject.
string &object.append(string::size_type n, char c): Using this member function,ncharactersccan be appended toobject.
string &object.assign(string argument, string::size_type pos = 0;
string::size_type n = string::npos):
If
- If only
argumentis given, it is assigned toobject.- If
posis specified as well,objectis assigned from index positionposuntil the end ofargument.- If all three arguments are provided,
ncharacters ofargument, starting at index positionposare assigned toobject.argumentis of typechar const *, no parameterposis available. So, withchar const *arguments, either all characters or an initial subset of the characters of the providedchar const *argument are assigned toobject.
string &object.assign(string::size_type n, char c): Using this member function,ncharactersccan be assigned toobject.
string::size_type object.capacity():
returns the number of characters that can currently be
stored inside object.
int object.compare(string argument, string::size_type pos,
string::size_type n):
This member function may be used to compare (according to the ascii-character set) the strings stored inobjectandargument. The parameternmay be used to specify the number of characters inargumentthat are used in the comparison, the parameterposmay be used to specify the initial character inobjectthat is used in the comparison.
string::size_type object.copy(char const *argument,
string::size_type n, string::size_type pos):
If the third argument is omitted, the firstncharacters ofobjectare copied toargument. If the thirst argument is given, copying starts from&object[pos]. Following the copying, noascii-Zis appended to the copied string. Ifnexceedsobject.length(), at mostobject.length()characters are copied. The actual number of characters that were copied is returned.
char const *object.c_str:
the member function returns the contents ofobjectas anascii-ZC-string.
char const *object.data():
returns the raw text stored in object.
bool object.empty():
returnstrueifobjectcontains no data.
string &object.erase(string::size_type pos; string::size_type
n):
This member function can be used to erase (a sub)string ofobject. The basic form erasesobjectcompletely. The working of other forms oferase()depend on the specification of extra arguments:
- If
posis specified, the contents ofobjectare erased from index positionposuntil the end ofobject.- If
posandnare provided,ncharacters ofobject, starting at index positionposare erased.
iterator object.erase(iterator p):
The contents ofobjectare erased until (iterator) positionp. The iteratorpis returned.
iterator object.erase(iterator f, iterator l):
The range of characters ofobject, implied by theiterators fandlare erased. The iteratorfis returned.
string::string::size_type object.find(string argument,
string::size_type pos):
Returns the index inobjectwhereargumentis found. Ifposis omitted, the search starts at the beginning ofobject. Ifposis provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.find(char const *argument,
string::size_type pos, string::size_type n):
Returns the index inobjectwhereargumentis found. The parameternindicates the number of characters ofargumentthat should be used in the search: it defines a partial string starting at the beginning ofargument. If omitted, all characters inargumentare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well,objectis scanned completely.
string::size_type object.find(char c, string::size_type pos):
Returns the index inobjectwherecis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forobjectshould start.
string::size_type object.find_first_of(string argument,
string::size_type pos):
Returns the index inobjectwhere any character inargumentis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.find_first_of(char const* argument,
string::size_type pos, string::size_type n):
Returns the index inobjectwhere a character ofargumentis found, no matter which character. The parameternindicates the number of characters ofobjectthat should be used in the search: it defines a partial string starting at the beginning ofobject. If omitted, all characters inobjectare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well,objectis scanned completely.
string::size_type object.find_first_of(char c, string::size_type
pos):
Returns the index inobjectwhere charactercis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forcshould start.
string::size_type object.find_first_not_of(string argument,
string::size_type pos):
Returns the index inobjectwhere a character not appearing inargumentis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.find_first_not_of(char const*
argument, string::size_type pos, string::size_type n):
Returns the index inobjectwhere any character not appearing inargumentis found. The parameternindicates the number of characters ofobjectthat should be used in the search: it defines a partial string starting at the beginning ofobject. If omitted, all characters inobjectare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well,objectis scanned completely.
string::size_type object.find_first_not_of(char c,
string::size_type pos):
Returns the index inobjectwhere another character thancis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forcshould start.
string::size_type object.find_last_of(string argument,
string::size_type pos):
Returns the last index inobjectwhere a character inargumentis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.find_last_of(char const* argument,
string::size_type pos, string::size_type n):
Returns the last index inobjectwhere a character ofargumentis found. The parameternindicates the number of characters ofobjectthat should be used in the search: it defines a partial string starting at the beginning ofobject. If omitted, all characters inobjectare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well,objectis scanned completely.
string::size_type object.find_last_of(char c, string::size_type
pos):
Returns the last index inobjectwhere charactercis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forcshould start.
string::size_type object.find_last_not_of(string argument,
string::size_type pos):
Returns the last index inobjectwhere any character not appearing inargumentis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.find_last_not_of(char const*
argument, string::size_type pos, string::size_type n):
Returns the last index inobjectwhere any character not appearing inargumentis found. The parameternindicates the number of characters ofobjectthat should be used in the search: it defines a partial string starting at the beginning ofobject. If omitted, all characters inobjectare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well, all ofobjectis scanned.
string::size_type object.find_last_not_of(char c,
string::size_type pos):
Returns the last index inobjectwhere another character thancis found. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forcshould start.
istream &getline(istream instream, string object, char
delimiter):
This member function can be used to read a line of text (up to the first delimiter or the end of the stream) frominstream. The delimiter has a default value'\n'. It is removed frominstream, but it is not stored inobject.
string &object.insert(string::size_type t_pos, string argument,
string::size_type pos; string::size_type n):
This member function can be used to insert (a sub)string ofargumentintoobject, atobject's index positiont_pos. The basic form insertsargumentcompletely at indext_pos. The way other forms ofinsert()work depend on the specification of extra arguments:If
- If
posis specified,argumentis inserted from index positionposuntil the end ofargument.- If
posandnare provided,ncharacters ofargument, starting at index positionposare inserted intoobject.argumentis of typechar const *, no parameterposis available. So, withchar const *arguments, either all characters or an initial subset of the characters of the providedchar const *argument are inserted intoobject.
string &object.insert(string::size_type t_pos, string::size_type n, char c): Using this member function,ncharactersccan be inserted toobject.
iterator object.insert(iterator p, char c):
The charactercis inserted at the (iterator) positionpinobject. The iteratorpis returned.
iterator object.insert(iterator p, string::size_type n, char c):
Ncharacterscare inserted at the (iterator) positionpinobject. The iteratorpis returned.
iterator object.insert(iterator p, InputIterator first,
InputIterator last):
The range of characters implied by theInputIterators firstandlastare inserted at the (iterator) positionpinobject. The iteratorpis returned.
string::size_type object.length():
returns the number of characters stored in object.
string::size_type object.max_size():
returns the maximum number of characters that can be stored
in object.
string& object.replace(string::size_type pos1, string::size_type
n1, const string argument, string::size_type pos2, string::size_type n2):
The substring ofn1characters ofobject, starting at positionpos1is replaced byargument. Ifn1is set to 0, the member function insertsargumentintoobject.
The basic form usesargumentcompletely. The way other forms ofreplace()work depends on the specification of extra arguments:If
- If
pos2is specified,argumentis inserted from index positionpos2until the end ofargument.- If
pos2andn2are provided,n2characters ofargument, starting at index positionpos2are inserted intoobject.argumentis of typechar const *, no parameterpos2is available. So, withchar const *arguments, either all characters or an initial subset of the characters of the providedchar const *argument are replaced inobject.
string &object.replace(string::size_type pos, string::size_type
n1, string::size_type n2, char c):
This member function can be used to replacen1characters ofobject, starting at index positionpos, byn2 c-characters. The argumentn2may be omitted, in which case the string to be replaced is replaced by just one characterc.
string& object.replace (iterator i1, iterator i2, string
argument):
Here, the string implied by the iteratorsi1andi2are replaced by the stringstr. Ifargumentis achar const *, an extra argumentnmay be used, specifying the number of characters ofargumentthat are used in the replacement.
iterator object.replace(iterator f, iterator l, string argument):
The range of characters ofobject, implied by theiterators fandlare replaced byargument. Ifargumentis achar const *, an extra argumentnmay be used, specifying the number of characters ofargumentthat are used in the replacement. The stringobjectis returned.
iterator object.replace(iterator f, iterator l, string::size_type
n, char c):
The range of characters ofobject, implied by theiterators fandlare replaced byn c-characters. The iteratorfis returned.
string object.replace(iterator i1, iterator i2, InputIterator j1,
InputIterator j2):
Here the range of characters implied by the iteratorsi1andi2is replaced by the range of characters implied by theInputIterators j1andj2.
void object.resize(string::size_type n, char c):
The string stored inobjectis resized toncharacters. The second argument is optional. If provided and the string is enlarged, the extra characters are initialized toc.
string::size_type object.rfind(string argument,
string::size_type pos):
Returns the index inobjectwhereargumentis found. Searching proceeds from the end ofobjectback to the beginning. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.rfind(char const *argument,
string::size_type pos, string::size_type n):
Returns the index inobjectwhereargumentis found. Searching proceeds from the end ofobjectback to the beginning. The parameternindicates the number of characters ofargumentthat should be used in the search: it defines a partial string starting at the beginning ofargument. If omitted, all characters inargumentare used. The parameterposrefers to the index inobjectwhere the search forargumentshould start. If the parameterposis omitted as well, all ofobjectis scanned.
string::size_type object.rfind(char c, string::size_type pos):
Returns the index inobjectwherecis found. Searching proceeds from the end ofobjectback to the beginning. If the argumentposis omitted, the search starts at the beginning ofobject. If provided, it refers to the index inobjectwhere the search forargumentshould start.
string::size_type object.size():
returns the number of characters stored in object.
string object.substr(string::size_type pos, string::size_type
n):
Returns a substring ofobject. The parameternmay be used to specify the number of characters ofargumentthat are returned. The parameterposmay be used to specify the index of the first character ofargumentthat is returned. Eithernor both arguments may be omitted.
string::size_type object.swap(string argument):
swaps the contents of theobjectandargument. In this case,argumentmust be astringand cannot be achar const *.