2013-04-24 84 views
5

以下代碼將std::string轉換爲int,問題在於它無法從真正的整數或隨機字符串中辨別出這一事實。有沒有一種系統的方法來處理這樣的問題?如何檢查std :: string,如果它的確是一個整數?

#include <cstring> 
#include <iostream> 
#include <sstream> 

int main() 
{ 
    std::string str = "H"; 

    int int_value; 
    std::istringstream ss(str); 
    ss >> int_value; 

    std::cout<<int_value<<std::endl; 

    return 0; 
} 

編輯:這是我很喜歡,因爲它是非常小的,優雅的解決方案!它不適用於負數,但我只需要積極的數字。

#include <cstring> 
#include <iostream> 
#include <sstream> 

int main() 
{ 
    std::string str = "2147483647"; 

    int int_value; 
    std::istringstream ss(str); 

    if (ss >> int_value) 
     std::cout << "Hooray!" << std::endl; 

    std::cout<<int_value<<std::endl; 


    str = "-2147483648"; 
    std::istringstream negative_ss(str); 

    if (ss >> int_value) 
     std::cout << "Hooray!" << std::endl; 

    std::cout<<int_value<<std::endl; 

    return 0; 
} 
+3

'if(ss >> int_value)''可能是一個好的開始。 – WhozCraig 2013-04-24 00:56:13

+0

@WhozCraig不確定你的意思,請你詳細說明一下? – pandoragami 2013-04-24 00:56:50

+1

*試試看。*用'if(ss >> int_value)std :: cout <<「Hooray!」替換當前的裸'ss >> int_value;'。 << std :: endl;' – WhozCraig 2013-04-24 00:58:52

回答

5

WhozCraig的做法好得多,我想用這種方法就可以了,擴大了C++ FAQ用途主要內容如下:

#include <iostream> 
#include <sstream> 
#include <string> 
#include <stdexcept> 

class BadConversion : public std::runtime_error { 
public: 
    BadConversion(std::string const& s) 
    : std::runtime_error(s) 
    { } 
}; 



inline int convertToInt(std::string const& s, 
           bool failIfLeftoverChars = true) 
{ 
    std::istringstream i(s); 
    int x; 
    char c; 
    if (!(i >> x) || (failIfLeftoverChars && i.get(c))) 
    throw BadConversion("convertToInt(\"" + s + "\")"); 
    return x; 
} 


int main() 
{ 
    std::cout << convertToInt("100") << std::endl ; 
    std::cout << convertToInt("-100") << std::endl ; 
    std::cout << convertToInt(" -100") << std::endl ; 
    std::cout << convertToInt(" -100 ", false) << std::endl ; 

    // The next two will fail 
    std::cout << convertToInt(" -100 ", true) << std::endl ; 
    std::cout << convertToInt("H") << std::endl ; 
} 

這是強大,就會知道,如果轉換失敗,您還可以選擇在字符上留下失敗。

+0

+1對於很好的自定義功能 – taocp 2013-04-24 01:14:29

+0

我認爲我在某一點上做了這樣的事情,雖然有點更一般,並提供了一些特定的'std :: stoi'和'boost :: lexical_cast'沒有。這就像'if(SafeInput :: Input(「輸入一個整數:」,someInt,SafeInput :: Options :: NoLeftovers)){...}':p – chris 2013-04-24 02:16:19

7

您可以嘗試使用升壓lexical_cast,它會拋出一個異常,如果轉換失敗。

int number; 
try 
{ 
    number = boost::lexical_cast<int>(str); 
} 
catch(boost::bad_lexical_cast& e) 
{ 
    std::cout << str << "isn't an integer number" << std::endl; 
} 

編輯 Accorinding到@克里斯,你也可以嘗試使用std::stoi因爲C++ 11。如果不能執行轉換,它將拋出std::invalid_argument異常。您可以在這裏找到更多的信息:std::stoi

+0

出於好奇,這是怎麼工作在'「-10」' – WhozCraig 2013-04-24 01:02:45

+0

多數民衆贊成在不錯的你tacp,但WhozCraig以非常簡單的方式回答它。謝謝。 – pandoragami 2013-04-24 01:05:38

+0

+1爲助推答案。我喜歡! – WhozCraig 2013-04-24 01:06:21

1
/* isdigit example */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
int main() 
{ 
    char str[]="1776ad"; 
    int year; 
    if (isdigit(str[0])) 
    { 
    year = atoi (str); 
    printf ("The year that followed %d was %d.\n",year,year+1); 
    } 
    return 0; 
} 
相關問題