C++之string::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.  
     
  6.  
    int main()
  7.  
    {
  8.  
    size_t npos = -1;
  9.  
    cout << "npos: " << npos << endl;
  10.  
    cout << "size_t max: " << numeric_limits<size_t>::max() << endl;
  11.  
    }

執行結果為:

                 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.  
     
  10.  
    size_t idx = filename.find('.'); //as a return value
  11.  
    if(idx == string::npos)
  12.  
    {
  13.  
    cout << "filename does not contain any period!" << endl;
  14.  
    }
  15.  
    else
  16.  
    {
  17.  
    string tmpname = filename;
  18.  
    tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作為長度參數,表示直到字符串結束
  19.  
    cout << "repalce: " << tmpname << endl;
  20.  
    }
  21.  
    }

 

執行結果如下:

filename:test.cpp

replace: test.xxx

三、值得注意的地方:
1、npos的類型
  1. int idx = str.find("abc");  
  2. if (idx == string::npos)  
  3.   ...  
上述代碼中,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::find()函數:是一個字符或字符串查找函數,該函數有唯一的返回類型,即string::size_type,即一個無符號整形類型,可能是整數也可能是長整數。如果查找成功,返回按照查找規則找到的第一個字符或者子串的位置;如果查找失敗,返回string::npos,即-1(當然打印出的結果不是-1,而是一個很大的數值,那是因為它是無符號的)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM