Yurttas/PL/OOL/Cplusplus/F/06/02/01/05/io gs 05.cpp

 1/*
 2   Copyright(C) 2005
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9
10   date   : September 1, 2005.
11   authorĀ : Salih Yurttas.
12
13   io_gs_05.cpp
14*/
15
16
17#include <vector>
18#include <string>
19
20#include <iostream>
21#include <sstream>
22
23using namespace std;
24
25int main(int argc, char* argv[]) {
26
27  string s = "4 8 2";
28
29  int l = s.length();
30  int k = 0;
31  for(int i=0; i<l; ++i)     // count spaces(separators).
32    if(s.at(i)==' ')
33      ++k;
34
35  istringstream ins;         // construct an input string stream object.
36
37  vector<int> v;
38
39  ins.str(s);                // specify input string.
40
41  int a;
42
43  for(int i=0; i<k+1; ++i) {
44    ins >> a;                // input the integers from the string.
45    v.push_back(a);
46  }
47
48  int n = v.size();
49  for(int i=0; i<n; ++i)
50    cout << v.at(i) << endl;
51
52}