StringStream

Sort by

recency

|

554 Discussions

|

  • + 0 comments

    It’s a simple task, but it really strengthens your understanding of string handling in C++. Radhe Exch

  • + 0 comments
    vector<int> parseInts(string str) {
    	stringstream ss(str);
        vector<int> out;
        char ch;
        int temp;
        while(ss >> temp){
            ss >> ch;
            out.push_back(temp);
        }
        return  out;
    
  • + 0 comments

    While the stringstream is reading from the stream (string in this case):

    vector<int> parseInts(string str) {
        vector<int> v;
        int n;
        char c;
        stringstream ss(str);
        while(ss) {
            ss >> n >> c;
            v.push_back(n);
        }
        return v;
    }
    
  • + 0 comments

    Very easy intuition

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    
    int main() {
        string s;
        cin >> s;
        stringstream ss(s);
        char ch;
        int a;
        
        while (true) {
            ch = '0';
            ss >> a >> ch;
            cout << a << endl;
            if (ch != ',') {
                break;
            }
        }
        
        return 0;
    }
    
  • + 0 comments
    #include <sstream>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    vector<int> parseInts(string str) {
    	// Complete this function
        
        vector<int> numbers;
        int number;
        for(int i = 0; i < str.size(); i++){
            if(str[i] == ','){
                str[i] = ' ';
            }
        }
        stringstream ss(str);
        while(ss >> number){
            numbers.push_back(number);
        }
        return numbers;
    }
    
    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;
    }