Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Express
- EnhancedInput
- 언리얼
- 마인크래프트뮤지컬
- 파이썬서버
- 레베카
- 게임개발
- Enhanced Input System
- 스터디
- Unseen
- JWT
- node
- 데이터베이스
- Jinja2
- 언리얼뮤지컬
- R
- flask
- 으
- Bootstrap4
- 정글사관학교
- 카렌
- 프린세스메이커
- 프메
- 언리얼프로그래머
- 알고풀자
- Ajax
- VUE
- 스마일게이트
- 미니프로젝트
- 디자드
Archives
- Today
- Total
Today, I will
[C++] 코딩테스트에서 자주 쓰는 함수 예제 모음 본문
1. 문자열 자르기 : 참고
abcde 에서 원하는 구간을 자유롭게 자를 수 있어야 한다.
substr()
문자열.substr(시작 위치, 길이)
#include <iostream>
using namespace std;
int main() {
string str = "abcde";
cout << str.substr(0, 1); // a
cout << str.substr(1, 1); // b
cout << str.substr(2, 1); // c
cout << str.substr(0, 2); // ab
cout << str.substr(1, 2); // bc
return 0;
}
❗ 두 번째 인수인 길이는 생략할 수 있다.
지정한 위치부터 마지막까지 문자열을 얻는다.
문자열.substr(시작 위치)
#include <iostream>
using namespace std;
int main() {
string str = "abcde";
cout << str.substr(1); // bcde
cout << str.substr(2); // cde
cout << str.substr(3); // de
return 0;
}
📌 뒤에서부터 자르기
문자열의 길이를 구하는 size 함수를 사용하여 얻고 싶은 문자열 길이만큼 빼고, 그 값이 시작 위치가 된다.
#include <iostream>
using namespace std;
int main() {
string str = "abcde";
cout << str.substr(str.size () - 1); //e
cout << str.substr(str.size () - 2); //de
cout << str.substr(str.size () - 3); //cde
return 0;
}
'language > C, C++' 카테고리의 다른 글
[C, C++] 문자열을 다루는 함수(strcat,sprintf,strcpy,strncpy, strcmp) (0) | 2024.01.04 |
---|