C++中string::npos的一些用法总结

一、关于npos的定义
在MSDN中有如下说明:
basic_string::npos
static const size_type npos = -1;//定义
The constant is the largest representable value of type size_type. It is assuredly larger than max_size(); hence it serves as either a very large value or as a special code.

以上的意思是npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。

     
     
  1. #include <iostream>
  2. #include <limits>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. size_t npos = -1;
  8. cout << “npos: “ << npos << endl;
  9. cout << “size_t max: “ << numeric_limits< size_t>::max() << endl;
  10. }

执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

可见他们是相等的,也就是说npos表示size_t的最大值


二、npos的用法
1、npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下计算字符串中含有的不同字符的个数

     
     
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string b;
  7. getline( cin,b);
  8. int count= 0;
  9. for( int i= 0;i<= 127;i++)
  10. if(b.find(i)!= string::npos)
  11. count++;
  12. cout<<count;
  13. }
举例2:

     
     
  1. string name(”Annaqijiashe”);
  2. int pos=name.find( “Anna”);
  3. if(pos== string::npos)
  4. cout<< “Anna not found!\n”;
  5. else cout<< “Anna found at pos:”<<pos<< endl;
2、string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:
  1. tmpname.replace(idx+1, string::npos, suffix);  
这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。

      
      
  1. #include <iostream>
  2. #include <limits>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. string filename = “test.cpp”;
  8. cout << “filename : “ << filename << endl;
  9. size_t idx = filename.find( ‘.’); //as a return value
  10. if(idx == string::npos)
  11. {
  12. cout << “filename does not contain any period!” << endl;
  13. }
  14. else
  15. {
  16. string tmpname = filename;
  17. tmpname.replace(idx + 1, string::npos, “xxx”); //string::npos作为长度参数,表示直到字符串结束
  18. cout << “repalce: “ << tmpname << endl;
  19. }
  20. }
执行结果如下:

filename:test.cpp

replace: test.xxx


三、值得注意的地方:
1、npos的类型

     
     
  1. int idx = str.find( “abc”);
  2. if (idx == string::npos)
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find(“abc”) == string::npos) { … }

2、string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。


参考资料:

string::npos 无符号数的陷阱 

string::npos的一些说明

        </div>
            </div>
一、关于npos的定义
在MSDN中有如下说明:
basic_string::npos
static const size_type npos = -1;//定义
The constant is the largest representable value of type size_type. It is assuredly larger than max_size(); hence it serves as either a very large value or as a special code.

以上的意思是npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。

   
   
  1. #include <iostream>
  2. #include <limits>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. size_t npos = -1;
  8. cout << “npos: “ << npos << endl;
  9. cout << “size_t max: “ << numeric_limits< size_t>::max() << endl;
  10. }

执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

可见他们是相等的,也就是说npos表示size_t的最大值


二、npos的用法
1、npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下计算字符串中含有的不同字符的个数

   
   
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string b;
  7. getline( cin,b);
  8. int count= 0;
  9. for( int i= 0;i<= 127;i++)
  10. if(b.find(i)!= string::npos)
  11. count++;
  12. cout<<count;
  13. }
举例2:

   
   
  1. string name(”Annaqijiashe”);
  2. int pos=name.find( “Anna”);
  3. if(pos== string::npos)
  4. cout<< “Anna not found!\n”;
  5. else cout<< “Anna found at pos:”<<pos<< endl;
2、string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:
  1. tmpname.replace(idx+1, string::npos, suffix);  
这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。

    
    
  1. #include <iostream>
  2. #include <limits>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. string filename = “test.cpp”;
  8. cout << “filename : “ << filename << endl;
  9. size_t idx = filename.find( ‘.’); //as a return value
  10. if(idx == string::npos)
  11. {
  12. cout << “filename does not contain any period!” << endl;
  13. }
  14. else
  15. {
  16. string tmpname = filename;
  17. tmpname.replace(idx + 1, string::npos, “xxx”); //string::npos作为长度参数,表示直到字符串结束
  18. cout << “repalce: “ << tmpname << endl;
  19. }
  20. }
执行结果如下:

filename:test.cpp

replace: test.xxx


三、值得注意的地方:
1、npos的类型

   
   
  1. int idx = str.find( “abc”);
  2. if (idx == string::npos)
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find(“abc”) == string::npos) { … }

2、string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。


参考资料:

string::npos 无符号数的陷阱 

string::npos的一些说明

        </div>
            </div>

猜你喜欢

转载自blog.csdn.net/monk1992/article/details/81702556