C++/따배C++ 17강 String
[C++/CPP] 17.03. std::string 길이와 용량, length and capacity
polarcompass
2021. 12. 16. 07:24
728x90
#include<iostream>
#include<string>
using namespace std;
int main()
{
// c-style 문자랑 다르게
// string은 뒤에 null문자가 포함되지 않음
string my_str("01234567");
// 미리 용량 확보(최소한의 용량)
my_str.reserve(1000);
cout << std::boolalpha;
cout << my_str.size() << endl;
cout << my_str.length() << endl;
// 여분의 용량을 항상 두게됨.
// 메모리 생성하고 지우는 과정이 느리기에
// 추가로 들어올 문자를 생각해서 여분을 항상 챙김.
cout << my_str.capacity() << endl;
cout << my_str.max_size() << endl;
return 0;
}
728x90