std::string::erase() 的定义行为是什么,带有 string::npos
What is the defined behavior of std::string::erase() with a pos of string::npos?
当pos
参数作为string::npos
传递时,C++11 标准为 string& erase (size_t pos = 0, size_t len = npos);
成员函数的行为指定了什么? 我认为它应该什么都不删除,但也许它会抛出一个out_of_range
异常? 标准的定义行为是什么?
它抛出std::out_of_range
,如标准中明确说明的那样:
21.4.6.5 basic_string::擦除 [字符串::擦除]
basic_string&erase(size_type pos = 0, size_type n = npos);
要求:
pos <= size()
投掷:
out_of_range
如果pos > size()
。效果:确定要删除
的字符串的有效长度xlen
作为 较小的n
和size() - pos
。然后,该函数将替换字符串 由带有一串长度size() - xlen
的*this
控制,其第一个pos
元素是原始字符串的初始元素的副本 由*this
控制,其其余元素是 原始字符串的元素由*this
控制,从 位置pos + xlen
.返回:
*this
.
它抛出std::out_of_range
.请参阅 http://en.cppreference.com/w/cpp/string/basic_string/erase。
一般原则是 0 到 size()
之间的 pos
值(即超过末尾的一个)是可以的,但超出此值的任何值都表示调用方错误。
- map<string, int> m= {}; 在C++中是什么意思?
- 将(临时的?)std::string传递给使用它来构造一个接受副本的对象的函数的最佳方法是什么?
- freq_table[field_pm_value] = map<string, int>(); 是什么意思?
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- 'context string for the given token'是什么意思?
- "conversion from void to non scalar type string"是什么意思?
- 此错误的含义是什么:从 'char* [40] 转换为非标量类型 'std::string
- 用一个字符制作 std::string 的最佳方法是什么?
- 解释"Bit String"的最佳方式是什么
- std::string::substr成员函数的复杂性是什么?
- 将 ASCII 文件读取到C++ std::string 中的最简单方法是什么?
- std::string::erase() 的定义行为是什么,带有 string::npos
- C++/CLI 中的声明“String^ strtests”是什么意思
- 替换由谓词确定的 std::string 中的字符的最 stl-ish 方法是什么
- 当使用const std::string&作为方法参数类型时,处理nullptr const char*的正确方法是什么?
- ^在bool GetDeviceInformation中是什么意思(String^端口,LibCECConfigurat
- ^(插入符号)在C++中是什么意思,就像在String^中一样
- 在 LLVM libc++ 中找到的 string::find 中实现的算法(及其复杂性)是什么?
- C++没有添加"std::vector<std::string>"作为"main()"的参数"overload"的原因是什么?
- c++中连接const char *和string的最干净的方法是什么?