C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::resize
std::basic_string<CharT,Traits,Allocator>::resize
(1) | ||
void resize( size_type count ); |
(C++20 前) | |
constexpr void resize( size_type count ); |
(C++20 起) | |
(2) | ||
void resize( size_type count, CharT ch ); |
(C++20 前) | |
constexpr void resize( size_type count, CharT ch ); |
(C++20 起) | |
重设 string 大小以含 count
个字符。
若当前大小小于 count
,则后附额外的字符。
若当前大小大于 count
,则缩减 string 到为其首 count
个元素。
第一版本初始化新字符为 CharT() ,第二版本初始化新字符为 ch
。
参数
count | - | string 的新大小 |
ch | - | 用以初始化新字符的字符 |
返回值
(无)
异常
若 count > max_size() 则为 std::length_error 。对应 Allocator
所抛的任何异常。
若因任何原因抛出异常,则此函数无效果(强异常保证)。 (C++11 起)
示例
运行此代码
#include <iostream> #include <stdexcept> int main() { std::cout << "Basic functionality:\n"; const unsigned desired_length(8); std::string long_string( "Where is the end?" ); std::string short_string( "Ha" ); // 缩短 std::cout << "Before: \"" << long_string << "\"\n"; long_string.resize( desired_length ); std::cout << "After: \"" << long_string << "\"\n"; // 加长 std::cout << "Before: \"" << short_string << "\"\n"; short_string.resize( desired_length, 'a' ); std::cout << "After: \"" << short_string << "\"\n"; std::cout << "\nErrors:\n"; { std::string s; try { // 大小 OK ,无 length_error // (可能抛 bad_alloc ) s.resize(s.max_size() - 1, 'x'); } catch (const std::bad_alloc&) { std::cout << "1. bad alloc\n"; } try { // 大小 OK ,无 length_error // (可能抛 bad_alloc ) s.resize(s.max_size(), 'x'); } catch (const std::bad_alloc& exc) { std::cout << "2. bad alloc\n"; } try { // 大小错误,抛出 length_error s.resize(s.max_size() + 1, 'x'); } catch (const std::length_error&) { std::cout << "3. length error\n"; } } }
可能的输出:
Basic functionality: Before: "Where is the end?" After: "Where is" Before: "Ha" After: "Haaaaaaa" Errors: 1. bad alloc 2. bad alloc 3. length error
复杂度
与 string 大小成线性。
参阅
返回字符数 (公开成员函数) |