The arguments passed to the function determine the content that is inserted:
- string& insert ( size_t pos1, const string& str );
- Inserts a copy of the entire string object str at character position pos1.
- string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
- Inserts a copy of a substring of str at character position pos1. The substring is the portion of str that begins at the character position pos2 and takes up to n characters (it takes less than n if the end of str is reached before).
- string& insert ( size_t pos1, const char * s, size_t n );
- Inserts at the character position pos1, a copy of the string formed by the first n characters in the array of characters pointed by s.
- string& insert ( size_t pos1, const char * s );
- Inserts at character position pos1, a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this caracter sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).
- string& insert ( size_t pos1, size_t n, char c );
- Inserts a string formed by a repetition of character c, n times, at the character position pos1.
- iterator insert ( iterator p, char c );
- Inserts a copy of character c at the position referred by iterator p and returns an iterator referring to this position where it has been inserted.
- void insert ( iterator p, size_t n, char c );
- Inserts a string formed by the repetition of character c, n times, at the position referred by iterator p.
- template<class InputIterator> void insert (iterator p, InputIterator first, InputIterator last);
- Inserts at the internal position referred by p the content made up of the characters that go from the element referred by iterator first to the element right before the one referred by iterator last.
Parameters
- str
- Another object of class string whose content is entirely or partially inserted into the content.
- pos1
- Position within the string where the additional content is to be inserted. Notice that the first position has a value of 0, not 1.
If the position passed is past the end of the string, an out_of_range exception is thrown. - pos2
- Starting position of the substring of str that has the content to be inserted. Notice that the first position has also a value of 0.
If the position passed is past the end of str, an out_of_range exception is thrown. - n
- Number of characters to insert.
- s
- Array with a sequence of characters.
In the third member function version, the length is determined by parameter n, even including null characters in the content.
By contrast, in the fourth member version, s is a null-terminated character, and therefore its length is determined only by the first occurrence of a null character. - c
- Character value to be used to be repeated n times as the inserted content.
- p
- Iterator referring to an element part of the string, where the additional content is to be inserted.
This is an iterator of the string::iterator member type, which is a compiler specific iterator type suitable to iterate through the elements of this object. - start
- Iterator referring to the beginning of a sequence of characters whose content is to be inserted in the string.
- last
- Iterator referring to the past-the-end element of the sequence of characters to be inserted.
Return Value
*this, for the versions returning string&, and an interator referring to the insertion position for the version returning an iterator.Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Output:
to be, or not to be: that is the question... |
Basic template member declarations
( basic_string<charT,traits,Allocator> )| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
See also
| string::append | Append to string (public member function) |
| string::replace | Replace part of string (public member function) |
| string::substr | Generate substring (public member function) |
| string::operator= | String assignment (public member function) |
| string::operator+= | Append to string (public member function) |
