Play with Streams

#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str)
{
    stringstream ss(str);
    vector<int> result;
    int temp_int;
    char temp_char;

    ss >> temp_int;
    result.push_back(temp_int);
    while (ss >> temp_char)
    {
        ss >> temp_int;
        result.push_back(temp_int);
    }
    return result;
}

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;
}

3 comments:

  1. Replies
    1. #include
      #include
      #include
      using namespace std;

      vector parseInts(string str)
      {
      stringstream ss(str);
      vector result;
      int temp_int;
      char temp_char;

      ss >> temp_int;
      result.push_back(temp_int);
      while (ss >> temp_char)
      {
      ss >> temp_int;
      result.push_back(temp_int);
      }
      return result;
      }

      int main()
      {
      string str;
      cin >> str;
      vector integers = parseInts(str);
      int c = integers.size();
      for (int i = 0; i < c; i++)
      {
      cout << integers[i] << "\n";
      }

      return 0;
      }

      Delete
  2. This comment has been removed by the author.

    ReplyDelete