C++ 나만의 복습
string - reverse()
sudoju
2024. 3. 31. 22:24
string은 revers()라는 메서드를 지원하지 않는다.
문자열을 거꾸로 뒤집고 싶으면 STL에서 지원하는 함수인 reverse()를 쓰면 된다.
void reverse (BindirectionalIterator first, BidrectionalIterator last);
reverse() 함수는 void 타입으로 아무것도 반환하지 않는다.
다만 원본 문자열을 바꿔버린다.
다음 코드처럼 구축이 가능하다.
#include <stdio.h>
using namespace std;
int main()
{
string a = "I have aches and pains all over";
reverse(a.begin(), a.end());
cout << a << '\n';
}
/*
결과물
revo lla sniap dna sehca evah I
*/
여기서 a.begin() + 3으로 시작위치를 바꿔 뒤집고 싶은 부분만 바꿔서 할 수 있다.
reverse(a.begin() + 3, a.end());