2013-06-26 46 views
4

我需要一些幫助,從std :: string中獲取所有整數並將每個整數轉換爲int變量。從字符串C++中讀取所有整數

字符串例子:

<blah> hi 153 67 216 

我想程序忽略「嗒嗒」和「喜」,並且每個整數存儲到一個int變量。所以它出來是這樣的:

a = 153 
b = 67 
c = 216 

然後我就可以自由打印每個單獨像:

printf("First int: %d", a); 
printf("Second int: %d", b); 
printf("Third int: %d", c); 

謝謝!

+0

搜索這個網站。這已被問及答覆數千次。 –

+0

''應該是什麼? – 0x499602D2

+0

@ 0x499602D2它可以是任何「sdnjid blah hi 23 35 2」這只是一個例子 – Mike

回答

8

您可以創建自己的函數,操縱通過使用其scan_is方法的std::ctype方面。然後您可以將生成的字符串返回到stringstream對象和插入內容到您的整數:

#include <iostream> 
#include <locale> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <iterator> 
#include <cstring> 

std::string extract_ints(std::ctype_base::mask category, std::string str, std::ctype<char> const& facet) 
{ 
    using std::strlen; 

    char const *begin = &str.front(), 
       *end = &str.back(); 

    auto res = facet.scan_is(category, begin, end); 

    begin = &res[0]; 
    end = &res[strlen(res)]; 

    return std::string(begin, end); 
} 

std::string extract_ints(std::string str) 
{ 
    return extract_ints(std::ctype_base::digit, str, 
     std::use_facet<std::ctype<char>>(std::locale(""))); 
} 

int main() 
{ 
    int a, b, c; 

    std::string str = "abc 1 2 3"; 
    std::stringstream ss(extract_ints(str)); 

    ss >> a >> b >> c; 

    std::cout << a << '\n' << b << '\n' << c; 
} 

輸出:

Demo

+0

+1優秀的解決方案。 –

+0

...但它缺少'#include '爲'std :: back_inserter' –

+0

@CaptainObvlious您說得對,謝謝。 ;) – 0x499602D2

1
  • 使用std::isdigit()檢查字符是否是數字。在未達到空間時,逐步添加到單獨的string
  • 然後使用std::stoi()將您的字符串轉換爲int。
  • 使用clear()方法清除字符串的內容。未達到
  • 轉到第一步

重複直到基座字符串的結尾。

1

首先使用string tokenizer

std::string text = "token, test 153 67 216"; 

char_separator<char> sep(", "); 
tokenizer< char_separator<char> > tokens(text, sep); 

然後,如果你不知道你會到底有多少價值得到,你不應該使用單變量a b c,但像int input[200],或更好的,一個std::vector一個數組,它可以適應你閱讀的元素的數量。

std::vector<int> values; 
BOOST_FOREACH (const string& t, tokens) { 
    int value; 
    if (stringstream(t) >> value) //return false if conversion does not succeed 
     values.push_back(value); 
} 

for (int i = 0; i < values.size(); i++) 
    std::cout << values[i] << " "; 
std::cout << std::endl; 

你必須:

#include <string> 
#include <vector> 
#include <sstream> 
#include <iostream> //std::cout 
#include <boost/foreach.hpp> 
#include <boost/tokenizer.hpp> 
using boost::tokenizer; 
using boost::separator; 

順便說一句,如果你正在編寫C++,你可能要避免使用printf,而寧願std::cout

0
#include <iostream> 
#include <string> 
#include <sstream> 
#include <algorithm> 
#include <iterator> 
#include <vector> 

int main() { 
    using namespace std; 
    int n=8,num[8]; 
    string sentence = "10 20 30 40 5 6 7 8"; 
    istringstream iss(sentence); 

    vector<string> tokens; 
copy(istream_iterator<string>(iss), 
    istream_iterator<string>(), 
    back_inserter(tokens)); 

    for(int i=0;i<n;i++){ 
    num[i]= atoi(tokens.at(i).c_str()); 
    } 

    for(int i=0;i<n;i++){ 
    cout<<num[i]; 
    } 

} 
0

讀取一行字符串並從中提取整數,

getline(cin,str); 
stringstream stream(str); 
while(1) 
    { 
    stream>>int_var_arr[i]; 
    if(!stream) 
      break; 
    i++; 
    } 
    } 

的整數存儲在int_var_arr []