250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/CPP] 17.03. std::string 길이와 용량, length and capacity 본문

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