string stream and vectors in cpp

Could anybody solve this program work for every input we give to extract them and printing them by using vectors and string stream.
I am succeeded in obtaining output if i have known size of input but i need it to work for unknown size of string. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) {
   stringstream ss;
   string temp;
   int i=0;
   vector<int> d;
   ss<<str;
   while( !ss.eof() )
   {
       ss>>temp;
       if(stringstream(temp)>>i)
       {
           d.push_back(i);
       }
       i++;
   }
   return d;
   
}

int main() {
    string str;
    cin>>str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    
    return 0;
}
What do you include in "every input"?
Please give examples of the type of input you want to accept.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

vector<int> parseInts(const string & str)
{
   vector<int> d;

   stringstream ss(str);

   string temp;   

   while ( ss >> temp )
   {
       stringstream st(temp);

       while ( st  )
       {
           int i;
           if (st >> i)
               d.push_back(i);
           else
           {
               st.clear();
               st.get();
           }
        }               
   }
   
   return d;
}

int main()
{
    cout << "Enter some integers\nType q to quit\n";
    
    string str;
    
    while (getline(cin, str) && str != "q")
    {
        vector<int> integers = parseInts(str);
        
        
        for (auto n : integers)
            cout << n << "\t";
        
        
        if (integers.size())
            cout << '\n';
    }

}


Last edited on
Thank you a lot chervil
Could explain this program segment.
vector<int> d;

stringstream ss(str);

string temp;

while ( ss >> temp )
{
stringstream st(temp);

while ( st )
{
int i;
if (st >> i)
d.push_back(i);
else
{
st.clear();
st.get();
}
}
}
I think you already understand most of it? It uses some similar or same code to your own.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
vector<int> parseInts(const string & str)
{
   vector<int> d;                 // vector of integers.

   stringstream ss(str);          // stringstream, nitialise with str

   string temp;   

   while ( ss >> temp )           // loop while a string can be extracted from ss
   {
       stringstream st(temp);     // another stringstream, contains just one 'word'

       while ( st  )              // loop while status of stringstream is good
       {
           int i;
           if (st >> i)           // try to read an integer 
               d.push_back(i);    // success, store it
           else
           {
               st.clear();        // failed, so reset status flags
               st.get();          // read and discard a single char
           }
        }               
   }
   
   return d;                      // return result
}


The idea behind it, rightly or wrongly - because I still don't know what input you would like to accept, is to deal with a "word" like this:

fghd86g890g68d9g69dfg689dfhsd

and extract these integers:
86      890     68      9       69      689


Or a "word" like this: 3.14159 would result in these two integers.
3       14159


In the latter example, it first reads 3, stores it. Then tries to read another integer, but fails. So it instead discards one character (could also use the ignore() function), and tries again.
Last edited on
Topic archived. No new replies allowed.