跳到主要内容

std::string erase() 方法

// (1) Non const version only
constexpr basic_string& erase( size_type index = 0, size_type count = npos );

// (2) Non const version only
constexpr iterator erase( const_iterator position );

// (3) Non const version only
constexpr iterator erase( const_iterator first, const_iterator last );

从字符串中擦除指定字符。

  • (1) 删除从 index 开始的 std::min(count, size() - index) 个字符。
  • (2) 删除 position 处的字符。
  • (3) 删除范围 [ first, last ) 中的字符。

参数

  • index - 要删除的第一个字符
  • count - 要删除的字符数
  • position - 指向要删除字符的迭代器
  • first, last - 要删除字符的范围

返回值

  • (1) *this
  • (2)
    指向紧随已擦除字符之后的字符的迭代器。
    如果不存在此类字符,则为end()
  • (3)
    指向擦除前最后指向的字符的迭代器。
    如果不存在此类字符,则为end()

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

在任何情况下,如果由于任何原因抛出异常,此函数无效(强异常保证)。 (自 C++11 起)

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::string s = "This Is An Example";
std::cout << "1) " << s << '\n';

s.erase(7, 3); // erases " An" using overload (1)
std::cout << "2) " << s << '\n';

s.erase(std::find(s.begin(), s.end(), ' ')); // erases first ' '; overload (2)
std::cout << "3) " << s << '\n';

s.erase(s.find(' ')); // trims from ' ' to the end of the string; overload (1)
std::cout << "4) " << s << '\n';

auto it = std::next(s.begin(), s.find('s')); // obtains iterator to the first 's'
s.erase(it, std::next(it, 2)); // erases "sI"; overload (3)
std::cout << "5) " << s << '\n';
}
输出
1) This Is An Example
2) This Is Example
3) ThisIs Example
4) ThisIs
5) This
本文来源于此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::string erase() 方法

// (1) Non const version only
constexpr basic_string& erase( size_type index = 0, size_type count = npos );

// (2) Non const version only
constexpr iterator erase( const_iterator position );

// (3) Non const version only
constexpr iterator erase( const_iterator first, const_iterator last );

从字符串中擦除指定字符。

  • (1) 删除从 index 开始的 std::min(count, size() - index) 个字符。
  • (2) 删除 position 处的字符。
  • (3) 删除范围 [ first, last ) 中的字符。

参数

  • index - 要删除的第一个字符
  • count - 要删除的字符数
  • position - 指向要删除字符的迭代器
  • first, last - 要删除字符的范围

返回值

  • (1) *this
  • (2)
    指向紧随已擦除字符之后的字符的迭代器。
    如果不存在此类字符,则为end()
  • (3)
    指向擦除前最后指向的字符的迭代器。
    如果不存在此类字符,则为end()

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

在任何情况下,如果由于任何原因抛出异常,此函数无效(强异常保证)。 (自 C++11 起)

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::string s = "This Is An Example";
std::cout << "1) " << s << '\n';

s.erase(7, 3); // erases " An" using overload (1)
std::cout << "2) " << s << '\n';

s.erase(std::find(s.begin(), s.end(), ' ')); // erases first ' '; overload (2)
std::cout << "3) " << s << '\n';

s.erase(s.find(' ')); // trims from ' ' to the end of the string; overload (1)
std::cout << "4) " << s << '\n';

auto it = std::next(s.begin(), s.find('s')); // obtains iterator to the first 's'
s.erase(it, std::next(it, 2)); // erases "sI"; overload (3)
std::cout << "5) " << s << '\n';
}
输出
1) This Is An Example
2) This Is Example
3) ThisIs Example
4) ThisIs
5) This
本文来源于此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。