首先使用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
搜索這個網站。這已被問及答覆數千次。 –
''應該是什麼? –
0x499602D2
@ 0x499602D2它可以是任何「sdnjid blah hi 23 35 2」這只是一個例子 – Mike