这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。 比如: string str; pos=str.find_first_of("h"); if(pos!=string::npos) {.. ... } 4 567891011 12...
npos 是一个常数,用来表示不存在的位置(在string中找不到某个要查找的字符),类型是string::size_type
正如其他人所提到的, string::npos 它是 size_t 的最大值。 这是它的定义: static constexpr auto npos{static_cast<size_type>(-1)}; 困惑的是,错误的答案得到了投票。 这是一个快速测试示例: int main() { string s = "C :"; size_t i = s.rfind('?'); size_t b = size_t (-1);...
string::npos是标准库的string容器属性。返回字符存放位置。这个东西是一个容器,它将字符串分成一个一个来存储。
1. string s = “xxx”; int a = s.find(‘x’); 如果没有匹配到,那么a = string::npos;
如果未找到,则返回string::npos。 rfind(str, pos): 从位置pos开始,从后向前查找子字符串str在当前字符串中首次出现的位置。如果未找到,则返回string::npos。 replace(pos, len, str): 从位置pos开始,用字符串str替换长度为len的子字符串。 erase(pos, len): 从位置pos开始,删除长度为len的子字符串。
解析:std::string::npos 的实际值是 std::size_t 的最大值(是一个非常大的无符号整数),而不是 -1。 2.以下代码的输出是什么? char src1[] = "Hello"; char src2[] = "NJUST"; char dest[10]; strcpy(dest, src1); strncpy(dest, src2, 3); ...
find + npos 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置 rfind 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置 substr 在str中从pos位置开始,截取n个字符,然后将其返回 1. 头插、追加、尾删 void Test_modify1() { string s; //尾插 s.push_back('H'); s.push_back...
其位置cout<<"find:";int npos1=s1.find('a');cout<<npos1<<endl;//rfind 从pos位置开始往前查找字符并返回其位置cout<<"rfind:";int npos2=s1.rfind('a');cout<<npos2<<endl;//substr 从pos位置开始截取n个字符并返回cout<<"substr后:";string tmp=s1.substr(npos1,npos2-npos1);cout<<tmp...