Code.GeekInterview.com
 
Code Samples C++
 

Using Stream Iterators with Containers


Code ResourceAuthor: jbode  

Difficulty Level: Intermediate

Published: 19th Jan 2011   Read: 2188 times  

Filed in: C++
Add Comment


 

 

Sponsored Links


 

 

The following sample illustrates how to a) load data from an input stream into a map container using input stream iterators and b) write container contents to standard output using an output stream iterator.

 


Sample Code
  1. #include <iostream>
  2. #include <iterator>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <map>
  6.  
  7. /**
  8.  * Struct type to store data read from input stream.
  9.  * Provides a cast operator to convert struct data
  10.  * to a std::pair suitable for insertion into the map
  11.  */
  12. struct Record
  13. {
  14.   int         m_id;
  15.   std::string m_name;
  16.  
  17.   Record() {}
  18.   Record(int id, const std::string& name) : m_id(id), m_name(name) {}
  19.   Record(const Record& rec) : m_id(rec.m_id), m_name(rec.m_name) {}
  20.  
  21.   /**
  22.    * Cast operator
  23.    */
  24.   operator std::pair<const int, std::string>() const { return std::make_pair(m_id, m_name); }
  25. };
  26.  
  27. /**
  28.  * Converts a map entry to a formatted string.  
  29.  */
  30. std::string toString(std::pair<const int, std::string> it)
  31. {
  32.   std::stringstream tmp;
  33.   tmp << "{id: " << it->first << "; name: " << it->second << "}";
  34.   return tmp.str();
  35. }
  36.  
  37. /**
  38.  * Overload the input operator for the Record data type
  39.  */
  40. std::istream& operator>>(std::istream& str, Record& rec)
  41. {
  42.   str >> rec.m_id >> rec.m_name;
  43.   return str;
  44. }
  45.  
  46. /**
  47.  * Main
  48.  */
  49. int main(void)
  50. {
  51.   /**
  52.    * Set up the input stream.  For this example, we'll use
  53.    * a string stream. The data object contains the "file"'s
  54.    * contents; a sequence of id-name pairs.
  55.    */
  56.   std::string data = "1 foo 2 bar 3 bletch 4 blurga";
  57.   std::stringstream in;
  58.   int.str(data);
  59.  
  60.   /**
  61.    * The map that we'll be loading the above data into
  62.    */
  63.   std::map<int, std::string> vmap;
  64.  
  65.   /**
  66.    * Read the entries from the input stream using an
  67.    * istream iterator and insert them into the map
  68.    */
  69.   std::copy(std::istream_iterator<Record>(in), // initially points to the beginning of the stream
  70.             std::istream_iterator<Record>(),   // indicates end of stream
  71.             std::inserter(vmap, vmap.end()));  // creates an output iterator pointing to the end of the map
  72.  
  73.   std::cout << "Contents of container as read from input stream: " << std::flush;
  74.  
  75.   /**
  76.    * Iterate over the contents of the container and format
  77.    * them for standard output.  Need to use the transform
  78.    * function since the output iterator doesn't know how to
  79.    * format std::pair types
  80.    */
  81.   std::transform(vmap.begin(),
  82.                  vmap.end(),
  83.                  std::ostream_iterator<std::string>(std::cout, " "),
  84.                  toString);
  85.   std::cout << std::endl;
  86.  
  87.   return 0;
  88. }
Copyright GeekInterview.com


Next Article: Program to print asterisk(*) pattern in C++


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. Priya38159
2. chowsys15750
3. bipin_vaylu14163
4. raghu.mohindru9083
5. Subashini7757
6. rachittyagi7545
7. vimal.fire7413
8. venki_madesh6541
9. prabha6526
10. vipin gupta6336

Active Coders

Refined Tags