Language & etc/Reference
string 의 erase
JIK_
2021. 11. 26. 06:31
basic_string& erase(size_type index = 0, size_type count = npos); // (1)
iterator erase(const_iterator position); // (2)
iterator erase(const_iterator first, const_iterator last); // (3)
(1) index 부터 count개 문자를 삭제. ( substr 과 비슷 ) count가 문자열 넘어가면, 문자 끝까지만 지운다.
(2) iterator 위치 문자 삭제.
(3) first 부터 last 까지 문자들을 삭제.
[ 예시 ]
#include <algorithm>
#include <iostream>
#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