c++ - 我如何知道是否实际创建了 std::string?

标签 c++ out-of-memory stdstring

我想使用std::string(size_type count,CharT ch)count 的值很大。读书https://en.cppreference.com/w/cpp/string/basic_string/basic_string ,我找不到此构造函数的异常定义,以防它失败。

如果是正确的,虽然构造函数中没有noexcept子句,但我如何确定字符串被创建了呢?我应该检查它的大小是否不为 0?

最佳答案

您的链接在异常(exception)下说明:

Throws std::length_error if the length of the constructed string would exceed max_size() (for example, if count > max_size() for (2)). Calls to Allocator::allocate may throw.

此外,std::string 使用分配器,这意味着如果分配器未能分配请求的内存量,也可以抛出 std::bad_alloc

关于c++ - 我如何知道是否实际创建了 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647083/

相关文章:

c++ - 使用 op[] 修改 std::string 超出其大小的影响?

c++ - 使用单个拷贝初始化 std::string

c++ - 空终止字符串,它真的是由标准规定的吗?

c++ - 在设计和体系结构上开发像.NET Framework 类库的非托管C++ 库是否浪费时间?

c++ - C++中二维动态内存分配数组中的免费分配内存

c++ - 如何将 C++ 参数包映射到一系列 std::pair 对象中?

Android onPictureTaken 回调在 Bitmap.decodeByteArray 中抛出内存异常

c++ - 如何在内核中添加包含库的路径?

c# - 处理大型数据集和内存限制

php - Cygwin PHP Windows 10 内存不足