名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::erase

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
(1)
basic_string& erase( size_type index = 0, size_type count = npos );
(C++20未満)
constexpr basic_string& erase( size_type index = 0, size_type count = npos );
(C++20以上)
(2)
iterator erase( iterator position );
(C++11未満)
iterator erase( const_iterator position );
(C++11以上)
(C++20未満)
constexpr iterator erase( const_iterator position );
(C++20以上)
(3)
iterator erase( iterator first, iterator last );
(C++11未満)
iterator erase( const_iterator first, const_iterator last );
(C++11以上)
(C++20未満)
constexpr iterator erase( const_iterator first, const_iterator last );
(C++20以上)

指定された文字を文字列から削除します。

1) index から始まる min(count, size() - index) 個の文字を削除します。
2) position の指す文字を削除します。
3) 範囲 [first, last) の文字を削除します。

目次

[編集] 引数

index - 削除する最初の文字
count - 削除する文字数
position - 削除する文字を指すイテレータ
first, last - 削除する文字の範囲

[編集] 戻り値

1) *this
2) 削除された文字の直後の文字を指すイテレータ、またはそのような文字が存在しない場合は end()
3) 削除前に last が指していた文字を指すイテレータ、またはそのような文字が存在しない場合は end()

[編集] 例外

1) index > size() の場合 std::out_of_range
2-3) (なし)

いずれのケースでも、何らかの理由で例外が投げられた場合、この関数は効果を持ちません (強い例外保証)。 (C++11以上)

[編集]

#include <iostream>
#include <algorithm>
#include <string>
 
int main()
{
    std::string s = "This is an example";
    std::cout << s << '\n';
 
    s.erase(0, 5); // "This " を削除します。
    std::cout << s << '\n';
 
    s.erase(std::find(s.begin(), s.end(), ' ')); // ' ' を削除します。
    std::cout << s << '\n';
 
    s.erase(s.find(' ')); // ' ' から文字列の終端までを削除します。
    std::cout << s << '\n';
}

出力:

This is an example
is an example
isan example
isan

[編集] 関連項目

内容をクリアします
(パブリックメンバ関数) [edit]