public member function
<string>
std::string::insert string (1)string& insert (size_t pos, const string& str);substring (2)
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);c-string (3)
string& insert (size_t pos, const char* s);buffer (4)
string& insert (size_t pos, const char* s, size_t n);fill (5)
string& insert (size_t pos, size_t n, char c); void insert (iterator p, size_t n, char c);single character (6)
iterator insert (iterator p, char c);range (7)
template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last);string (1)
string& insert (size_t pos, const string& str);substring (2)
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);c-string (3)
string& insert (size_t pos, const char* s);buffer (4)
string& insert (size_t pos, const char* s, size_t n);fill (5)
string& insert (size_t pos, size_t n, char c);iterator insert (const_iterator p, size_t n, char c);single character (6)
iterator insert (const_iterator p, char c);range (7)
template <class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);initializer list (8)
string& insert (const_iterator p, initializer_list<char> il);string (1)
string& insert (size_t pos, const string& str);substring (2)
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);c-string (3)
string& insert (size_t pos, const char* s);buffer (4)
string& insert (size_t pos, const char* s, size_t n);fill (5)
string& insert (size_t pos, size_t n, char c);iterator insert (const_iterator p, size_t n, char c);single character (6)
iterator insert (const_iterator p, char c);range (7)
template <class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);initializer list (8)
string& insert (const_iterator p, initializer_list<char> il);
Insert into string
Inserts additional characters into the string right before the character indicated by pos (or p):Member type iterator is a random access iterator type that points to characters of the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// inserting into a string
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}
Output:
to be, or not to be: that is the question...
If s does not point to an array long enough, or if either p or the range specified by [first,last) is not valid, it causes undefined behavior.
If pos is greater than the string length, or if subpos is greater than str's length, an out_of_range exception is thrown.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4