我想知道如何从C ++中的字符串中获取数字。我从输入中获取字符串并将获得多行,但我已经有了读取行的工作。 注意:这些行总是具有偶数个整数
以下是我希望代码看起来像:
std::getline(std::cin, line);// line looks something like "10 3 40 45 8 12"
int a, b;
while(!line.empty() /*line still has ints to extract*/) {
a = someMethod(line);//gets first int. So, 10 first loop, 40 second, 8 third
b = someMethod(line);//gets second int. So, 3 first loop, 45 second, 12 third
myMethod(a,b);//a method from elsewhere in my code. It's here so you know that I need both a and b
}
任何类似的东西都会有所帮助。非常感谢你!
答案 0 :(得分:2)
这是一个完整的例子。
#include <sstream>
#include <string>
#include <iostream>
int main(){
std::string line = "2 4 56 6";
std::stringstream stream(line);
int i;
while (stream >> i) {
std::cout << i << std::endl;
}
}
以下也可以正常工作,因此阅读多行不应该是个问题。
#include <sstream>
#include <string>
#include <iostream>
int main(){
std::string line = "2 4 56 6";
std::stringstream stream(line);
int i;
while (stream >> i) {
std::cout << i << std::endl;
}
line = "32 62 44 6 22 58 34 60 71 86";
stream.clear();
stream.str(line);
int a,b;
while(stream >> a && stream >> b){
std::cout << a << " " << b << "\n";
}
}
答案 1 :(得分:0)
你可以这样做:
string line;
getline(std::cin, line);// line looks something like "10 3 40 45 8 12"
int a, b;
vector<string> tokens;
istringstream iss(line);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
stringstream s;
for (int i=0;i<tokens.size()/2;i++)
{
s<< tokens[i];
s>>a;
s.clear();
s<<tokens[i+2];
s>>b;
s.clear();
myMethod(a,b);
}
答案 2 :(得分:0)
从字符串行获取标记并根据需要使用它们。
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
typedef boost::tokenizer<boost::char_separator<char> >
tokenizer;
void myMethod(int a, int b)
{
cout<<a<<" "<<b<<endl;
}
void getNumber(string line)
{
boost::char_separator<char> sep(" ");
tokenizer tokens(line, sep);
string evenStr, oddStr;
for(tokenizer::iterator iterToken=tokens.begin();
iterToken != tokens.end(); ++iterToken)
{
evenStr = *iterToken;
++iterToken;
if(iterToken != tokens.end())
{
oddStr = *iterToken;
myMethod(atoi(evenStr.c_str()), atoi(oddStr.c_str()));
}
}
}
int main()
{
string line("10 3 40 45 8 12");
getNumber(line);
return 0;
}
答案 3 :(得分:0)
有多种方法可以实现这一目标。我更喜欢使用boost。
示例:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
int main()
{
std::string line = "10 3 40 45 8 12 9"; //std::getline(std::cin, line);// line looks something like "10 3 40 45 8 12"
std::vector<std::string> intNumbers;
std::string s;
boost::split(intNumbers, line, boost::is_any_of(" "), boost::token_compress_on);
unsigned int i=0;
while(i < intNumbers.size())
{
try{
int a = boost::lexical_cast<int>(intNumbers[i++]);
if(i >= intNumbers.size())
{
std::cout << "invlaid values" << std::endl;
break;
}
int b = boost::lexical_cast<int>(intNumbers[i++]);
std::cout << "value a: " << a << std::endl;
std::cout << "value b: " << b << std::endl;
std::cout << "my method (multiply) a*b: " << (a*b) << std::endl;
}
catch(boost::bad_lexical_cast &e)
{
std::cout << "invlaid values" << std::endl;
}
}
}