Stringstream when iterating through string doesnt work

12,784

Solution 1

There are two points you should understand.

  1. If you use indexes to access string, you will get characters.
  2. istringstream requires string as a parameter not characters to create object.

Now you in your code

    int iterate = 0;
     while (iterate):
    /* here you are trying to construct istringstream object using  
 which is the error you are getting*/
        istringstream ss(test[iterate]); 
        int i;
        ss >> i;

To correct this problem you can following approach

istringstream ss(str); 
int i;
while(ss>>i)
{
    std::cout<<i<<endl
}

Solution 2

What you need is something like:

#include <iostream>
#include <sstream>

int main()
{
    std::string str = "12345";
    std::stringstream ss(str);
    char c; // read chars
    while(ss >> c) // now we iterate over the stringstream, char by char
    {
        std::cout << c << std::endl;
        int i =  c - '0'; // gets you the integer represented by the ASCII code of i
        std::cout << i << std::endl;
    }
}

Live on Coliru

If you use int c; instead as the type of c, then ss >> c reads the whole integer 12345, instead of reading it char by char. In case you need to convert the ASCII c to the integer that it represents, subtract '0' from it, like int i = c - '0';

EDIT As @dreamlax mentioned in the comment, if you just want to read the characters in the string and convert them to integers, there is no need to use a stringstream. You can just iterate over the initial string as

for(char c: str)
{
    int i = c - '0';
    std::cout << i << std::endl;
}
Share:
12,784
user3651193
Author by

user3651193

Updated on June 07, 2022

Comments

  • user3651193
    user3651193 almost 2 years

    So i want to use string stream to convert strings to integers.

    assume everything is done with:

     using namespace std;
    

    a basic case that seems to work is when I do this:

     string str = "12345";
     istringstream ss(str);
     int i;
     ss >> i;
    

    that works fine.

    However lets say I have a string defined as:

    string test = "1234567891";
    

    and I do:

    int iterate = 0;
    while (iterate):
        istringstream ss(test[iterate]);
        int i;
        ss >> i;
        i++;
    

    this doesnt work as i want. essentially I was to idividually work on each element of the string as if it is a number, so i want to convert it to an int first, but i cant seem too. Could someone please help me?

    the error i get is:

       In file included from /usr/include/c++/4.8/iostream:40:0,
                 from validate.cc:1:
    /usr/include/c++/4.8/istream:872:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
     operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
     ^
    /usr/include/c++/4.8/istream:872:5: note:   template argument     deduction/substitution failed:
    validate.cc:39:12: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
    cout >> i >> endl;