1. string library
- 3개의 string type를 제공하는 library
|
basic_string
|
basic_string_view(c++17)
|
null-terminated strings
|
|
string : basic_string<char>
wstring : basic_string<wchar_t>
etc
|
string : basic_string_view<char>
wstring : basic_string_view<wchar_t>
etc
|
null-terminated byte strings
null-terminated multibyte strings
null-terminated wide strings
|
- basic_string를 위주로 공부.
- basic_string : template로 이루어져있는 class 객체 그 자체.
2. string : basic_string<char>
#include <string>
- Random-access iterator
- basic_string<char>를 typedef로 선언한 타입
- cin, cout으로만 입출력 가능. scanf, printf 사용 불가능, 만약 사용하려면 char로 변환해서 사용.
3. 주요 문법
string s // string 생성
iteraror s.begin(), s.end() // iterator 반환
reverse_iterator s.rbegin(), s.rend() // reverse_iterator 반환
T s [ull idx] // s[idx] qksghks
bool s.empty // 비어있으면 true, 아니면 false 반환
void s.clear // 초기화
ull s.size() // size 반환
T s.front(), s.back() // 처음, 마지막 문자 값 반환
void s.push_back(char ch), s.pop_back() // ch 문자를 맨 뒤에 등록, 마지막 문자 삭제
string to_string(int x) // 정수 x를 string으로 변환(모든 정수형 가능)
int stoi(s) // s의 앞에서 부터 숫자가 아닌 첫 문자 이전까지를 int로 반환
// ex) "35x33" -> 3 5 만 정수로, 나머지는 날림
** ull 자리는 정확히 표현하면 size_t이지만 64bit OS에서는 unsigned long long과 동일하므로 편의상 null로 기록.
// string <-> char* 변환
char cstr[] = "abcdefg" // c문자열 생성
s = cstr // c문자열은 대입연산자만으로 string 변환 가능. char -> string
strcpy(cstr, s.c_str()) // string은 c_str()함수로 c 문자열 변환 가능
** operator (+, ==, !=, < , <=, >=, > 모두 정의되어있음.)
s1 == s2 // 동일시 true
s1 += s2 // 앞에거에 뒤에거 이어붙이기
s1 < s2 // s1이 s2보다사전순으로 빠르면 true
// insert
iterator s.insert(iterator pos, char ch) // pos위치에 ch를 등록
string& s.insert(ull udx, int cnt, char ch) // idx에 cnt개의 ch 등록 (cnt가 1이어도 써야함.)
string& s.insert(ull udx, char* cstr) // idx에 cstr 등록
string& s.insert(ull udx, char* cstr, ull cnt) // idx에 cstr의 cnt 길이만큼만 등록
string& s.insert(ull udx, string s2) // idx에 s2를 등록
string& s.insert(ull udx, string s2, ull s_idx, ull cnt = npos) // idx에 s2의 s_idx부터 cnt길이 등록
** npos 은 -1인 값인데 이게 unsigned long long에서는 표현가능한 최대값이 된다.
= 기본값이 제일 큰 값 = 생략시 뒤에 있는거 전부 없앰.
= ull cnt = npos는 끝까지 수행하라는 의미!
// find ; s에서 최초로 등장하는 x의 위치 반환(없으면 -1)
// idx:탐색 시작 위치, cnt 검색하고자 하는 x의 길이
ull s.find(string x, ull idx = 0)
ull s.find(char* x, ull idx, ull cnt)
ull s.find(char* x, ull idx = 0)
ull s.find(char x, ull idx = 0)
// Erase
iterator s.erase(iterator pos) // pos위치의 문자 삭제
string& s.erase(null idx = 0, ull cnt = npos) // idx위치부터 cnt개 문자 삭제
// Substr
string s.substr(ull idx = 0, ull cnt = npos) // s의 idx부터 cnt길이의 부분 문자열 반환
4. string 예시
char cstr[100] = "stl";
string str A = cstr; // stl
strA+= "Easy"; // stlEasy
atrA[0] = 'x'; // xtlEasy
strA.erase(strA.begin()+3); //xtlasy
strA.erase(1,2); // xasy
strA.erase(2); // 'xasy'인 경우 xa만 남고 s이후는 모두 지워짐
strA.substr(1,2); // 'xasy'인 경우 return : as , strA는 그대로
string strB = "123xyz45xyz";
int idx = strB.find("x"); // 'x'가 등장하는 첫 위치 인덱스 :3 반환
stoi(strB); // 123(int)
stoll(strB); // 123(longlong)
strA += strB; // xasy123xyz45xyz
strcpy(cstr, strA.c_str()); // xasy123xyz45 (char*)
string str = to_string(123); // 정수 123을 문자열 "123"으로 반환
str.insert(0, "abc"); // abc123
str.insert(3, 2, 'K'); // abcKK123
5. C string library
|
string.h (cstring)
|
ctype.h (cctype)
|
||
|
char* 를 처리하는 함수들이 저장된 library
|
char 를 처리하는 함수들이 저장된 library
|
||
|
strcmp()
strcpy()
strlen()
memcmp()
memcpy()
memset()
|
문자열 비교
문자열 복사
문자열 길이
바이트 단위로 배열 비교
바이트 단위로 배열 복사
바이트 단위로 배열 초기화
|
idalpha()
isdigit()
isalnum()
islower()
isupper()
tolower()
touuper()
|
알파벳인가?
'0'~'9'인가?
알파벳 또는 '0'~'9'인가?
소문자인가?
대문자인가?
소문자로 변경
대문자로 변경
|
'C++ > STL' 카테고리의 다른 글
| [STL] Priority Queue(1) - heap, sort, compair 설정 (0) | 2025.06.16 |
|---|---|
| [STL] Stack, Queue (0) | 2025.06.13 |
| [STL] Deque, List (1) | 2025.06.12 |
| [STL] Vector (0) | 2025.06.12 |
| [STL] STL Algorithm - sort, nth_element, find, find_if (0) | 2025.06.12 |