2016-11-18 46 views
0

我有一些的SKU,我需要一個字母數字代碼,諸如D2 D3 D4等使用簡單的IF(lineVector [1] .find(「D2字符串查找(),用於findng小差異

IM者區分「))

但是當我用多條語句做這件事情時,它似乎落入第一條,如果不改變。我的問題是,發現是否看到了D,並決定它不需要審視它,因爲它找到了它。

任何建議

繼承人我的代碼在短期

if (lineVector[7].find("D2")) 
      { 
       tempQuantity = 2; 
       totalQuantity += tempQuantity; 
      } 

      else if (lineVector[7].find("D3")) 
      { 
       tempQuantity = 3; 
       totalQuantity += tempQuantity; 
      } 

      else if (lineVector[7].find("D4")) 
      { 
       tempQuantity = 4; 
       totalQuantity += tempQuantity; 
      } 

      else if (lineVector[7].find("D5")) 
      { 
       tempQuantity = 5; 
       totalQuantity += tempQuantity; 
      } 
      else 
      { 
       tempQuantity = 1; 
       totalQuantity += tempQuantity; 
      } 

回答

0
// string::find 
#include <iostream>  // std::cout 
#include <string>   // std::string 
using namespace std; 

int main() 
{ 
    std::string str ("There are two needles in this haystack with needles."); 
    std::string str2 ("needle"); 

    // different member versions of find in the same order as above: 
    std::size_t found = str.find(str2); 
    if (found!=std::string::npos) 
    std::cout << "first 'needle' found at: " << found << '\n'; 

    return 0; 
} 

查找功能回到這裏比賽發生在success.On失敗(未找到)的指數,它返回的std :: string :: npos,其值不爲零。

所以你必須將返回值與std :: string :: npos進行比較。

我建議你,如果你有獨特的skus使用集(如果需要排序順序)或unordered_set(順序無關緊要)。