개요
- 문자 시퀀스를 저장하고 조작하는 클래스
- 멤버 함수
- element access
- at()
- operator[]
- front()
- back()
- data()
- c_str()
- 문자열의 수정 불가능한 표준 C 문자 배열을 반환
- iterators
- begin()
- cbegin()
- end()
- cend()
- rbegin()
- crbegin()
- C++11
- 역방향 반복자의 처음을 상수 형태로 반환
- rend()
- crend()
- C++11
- 역방향 반복자의 끝을 상수 형태로 반환
- capacity
- empty()
- size()/length()
- max_size()
- reserve()
- deprecated in C++20
- shrink_to_fit()를 권장
- 스토리지 할당 관리를 위한 예약된 크기를 변경
- capacity()
- 현재 할당된 스토리지에 할당할 수 있는 문자 수 반환
- shrink_to_fit()
- operations
- clear()
- insert()
- erase()
- push_back()
- pop_back()
- append()
- operator+=
- compare()
- starts_with()
- C++20
- 문자열이 주어진 접두사로 시작하는지 여부 반환
- ends_with()
- C++20
- 문자열이 주어진 접미사로 끝나는지 여부 반환
- replace()
- substr()
- copy()
- resize()
- swap()
- search
예제
- 코드
#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