Use istream_iterator to read various data types : Vector « Data Structure « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Data Structure » VectorScreenshots 
Use istream_iterator to read various data types


#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;

int main()
{
  int i;
  double d;
  string str;
  vector<int> vi;
  vector<double> vd;
  vector<string> vs;
 
  cout << "Enter some integers, enter 0 to stop.";
  istream_iterator<int> int_itr(cin);
  do {
    i = *int_itr; 
    if(i != 0) {
      vi.push_back(i);  
      ++int_itr; 
    }
  while (i != 0)

  cout << "Enter some doubles, enter 0 to stop.";
  istream_iterator<double> double_itr(cin);
  do {
    d = *double_itr; 
    if(d != 0.0) {
      vd.push_back(d)
      ++double_itr; 
    }
  while (d != 0.0)

  cout << "Enter some strings, enter 'quit' to stop.";
  istream_iterator<string> string_itr(cin);
  do {
    str = *string_itr; 
    if(str != "quit") {
      vs.push_back(str)
      ++string_itr;
    }
  while (str != "quit")

  cout << "Here is what you entered:\n";
  for(i = 0; i <vi.size(); i++)
    cout << vi<< " ";
  cout << endl;

  for(i = 0; i <vd.size(); i++)
    cout << vd<< " ";
  cout << endl;

  for(i = 0; i <vs.size(); i++)
    cout << vs<< " ";

  return 0;
}

           
       
Related examples in the same category
1.Perform an in-place merge for two vectorsPerform an in-place merge for two vectors
2.Using Other Search functionsUsing Other Search functions
3.Matching Elements Using the equals and mismatch OperationsMatching Elements Using the equals and mismatch Operations
4.Creating and Resizing VectorsCreating and Resizing Vectors
5.Demonstrate count and count_if.Demonstrate count and count_if.
6.Demonstrate remove_copy in VectorDemonstrate remove_copy in Vector
7.Access a vector using an iterator.Access a vector using an iterator.
8.Demonstrate insert and erase.Demonstrate insert and erase.
9.Store a class object in a vector. Store a class object in a vector.
10.Demonstrate allocator's max_size() fucntion in vectorDemonstrate allocator's max_size() fucntion in vector
11.Demonstrate count() in vectorDemonstrate count() in vector
12.Demonstrate count_if().Demonstrate count_if().
13.Demonstrate reverse in vectorDemonstrate reverse in vector
14.Demonstrate insert_iterator in vectorDemonstrate insert_iterator in vector
15.Demonstrate adjacent_difference() in vectorDemonstrate adjacent_difference() in vector
16.Demonstrate inner_product() in vectorDemonstrate inner_product() in vector
17.Demonstrate partial_sum() in Vector
18.Storing Class Objects with overloaded operators in a Vector
19.Vector Init Array
20.Use istream_iterator with the copy algorithm
21.Demonstrate remove_copy and replace_copy.
22.Vector: Insert Erase Sort
23.Demonstrate accumulate() in vector
24.end() in vector
25.Use pop_back() and empty().
26.Access the elements of a vector through an iterator.
27.The basic operation of a vector: size, push_back,
28.Accessing a Vector Through an Iterator
29.Create permutations based on vector
30.Work with heaps: make_heap from vector
31.Demonstrating the four ways that vectors can be created.
32.Vector Capacity vs size
33. Using clear()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.