2010-06-22 172 views
4

我想測試std::string爲包含任何範圍的數字,例如5 to 35std::string s = "XDGHYH20YFYFFY"會有功能,或者我將不得不將數字轉換爲字符串,然後使用循環找到每一個?檢查字符串是否包含一個數字範圍

+3

我想你在這裏需要的是一個正則表達式。 – 2010-06-22 18:35:11

+0

您是否試圖在範圍內找到至少一個數字或範圍內的所有數字?如果這是第一個我認爲使用正則表達式會更好,但如果你將檢查範圍內的所有數字比使用查找功能。 – 2010-06-22 18:44:33

回答

9

我可能會使用該處理一切,除了數字爲白色空間的區域,並從與該區域充滿一個字符串流讀取的數量和檢查,如果他們在範圍:

#include <iostream> 
#include <algorithm> 
#include <locale> 
#include <vector> 
#include <sstream> 

struct digits_only: std::ctype<char> 
{ 
    digits_only(): std::ctype<char>(get_table()) {} 

    static std::ctype_base::mask const* get_table() 
    { 
     static std::vector<std::ctype_base::mask> 
      rc(std::ctype<char>::table_size,std::ctype_base::space); 

     std::fill(&rc['0'], &rc['9'], std::ctype_base::digit); 
     return &rc[0]; 
    } 
}; 

bool in_range(int lower, int upper, std::string const &input) { 
    std::istringstream buffer(input); 
    buffer.imbue(std::locale(std::locale(), new digits_only())); 

    int n; 

    while (buffer>>n) 
     if (n < lower || upper < n) 
      return false; 
    return true; 
} 

int main() { 
    std::cout << std::boolalpha << in_range(5, 35, "XDGHYH20YFYFFY"); 
    return 0; 
} 
2

儘管有些人會立即轉到正則表達式,但我認爲您最好的選擇是混合解決方案。使用REGEX查找數字,然後解析它們並查看它們是否在範圍內。

在C#中是這樣的。不確定在C++中可以使用哪些正則表達式庫。

using System.Text.RegularExpressions.Regex; 
using System.Text.RegularExpressions.Match; 
using System.Text.RegularExpressions.MatchCollection; 

private static const Regex NUMBER_REGEX = new Regex(@"\d+") 

public static bool ContainsNumberInRange(string str, int min, int max) 
{ 
    MatchCollection matches = NUMBER_REGEX.Matches(str); 
    foreach(Match match in matches) 
    { 
     int value = Convert.ToInt32(match.Value); 
     if (value >= min && value <= max) 
      return true; 
    } 

    return false; 
} 
+0

你可以在C++中使用boost :: regex。我不確定'\ d +'表達式;你可以更精確。例如,用\ d {1,2}'可以更有效地找到範圍3-35(假設X200Y匹配) – MSalters 2010-06-23 09:19:19

+0

我認爲X200Y不應該匹配,因爲它應該解析爲200,這不在範圍內。不過,這個問題還沒有被很好地解釋清楚。 – jdmichal 2010-06-23 18:07:03

0

如果您正在搜索範圍內的所有數字,請在循環中使用string :: find函數。下面是該函數的引用:

http://www.cplusplus.com/reference/string/string/find/

而且你想在字符串中使用的數字只有一次?例如SDFSD256fdsfs會給你數字2 5 6 25 56 256,如果你沒有在每次比賽結束後將它們從字符串中刪除。

0

您可以用stringstream對象迭代執行此操作。

#include <iostream> 
#include <string> 
#include <sstream> 
#include <cctype> 

void check (std::string& s) { 
    std::stringstream ss(s); 

    std::cout << "Searching string: " << s << std::endl; 
    while (ss) { 

     while (ss && !isdigit(ss.peek())) 
      ss.get(); 

     if (! ss) 
      break; 

     int i = 0; 
     ss >> i; 
     std::cout << " Extraced value: " << i << std::endl; 

    } 
} 


int main() { 

    std::string s1 = "XDGHYH20YFYFFY"; 
    std::string s2 = "20YF35YFFY100"; 
    check (s1); 
    check (s2); 
    return 0; 
} 

產量:

Searching string: XDGHYH20YFYFFY 
    Extraced value: 20 
Searching string: 20YF35YFFY100 
    Extraced value: 20 
    Extraced value: 35 
    Extraced value: 100 

添加參數到check功能,限制接受的將是微不足道的結果。

相關問題