본문 바로가기
C++/C++

[C++] string 관련 참고사항

by sihyeong 2022. 10. 10.

swap(A, B);

swap(str1, str2);

- 스왑 시 복사를 사용하지 않고 바꾸기 때문에 복사에 의한 성능 저하 X

 

remove(first, end, removeValue);

#include <algorithm>

remove(s.begin(), s.end(), removeValue);

- removeValue에 해당하는 값들을 first부터 end까지 순환하여 모두 제거,

- 리턴값은 제거되지 않은 마지막 요소를 가리키는 반복자 리턴

 

remove() 주의사항

string str = "ABCD";

remove(str.begin(), str.end(), "A");
cout<< str;

================================
BCDD

- 위와같이 마지막 부분은 제거되지 않고 남게 된다.

- 따라서 erase()와 같이 사용됨

str.erase(remove(str.begin(), str.end(), "A"), str.end());
cout << str;

========================
BCD

 

'C++ > C++' 카테고리의 다른 글

[C++] wstring  (0) 2022.11.14
[C++] constexpr  (0) 2022.11.13
[C++] delete와 소멸자 ~class()  (0) 2022.10.13
[C++] 헤더 / 라이브러리에 관해  (1) 2022.10.11
[C++] 프로젝트 생성 관리  (0) 2022.10.11