Convert String Containing Several Numbers into Integers

Convert String containing several numbers into integers

I assume you want to read an entire line, and parse that as input. So, first grab the line:

std::string input;
std::getline(std::cin, input);

Now put that in a stringstream:

std::stringstream stream(input);

and parse

while(1) {
int n;
stream >> n;
if(!stream)
break;
std::cout << "Found integer: " << n << "\n";
}

Remember to include

#include <string>
#include <sstream>

How to convert a string containing multiple numbers to ints and store it in a vector

There is too main problem in your code.


First of all getline (ss, numList, ',' ); will stop on the first value of the list. In fact, when your list is 5,3,5, getline (ss, numList, ','); will read 5 then , so it will stop. At this point, numValue == "5"

This is quite simple to fix : Just remove the delimiter char, so getline(ss, numList);. Using this, numValue == "5,3,5"


Alright, now you have all your value. You replace ',' by ' ' in order to separate your numbers. Good, numList = "5 3 5".

And then is your second error : vec.push_back(stoi(numList));. stoi(numList) return an int and is not able to get through space characters. So it will only convert the first 5 and return it. You will never get the other numbers, as you don't even do a loop.

Here is my solution : convert your string to a stringstream and use >> operator

std::stringstream numStream(numList);
int value;
while(numList >> value)
vec.push_back(value);

So we end up with your final code (I removed stds, as it seems that you wrote using namespace std somewhere in your code)

struct randomStruct
{
string txt,
string nb,
vector<int> data
}
// -------
while(getline(file, line)){
stringstream ss(line);
getline(ss, text, ',');
getline (ss, nums, ':');
getline (ss, numList);
replace(numList.begin(), numList.end(), ',' , ' ');
stringstream numStream(numList);
int value;
while(numStream >> value)
vec.push_back(value);

randomStruct str = {text, nums, vec};

randomStructVec.push_back(str);
vec.clear();
}

// Accessing and printing data
for (auto str : randomStructVec)
{
for (auto i : str.data)
{
cout << i << " ";
}
cout << endl;
}

Convert String with multiple numbers to Integers in java

You should use split

int sum = 0;

String inputString = "5 10 20 10 5";
String []arr = inputString.split(" ");

for (String str : arr) {
sum += Integer.parseInt(str); //+= here
}

System.out.println("Average is " + (double) sum / arr.length);

C++ -- How to convert a string into multiple integers?

If you read a line of integers like "1 1 50 50" into a string and need to parse the integers from the string, then the standard C++ way to handle the conversion is by creating a stringstream from your string and using iostream to extract the integers from the stringstream.

For example:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

int main (void) {

std::istringstream is { "1 1 50 50\n" }; /* simulated input */
std::string s;
std::vector<int> vect;

while (getline (is, s)) { /* read string */
int f; /* declare int */
std::stringstream ss (s); /* make stringstream from s */
while ((ss >> f)) /* read ints from ss into f */
vect.push_back (f); /* add f to vector */
}

for (auto& i : vect) /* output integers in vector */
std::cout << i << '\n';
}

(if you need to store all lines individually, just use std::vector<std::vector<int>> vect;)

Note the initial istringstream was simply a way to simulate input for getline.

Example Use/Output

$ ./bin/ssint
1
1
50
50

Look things over and let me know if you have further questions.

How to convert a string filled with numbers to seperate integers in a list?

You can split the string , then convert each elements to int -


>>> s = '12 14 17'

>>> list(map(int,s.split()))
[12, 14, 17]
>>>

Parse string containing numbers into integer array

The end of file condition is not set upon a succesful parse, you have to check the state of the stream after parsing.

The second 76 is basically just pure chance. An unsuccesful parse leaves the target operand untouched, and because you did not initialize n, it can be anything.

A quickfix:

stream>>n;
if (stream)
cout<<n<<endl;

A cleaner fix:

int n;
while(stream >> n){
cout<<n<<endl;
}

To store those integers, the canonical way is to use std::vector if the number of elements is unknown. An example usage:

std::vector<int> values;
int n;
while(stream >> n){
...do something with n...
values.push_back(n);
}

However, you can use iterators over streams and use the following:

// Use std::vector's range constructor
std::vector<int> values(
(std::istream_iterator<int>(stream)), // begin
(std::istream_iterator<int>())); // end


Related Topics



Leave a reply



Submit