public member function
<string>
std::basic_string::erase sequence (1)basic_string& erase (size_type pos = 0, size_type len = npos);character (2)
iterator erase (iterator p);range (3)
iterator erase (iterator first, iterator last);sequence (1)
basic_string& erase (size_type pos = 0, size_type len = npos);character (2)
iterator erase (const_iterator p);range (3)
iterator erase (const_iterator first, const_iterator last);
Erase characters from string
Erases part of the basic_string, reducing its length:Member type iterator is a random access iterator type that points to characters of the basic_string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// string::erase
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9); // ^
std::cout << str << '\n';
// "This is a sentence."
str.erase (str.begin()+5, str.end()-9); // ^^^^^
std::cout << str << '\n';
// "This sentence."
return 0;
}
Output:
This is an example sentence. This is an sentence. This is a sentence. This sentence.
if an exception is thrown, there are no changes in the
basic_string.
If pos is greater than the string length, an out_of_range exception is thrown.
An invalid p in (2), or an invalid range in (3), causes undefined behavior.
For
(1)and
(3), if an exception is thrown, there are no changes in the
basic_string(strong guarantee).
For
(2), it never throws exceptions (no-throw guarantee).
If pos is greater than the string length, an out_of_range exception is thrown.
An invalid range in (3), causes undefined behavior.
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