Reputation: 147
I have a number/string like this ( I am not sure how to convert int to and from strings)
000000122310200000223340000700012220000011411000000011011271043334010220001127100003333201000001000070005222500233400000000000000000000
What I need to do is to separate the numbers between 0's, so I get strings like
"12231" "22334" "7" "1222"
and so forth, and then I need to convert them into int. (basically I have searched to no avail of how to do the conversion)
Can someone help out?
Thanks!
Upvotes: 0
Views: 91
Reputation: 49976
Solution with std::getline reading until '0':
// 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";
}
Upvotes: 1
Reputation: 4031
Here a possible solution:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
std::vector<int> split(const std::string& x, char separator)
{
std::stringstream stream(x);
std::vector<int> numbers;
std::string line;
while(std::getline(stream, line, separator)) {
if (!line.empty()){
int n = std::stoi(line);
numbers.push_back(n);
}
}
return numbers;
}
int main()
{
std::string myStringValue("000000122310200000223340000700012220000011411000000011011271043334010220001127100003333201000001000070005222500233400000000000000000000");
std::vector<int> values = split(myStringValue,'0');
for(auto i : values) {
std::cout << i << std::endl;
}
}
Upvotes: 0
Reputation: 799
If C++11 is good for you, than this should work
#include <vector>
#include <string>
#include <sstream>
std::vector<int> split(const std::string& s, char delim)
{
std::vector<int> res;
std::stringstream ss(s);
std::string sub;
while (std::getline(ss, sub, delim))
{
int val = std::stoi(sub); // c++11
res.push_back(val);
}
return res;
}
Upvotes: 1
Reputation: 171
Just iterate from the string and check if it's '0' or not.
#include<bits/stdc++.h>
using namespace std;
int main() {
char a[] = "000000122310200000223340000700012220000011411000000011011271043334010220001127100003333201000001000070005222500233400000000000000000000";
vector<int> nums;
int tmp = 0;
for(int i=0;a[i];i++) {
if(a[i] != '0') {
tmp *= 10;
tmp += a[i]-'0';
} else {
if(tmp != 0){
nums.push_back(tmp);
tmp = 0;
}
}
}
for(int i=0;i<nums.size();i++)
cout << nums[i] << endl;
}
Upvotes: 0
Reputation: 179779
std::getline
takes an optional separator. Normally that would be a newline character (hence getline) but you could use 0
.
Upvotes: 2