跳到主要内容

std::string resize() 方法

// (1) Non const version only
constexpr void resize( size_type count );

// (2) Non const version only
constexpr void resize( size_type count, CharT ch );

将字符串大小调整为包含 count 个字符。

如果当前大小小于 count,则会追加额外的字符

  • (1) 将追加的字符初始化为 CharT() (如果 CharTchar 则为 '\0')。
  • (2) 将追加的字符初始化为 ch
重要

如果当前大小大于count,则字符串将被截断为前 count 个元素。

参数

  • count - 字符串的新大小
  • ch - 用于初始化新字符的字符

返回值

(无)

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

如果 count > max_size(),则抛出std::length_error。任何由相应 Allocator 抛出的异常。

如果由于任何原因抛出异常,此函数无效(强异常保证)。 (自 C++11 起)

示例

Main.cpp
#include <iostream>
#include <iomanip>
#include <stdexcept>

int main()
{
const unsigned desired_length{ 8 };
std::string long_string( "Where is the end?" );
std::string short_string( "H" );


std::cout << "Basic functionality:\n"
<< "Shorten:\n"
<< "1. Before: " << quoted( long_string ) << '\n';
long_string.resize( desired_length );
std::cout << "2. After: " << quoted( long_string ) << '\n';


std::cout << "Lengthen with a given value 'a':\n"
<< "3. Before: " << quoted( short_string ) << '\n';
short_string.resize( desired_length, 'a' );
std::cout << "4. After: " << quoted( short_string ) << '\n';


std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
<< "5. Before: " << quoted( short_string ) << '\n';
short_string.resize( desired_length + 3 );
std::cout << "6. After: \"";
for (char c : short_string) {
std::cout << (c == char() ? '@' : c);
}
std::cout << "\"\n\n";


std::cout << "Errors:\n";
{
std::string s;

try {
// size is OK, no length_error
// (may throw bad_alloc)
s.resize(s.max_size() - 1, 'x');
} catch (const std::bad_alloc& ex) {
std::cout << "1. Exception: " << ex.what() << '\n';
}

try {
// size is OK, no length_error
// (may throw bad_alloc)
s.resize(s.max_size(), 'x');
} catch (const std::bad_alloc& ex) {
std::cout << "2. Exception: " << ex.what() << '\n';
}

try {
// size is BAD, throw length_error
s.resize(s.max_size() + 1, 'x');
} catch (const std::length_error& ex) {
std::cout << "3. Length error: " << ex.what() << '\n';
}
}
}
输出
Basic functionality:
Shorten:
1. Before: "Where is the end?"
2. After: "Where is"
Lengthen with a given value 'a':
3. Before: "H"
4. After: "Haaaaaaa"
Lengthen with char() == 0
5. Before: "Haaaaaaa"
6. After: "Haaaaaaa@@@"

Errors:
1. Exception: std::bad_alloc
2. Exception: std::bad_alloc
3. Length error: basic_string::_M_replace_aux
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::string resize() 方法

// (1) Non const version only
constexpr void resize( size_type count );

// (2) Non const version only
constexpr void resize( size_type count, CharT ch );

将字符串大小调整为包含 count 个字符。

如果当前大小小于 count,则会追加额外的字符

  • (1) 将追加的字符初始化为 CharT() (如果 CharTchar 则为 '\0')。
  • (2) 将追加的字符初始化为 ch
重要

如果当前大小大于count,则字符串将被截断为前 count 个元素。

参数

  • count - 字符串的新大小
  • ch - 用于初始化新字符的字符

返回值

(无)

复杂度

重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

异常

如果 count > max_size(),则抛出std::length_error。任何由相应 Allocator 抛出的异常。

如果由于任何原因抛出异常,此函数无效(强异常保证)。 (自 C++11 起)

示例

Main.cpp
#include <iostream>
#include <iomanip>
#include <stdexcept>

int main()
{
const unsigned desired_length{ 8 };
std::string long_string( "Where is the end?" );
std::string short_string( "H" );


std::cout << "Basic functionality:\n"
<< "Shorten:\n"
<< "1. Before: " << quoted( long_string ) << '\n';
long_string.resize( desired_length );
std::cout << "2. After: " << quoted( long_string ) << '\n';


std::cout << "Lengthen with a given value 'a':\n"
<< "3. Before: " << quoted( short_string ) << '\n';
short_string.resize( desired_length, 'a' );
std::cout << "4. After: " << quoted( short_string ) << '\n';


std::cout << "Lengthen with char() == " << static_cast<int>(char()) << '\n'
<< "5. Before: " << quoted( short_string ) << '\n';
short_string.resize( desired_length + 3 );
std::cout << "6. After: \"";
for (char c : short_string) {
std::cout << (c == char() ? '@' : c);
}
std::cout << "\"\n\n";


std::cout << "Errors:\n";
{
std::string s;

try {
// size is OK, no length_error
// (may throw bad_alloc)
s.resize(s.max_size() - 1, 'x');
} catch (const std::bad_alloc& ex) {
std::cout << "1. Exception: " << ex.what() << '\n';
}

try {
// size is OK, no length_error
// (may throw bad_alloc)
s.resize(s.max_size(), 'x');
} catch (const std::bad_alloc& ex) {
std::cout << "2. Exception: " << ex.what() << '\n';
}

try {
// size is BAD, throw length_error
s.resize(s.max_size() + 1, 'x');
} catch (const std::length_error& ex) {
std::cout << "3. Length error: " << ex.what() << '\n';
}
}
}
输出
Basic functionality:
Shorten:
1. Before: "Where is the end?"
2. After: "Where is"
Lengthen with a given value 'a':
3. Before: "H"
4. After: "Haaaaaaa"
Lengthen with char() == 0
5. Before: "Haaaaaaa"
6. After: "Haaaaaaa@@@"

Errors:
1. Exception: std::bad_alloc
2. Exception: std::bad_alloc
3. Length error: basic_string::_M_replace_aux
本文档源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。