我有一个这样的数字/字符串(我不确定如何将int与字符串进行相互转换)
000000122310200000223340000700012220000011411000000011011271043334010220001127100003333201000001000070005222500233400000000000000000000
我需要做的是将数字分隔为0,所以我得到像
“ 12231”
“ 22334”
“ 7”
“ 1222”
等等,然后我需要将它们转换为int。
(基本上,我没有进行任何转换搜索)
有人可以帮忙吗?
谢谢!
最佳答案
std::getline读取直到为'0'的解决方案:
live
// First create a string stream with you input data
std::stringstream ss("000000122310200000223340000700012220000011411000000011011271043334010220001127100003333201000001000070005222500233400000000000000000000");;
// Then use getline, with the third argument it will read untill zero character
// is found. By default it reads until new line.
std::string line;
while(std::getline(ss, line, '0')) {
// In case there are no data, two zeros one by one, skip this loop
if ( line.empty() )
continue;
// now parse found data to integer
// Throws excepions if bad data, consult: http://en.cppreference.com/w/cpp/string/basic_string/stol
int n = std::stoi(line);
std::cout << n << "\n";
}
关于c++ - 以0分隔的C++字符串解析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36616046/