다음을 통해 공유


basic_string::reserve

용량 문자열의 숫자를 최소한 지정 된 수 만큼 좋은 설정합니다.

void reserve(
    size_type _Count = 0
);

매개 변수

  • _Count
    수 문자 메모리를 예약 하는 중입니다.

설명

재할당 하려면 많은 시간이 소모 되며 참조, 포인터와 문자열의에서 문자를 참조 하는 반복기를 무효화 하기 때문에 충분 한 용량을 사용 하는 것이 중요 합니다.

형식 문자열의 개체에 대 한 용량 개념이 형식 벡터의 개체와 동일합니다.멤버 함수는 벡터와는 달리 예약 개체의 용량을 줄이려면 호출할 수 있습니다.요청 된 nonbinding 및 수도 있고 발생 하지 않을 수 있습니다.기본적으로 매개 변수의 값은 호출 하는 문자열인 예약 바인딩 요청 문자열의 문자 수가 현재 문자열에 맞게 용량 축소 됩니다.절대로 문자 수가 현재 아래 용량이 줄어듭니다.

호출 reserve 문자열의 용량 축소만 가능한 방법입니다.그러나 위에서 언급 했 듯이이 요청 됩니다 nonbinding 및 발생 하지 않을 수 있습니다.

예제

// basic_string_reserve.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;
   string str1 ("Hello world");
   cout << "The original string str1 is: " << str1 << endl;

   basic_string <char>::size_type sizeStr1, sizerStr1;
   sizeStr1 = str1.size ( );
   basic_string <char>::size_type capStr1, caprStr1;
   capStr1 = str1.capacity ( );

   // Compare size & capacity of the original string
   cout << "The current size of original string str1 is: " 
        << sizeStr1 << "." << endl;
   cout << "The capacity of original string str1 is: "
        << capStr1 << "." << endl << endl;

   // Compare size & capacity of the string
   // with added capacity
   str1.reserve ( 40 );
   sizerStr1 = str1.size ( );
   caprStr1 = str1.capacity ( );

   cout << "The string str1with augmented capacity is: "
        << str1 << endl;
   cout << "The current size of string str1 is: " 
        << sizerStr1 << "." << endl;
   cout << "The new capacity of string str1 is: "
        << caprStr1 << "." << endl << endl;

   // Compare size & capacity of the string
   // with downsized capacity
   str1.reserve ( );
   basic_string <char>::size_type sizedStr1;
   basic_string <char>::size_type capdStr1;
   sizedStr1 = str1.size ( );
   capdStr1 = str1.capacity ( );

   cout << "The string str1 with downsized capacity is: "
        << str1 << endl;
   cout << "The current size of string str1 is: " 
        << sizedStr1 << "." << endl;
   cout << "The reduced capacity of string str1 is: "
        << capdStr1 << "." << endl << endl;
}
  
  
  
  
  
  

요구 사항

헤더: <string>

네임 스페이스: std

참고 항목

참조

basic_string Class