std::erase, std::erase_if(std::basic_string)
| 헤더 <string>
|
||
template< class CharT, class Traits, class Alloc, class U >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type
erase( std::basic_string<CharT, Traits, Alloc>& c, const U& value );
|
에 정의됨 (1) | (C++20부터) (C++26까지) |
template< class CharT, class Traits, class Alloc, class U = CharT >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type
erase( std::basic_string<CharT, Traits, Alloc>& c, const U& value );
|
(C++26부터) | |
template< class CharT, class Traits, class Alloc, class Pred >
constexpr std::basic_string<CharT, Traits, Alloc>::size_type
erase_if( std::basic_string<CharT, Traits, Alloc>& c, Pred pred );
|
(2) | (C++20부터) |
value와(과) 같은 모든 요소를 지웁니다. 다음와 동일합니다.
auto it = std::remove(c.begin(), c.end(), value);
auto r = c.end() - it;
c.erase(it, c.end());
return r;
pred를 만족하는 모든 요소를 컨테이너에서 지웁니다. 다음와 동일합니다.
auto it = std::remove_if(c.begin(), c.end(), pred);
auto r = c.end() - it;
c.erase(it, c.end());
return r;
매개변수
| c | - | 요소를 삭제할 컨테이너 |
| value | - | 제거할 값 |
| pred | - |
요소가 삭제되어야 할 경우
true
를 반환하는 단항 predicate.
표현식
pred
(
v
)
는
|
반환값
삭제된 요소의 수.
복잡도
선형.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) |
목록 초기화
for
std::erase
|
예제
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string word{"startling"};
std::cout << "Initially, word = " << std::quoted(word) << '\n';
std::erase(word, 'l');
std::cout << "After erase 'l': " << std::quoted(word) << '\n';
auto erased = std::erase_if(word, [](char x)
{
return x == 'a' or x == 'r' or x == 't';
});
std::cout << "After erase all 'a', 'r', and 't': " << std::quoted(word) << '\n';
std::cout << "Erased symbols count: " << erased << '\n';
#if __cpp_lib_algorithm_default_value_type
std::erase(word, {'g'});
std::cout << "After erase {'g'}: " << std::quoted(word) << '\n';
#endif
}
가능한 출력:
Initially, word = "startling"
After erase 'l', word = "starting"
After erase all 'a', 'r', and 't': "sing"
Erased symbols count: 4
After erase {'g'}: "sin"
참고 항목
| 문자를 제거합니다 (공개 멤버 함수) | |
| 특정 조건을 만족하는 요소를 제거합니다 (함수 템플릿)&알고리즘 함수 객체) | |
(C++20)(C++20) |
|
(C++20)(C++20) |
특정 조건을 만족하는 요소를 제거합니다 (알고리즘 함수 객체) |