std::basic_string::erase
basic_string& erase( size_type index = 0, size_type count = npos ); | (1) | |
---|---|---|
| (2) | |
iterator erase( iterator position ); | (until C++11) | |
iterator erase( const_iterator position ); | (since C++11) | |
| (3) | |
iterator erase( iterator first, iterator last ); | (until C++11) | |
iterator erase( const_iterator first, const_iterator last ); | (since C++11) |
从字符串中移除指定的字符。
1%29移除最小%28count
,,,size()- index
%29字符从index
...
2%29删除position
...
3%29移除范围内的字符。[first, last)
...
参数
index | - | first character to remove |
---|---|---|
count | - | number of characters to remove |
position | - | iterator to the character to remove |
first, last | - | range of the characters to remove |
返回值
1%29*this
2%29迭代器,指向紧接该字符之后的字符,或end()
如果不存在这样的字符
3%29迭代器指向字符last
指向擦除之前,或end()
如果不存在这样的字符
例外
1%29std::out_of_range
如果index > size()
...
2-3%29%280%29
在任何情况下,如果出于任何原因引发异常,则此函数不具有%28强异常保证%29的效果。%28自C++11%29。
例
二次
#include <iostream>
#include <algorithm>
#include <string>
int main ()
{
std::string s = "This is an example";
std::cout << s << '\n';
s.erase(0, 5); // Erase "This "
std::cout << s << '\n';
s.erase(std::find(s.begin(), s.end(), ' ')); // Erase ' '
std::cout << s << '\n';
s.erase(s.find(' ')); // Trim from ' ' to the end of the string
std::cout << s << '\n';
}
二次
产出:
二次
This is an example
is an example
isan example
isan
二次
另见
clear | clears the contents (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com