헤더파일 : #include <string>
생성방법 1: string str1("hello");
생성방법 2: string str2 = "hello";
생성방법 3: string str3(str1);
char& at (size_t index)
char& operator[size_t index]
char& front()
char& back()
size_t size() const
사이즈 반환size_t length() const
길이 반환size_t capacity() const
할당된 메모리 크기(bytes) 반환.void shrink_to_fit()
낭비되고 있는 capacity(메모리)를 줄임void reserve(size_t n = 0)
파일 읽을 때 많이 사용. 미리 메모리를 할당해, capacity가 사이즈에 맞게 계속 늘어나는 행위를 덜하게해서 성능저하를 줄임. (성능저하 예시, 노래방2인실->4인실->8인실)void clear()
bool empty() const
비었const char* c_str() const
string 마지막에 \0를 추가. c언어처럼 바뀜.string substr(size_t index = 0, size_t len = npos) const
index에서부터 len만큼 잘라서 반환string& replace(size_t index, size_t len, const string& str)
int compare(const string& str2) const
int compare(size_t index, size_t len, const string& str2) const
int compare(size_t index, size_t len, const string& str2, size_t index2, size_t len2) const
size_t copy(char* arr, size_t len, size_index = 0) const
char arr[10];
int arrLen = str1.copy(arr, 3, 5); //arrLen== 3, arr == 'Mas'
arr[arrLen] = '\0'; //arr == 'Mas\0'
size_t find (const string& str, size_t index = 0) const
size_t find (const char* arr, size_t index = 0) const
일치하는 부분의 첫번째 순서(index) 반환
str2.find("Blog") // 0반환
str2.find("Blog", 5) //8반환 BlogBlogBlogBlog 5번째가 'l'로 다르니 그 다음 Blog의 B index
void push_back(char c)
void pop_back()
iterator begin()
iterator end()
const_iterator end() const
void swap (string& st1, string& str2)
#include <iostream>
#include <string>
using namespace std;
int main(void){
string str1 = "BlockDMask";
string str2 = "BlogBlogBlogBlog";
//push_back, pop_back
str1.push_back('4');
cout << str1 << endl;
//str1.pop_back();
//cout << str1 << endl;
// str2.resize(4); //Blog 4 4 16
//str2.shrink_to_fit(); //
//str2.clear(); // 0 0 16
int size = str2.size();
int length = str2.length();
int capacity = str2.capacity();
cout << str2 << ' ' << size << ' ' << length << ' ' << capacity << endl;
//BlogBlogBlogBlog 16 16 16
//find
cout << str1.find("DM") << endl; //5
cout << str2.find("Blog") << endl; //0
cout << str2.find("Blog", 5) << endl; //8
// operator[], atr
cout << str1[0] << endl; //B
cout << str1.at(0) << endl; //B
//front, back
//cout << str1.front() << endl; //B
//cout << str1.back() << endl; //k
//iterator begin(), end() // B l o c k D M a s k 4
string::iterator iter = str1.begin();
for(; iter != str1.end(); ++iter){
cout << *iter << ' ';
}
cout << endl;
system("pause");
return 0;
}