[C++] string
Updated:
개요
- 문자 시퀀스를 저장하고 조작하는 클래스
- 멤버 함수
- element access
- at()
- 경계 검사를 사용하여 지정된 문자에 접근
- operator[]
- 지정된 문자에 접근
- front()
- C++11
- 첫 번째 문자에 접근
- back()
- C++11
- 마지막 문자에 접근
- data()
- 문자열의 첫 번째 문자에 대한 포인터를 반환
- c_str()
- 문자열의 수정 불가능한 표준 C 문자 배열을 반환
- at()
- iterators
- begin()
- 반복자의 처음을 반환
- cbegin()
- C++11
- 반복자의 처음을 상수 형태로 반환
- end()
- 반복자의 끝을 반환
- cend()
- C++11
- 반복자의 끝을 상수 형태로 반환
- rbegin()
- 역방향 반복자의 처음을 반환
- crbegin()
- C++11
- 역방향 반복자의 처음을 상수 형태로 반환
- rend()
- 역방향 반복자의 끝을 반환
- crend()
- C++11
- 역방향 반복자의 끝을 상수 형태로 반환
- begin()
- capacity
- empty()
- 문자열이 비어있는지 확인
- size()/length()
- 문자 수를 반환
- max_size()
- 최대 문자 수를 반환
- reserve()
- deprecated in C++20
- shrink_to_fit()를 권장
- 스토리지 할당 관리를 위한 예약된 크기를 변경
- capacity()
- 현재 할당된 스토리지에 할당할 수 있는 문자 수 반환
- shrink_to_fit()
- C++11
- 사용하지 않는 메모리를 해제
- empty()
- operations
- clear()
- 내용 삭제
- insert()
- 문자 삽입
- erase()
- 문자 삭제
- push_back()
- 끝에 문자 추가
- pop_back()
- C++11
- 마지막 문자 삭제
- append()
- 끝에 문자 추가
- operator+=
- 끝에 문자 추가
- compare()
- 두 문자열 비교
- starts_with()
- C++20
- 문자열이 주어진 접두사로 시작하는지 여부 반환
- ends_with()
- C++20
- 문자열이 주어진 접미사로 끝나는지 여부 반환
- replace()
- 문자열의 지정된 부분을 변경
- substr()
- 지정된 부분의 문자열 반환
- copy()
- 문자 복사
- resize()
- 저장된 문자수 변경
- swap()
- 내용을 변경
- clear()
- search
- find()
- 문자열에서 문자 검색
- find()
- element access
예제
- 코드
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { auto element_access = []() { string s = "abc"; cout << "s[2] : " << s[2] << endl; cout << "s[3] : " << s[3] << endl; cout << "s.at(2) : " << s.at(2) << endl; try { cout << "s.at(3) : " << s.at(3) << endl; } catch (exception &e) { cout << e.what() << endl; } cout << "s.front() : " << s.front() << endl; cout << "s.back() : " << s.back() << endl; cout << "s.data() : " << s.data() << endl; cout << "s.c_str() : " << s.c_str() << endl; }; element_access(); cout << endl << "------" << endl << endl; auto iterators = []() { string s = "abc"; *s.begin() = 'A'; cout << "*s.begin() = 'A'; : " << s << endl; // *s.cbegin() = 'a'; *(s.end() - 1) = 'C'; cout << "*(s.end() - 1) = 'C'; : " << s << endl; // *s.cend() = 'a'; *s.rbegin() = '3'; cout << "*s.rbegin() = '3'; : " << s << endl; // *s.crbegin() = '3'; *(s.rend() - 1) = '1'; cout << "*(s.rend() - 1) = '1'; : " << s << endl; // *s.crend() = '1'; for (auto iter = s.begin(); iter < s.end(); ++iter) { cout << *iter << " "; } cout << endl; }; iterators(); cout << endl << "------" << endl << endl; auto capacity = []() { string s = ""; cout << "s.empty() : " << s.empty() << endl; s = "abc"; cout << "s.empty() : " << s.empty() << endl; cout << "s.size() : " << s.size() << endl; cout << "s.length() : " << s.length() << endl; cout << "s.max_size() : " << s.max_size() << endl; cout << "s.capacity() 1 : " << s.capacity() << endl; s.reserve(100); cout << "s.capacity() 2 : " << s.capacity() << endl; s.reserve(); cout << "s.capacity() 3 : " << s.capacity() << endl; s.reserve(100); cout << "s.capacity() 4 : " << s.capacity() << endl; s.shrink_to_fit(); cout << "s.capacity() 5 : " << s.capacity() << endl; }; capacity(); cout << endl << "------" << endl << endl; auto operations = []() { string s = "abc"; s.clear(); cout << "s.empty() : " << s.empty() << endl; s.insert(0, "a"); cout << "s 1 : " << s << endl; s.insert(1, 2, 'b'); cout << "s 2 : " << s << endl; s.erase(2, 1); cout << "s 3 : " << s << endl; s.push_back('c'); cout << "s 4 : " << s << endl; s.pop_back(); cout << "s 5 : " << s << endl; s.append(2, 'c'); cout << "s 6 : " << s << endl; s += "e"; cout << "s 7 : " << s << endl; cout << s.compare("abcce") << endl; cout << s.compare("ab") << endl; cout << s.compare("abccea") << endl; cout << s.starts_with("ab") << endl; cout << s.starts_with("b") << endl; cout << s.ends_with("ce") << endl; cout << s.starts_with("c") << endl; cout << s.replace(3, 1, "d") << endl; cout << s.substr(1, 2) << endl; char c[10]{}; cout << s.copy(c, sizeof(c)) << endl; cout << c << endl; s.resize(3); cout << "s 8 : " << s << endl; s.resize(5, '!'); cout << "s 9 : " << s << endl; string s2 = "123"; s2.swap(s); cout << "s 10 : " << s << ", s2 : " << s2 << endl; }; operations(); cout << endl << "------" << endl << endl; auto search = []() { string s = "abcdef"; if (s.find("bc") != string::npos) { cout << "find" << endl; } }; search(); return 0; }
- 실행 결과
s[2] : c s[3] : s.at(2) : c s.at(3) : basic_string::at: __n (which is 3) >= this->size() (which is 3) s.front() : a s.back() : c s.data() : abc s.c_str() : abc ------ *s.begin() = 'A'; : Abc *(s.end() - 1) = 'C'; : AbC *s.rbegin() = '3'; : Ab3 *(s.rend() - 1) = '1'; : 1b3 1 b 3 ------ s.empty() : 1 s.empty() : 0 s.size() : 3 s.length() : 3 s.max_size() : 9223372036854775807 s.capacity() 1 : 15 s.capacity() 2 : 100 s.capacity() 3 : 15 s.capacity() 4 : 100 s.capacity() 5 : 15 ------ s.empty() : 1 s 1 : a s 2 : abb s 3 : ab s 4 : abc s 5 : ab s 6 : abcc s 7 : abcce 0 3 -1 1 0 1 0 abcde bc 5 abcde s 8 : abc s 9 : abc!! s 10 : 123, s2 : abc!! ------ find