std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite
来自 cppreference.cn
< cpp | string | basic string
template< class Operation > constexpr void resize_and_overwrite( size_type count, Operation op ); |
(自 C++23 起) | |
调整字符串大小以最多容纳 count 个字符,使用用户提供的操作 op 来修改可能不确定的内容并设置长度。这避免了在 std::string 打算用作要由例如 C API 调用填充的字符数组时,初始化适当大小的 std::string 的成本。
此函数执行以下步骤
- 获取包含 count + 1 个字符的连续存储空间,并使其前 k 个字符等于 *this 的前 k 个字符,其中 k 是 count 和调用
resize_and_overwrite
之前 size() 结果中较小的一个。 令 p 表示指向存储空间中第一个字符的指针。- 相等性通过检查 this->compare(0, k, p, k) == 0 来确定。
- [p + k
,
p + count] 范围内的字符可能具有不确定的值。
- 计算 std::move(op)(p, count),令 r 为返回值。
- 将 *this 的内容替换为 [p
,
p + r)(这会将 *this 的长度设置为 r)。 使所有指向 [p,
p + count] 范围的指针和引用失效。
如果 r 不是整数类型,则程序是非良构的。
如果满足以下任何条件,则行为是未定义的
- std::move(op)(p, count) 抛出异常。
- std::move(op)(p, count) 修改 p 或 count。
- r 不在 [0
,
count] 范围内。 - [p
,
p + r) 范围内的任何字符都具有不确定的值。
建议实现通过例如使 p 等于调用后为 *this 分配的字符存储空间开始处的指针来避免不必要的复制和分配,如果 count 小于或等于 capacity(),这可以与 *this 的现有存储空间相同。
目录 |
[编辑] 参数
count | - | 字符串的最大可能新大小 |
op | - | 用于设置字符串新内容的函数对象 |
[编辑] 异常
std::length_error 如果 count > max_size()。 由相应的 Allocator 抛出的任何异常。
如果 std::move(op)(p, count) 抛出异常,则行为是未定义的。 否则,如果抛出异常,则此函数无效。
[编辑] 注释
resize_and_overwrite
使所有迭代器、指针和对 *this 的引用失效,无论是否发生重新分配。 实现可以假定在调用 resize_and_overwrite
之后,字符串的内容不会被别名化。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_string_resize_and_overwrite |
202110L |
(C++23) | std::basic_string::resize_and_overwrite
|
[编辑] 示例
测试示例的链接:compiler explorer。
运行此代码
#include <algorithm> #include <cassert> #include <cstddef> #include <cstring> #include <iomanip> #include <iostream> #include <string> #include <string_view> static_assert(__cpp_lib_string_resize_and_overwrite); constexpr std::string_view fruits[]{"apple", "banana", "coconut", "date", "elderberry"}; int main() { // A simple case, append only fruits[0]. The string size will be increased. std::string s{"Food: "}; s.resize_and_overwrite(16, [sz = s.size()](char* buf, std::size_t buf_size) noexcept { const auto to_copy = std::min(buf_size - sz, fruits[0].size()); std::memcpy(buf + sz, fruits[0].data(), to_copy); return sz + to_copy; }); std::cout << "1. " << std::quoted(s) << '\n'; // The size shrinking case. Note, that the user's lambda is always invoked. s.resize_and_overwrite(10, [](char* buf, int n) noexcept { return std::find(buf, buf + n, ':') - buf; }); std::cout << "2. " << std::quoted(s) << '\n'; std::cout << "3. Copy data until the buffer is full. Print data and sizes.\n"; std::string food{"Food:"}; const auto resize_to{27}; std::cout << "Initially, food.size: " << food.size() << ", food.capacity: " << food.capacity() << ", resize_to: " << resize_to << ", food: " << std::quoted(food) << '\n'; food.resize_and_overwrite ( resize_to, [food_size = food.size()](char* p, std::size_t n) noexcept -> std::size_t { // p[0]..p[n] is the assignable range // p[0]..p[min(n, food_size) - 1] is the readable range // (contents initially equal to the original string) // Debug print: std::cout << "In Operation(); n: " << n << '\n'; // Copy fruits to the buffer p while there is enough space. char* first = p + food_size; for (char* const end = p + n; const std::string_view fruit : fruits) { char* last = first + fruit.size() + 1; if (last > end) break; *first++ = ' '; std::ranges::copy(fruit, first); first = last; } const auto final_size{static_cast<std::size_t>(first - p)}; // Debug print: std::cout << "In Operation(); final_size: " << final_size << '\n'; assert(final_size <= n); return final_size; // Return value is the actual new length // of the string, must be in range 0..n } ); std::cout << "Finally, food.size: " << food.size() << ", food.capacity: " << food.capacity() << ", food: " << std::quoted(food) << '\n'; }
可能的输出
1. "Food: apple" 2. "Food" 3. Copy data until the buffer is full. Print data and sizes. Initially, food.size: 5, food.capacity: 15, resize_to: 27, food: "Food:" In Operation(); n: 27 In Operation(); final_size: 26 Finally, food.size: 26, food.capacity: 30, food: "Food: apple banana coconut"
[编辑] 参见
更改存储的字符数 (公共成员函数) |