basic_string::resize
문자열을 추가 하거나 필요에 따라 요소 지우기에 대 한 새 크기를 지정 합니다.
void resize(
size_type _Count,
);
void resize(
size_type _Count,
_Elem _Ch
);
매개 변수
_Count
문자열의 새 크기입니다._Ch
추가 문자 값 초기화와 추가 요소가 필요한 경우.
설명
결과 크기 최대 문자 수를 초과 하는 경우 폼을 throw length_error.
예제
// basic_string_resize.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;
sizeStr1 = str1.size ( );
basic_string <char>::size_type capStr1;
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;
// Use resize to increase size by 2 elements: exclamations
str1.resize ( str1.size ( ) + 2 , '!' );
cout << "The resized string str1 is: " << str1 << endl;
sizeStr1 = str1.size ( );
capStr1 = str1.capacity ( );
// Compare size & capacity of a string after resizing
cout << "The current size of resized string str1 is: "
<< sizeStr1 << "." << endl;
cout << "The capacity of resized string str1 is: "
<< capStr1 << "." << endl << endl;
// Use resize to increase size by 20 elements:
str1.resize ( str1.size ( ) + 20 );
cout << "The resized string str1 is: " << str1 << endl;
sizeStr1 = str1.size ( );
capStr1 = str1.capacity ( );
// Compare size & capacity of a string after resizing
// note capacity increases automatically as required
cout << "The current size of modified string str1 is: "
<< sizeStr1 << "." << endl;
cout << "The capacity of modified string str1 is: "
<< capStr1 << "." << endl << endl;
// Use resize to downsize by 28 elements:
str1.resize ( str1.size ( ) - 28 );
cout << "The downsized string str1 is: " << str1 << endl;
sizeStr1 = str1.size ( );
capStr1 = str1.capacity ( );
// Compare size & capacity of a string after downsizing
cout << "The current size of downsized string str1 is: "
<< sizeStr1 << "." << endl;
cout << "The capacity of downsized string str1 is: "
<< capStr1 << "." << endl;
}
요구 사항
헤더: <string>
네임 스페이스: std