본문 바로가기

C++/기본 문법

<C++> string reverse(), substr()

std::reverse() : 문자열을 reverse 할 수 있음

참조(algorithm.h)

 

substr(size_type pos = 0, size_type count = npos) : pos 부터 count 개수만큼의 문자열을 리턴함

count npos를 전달한다면, 자동으로 pos 부터 원래 문자열의 끝 까지 리턴함

참조(string.h)

 

 

 

예시 코드


#include <bits/stdc++.h>
using namespace std;


int main()
{
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
	
	string dopa = "life is Limited";

	cout << dopa.substr(0, 3) << endl;

	reverse(dopa.begin(), dopa.end());
	cout << dopa << endl;
	
	dopa += "dopa!!";
	cout << dopa << endl;
}

'C++ > 기본 문법' 카테고리의 다른 글

<C++11> SmartPointer(스마트 포인터)  (0) 2022.09.28
Lvalue와 Rvalue 차이  (0) 2022.09.28
<C++> fill과 memset, memcpy  (0) 2022.04.19
<C++> 정적 배열(Array)  (0) 2022.04.12
<C++> vector  (0) 2022.04.12