首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
This is an example
is an example
isan example
isan

二次

另见

clear

clears the contents (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券