question for looping through this map structure and text files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: question for looping through this map structure and text files

  1. #1
    Join Date
    Jan 2006
    Posts
    28

    question for looping through this map structure and text files

    e.g.. this is how i insert data into my map..

    typedef map<string, string> MapFunction;
    MapFunction dbase;
    dbase.insert(MapFunction::value_type("Helen", "Female67"));
    dbase.insert(MapFunction::value_type("Mark", "niceguy"));
    dbase.insert(MapFunction::value_type("Rosie", "Helens_sis"));

    how can i then loop through the dbase map and write the entries to c:\dbase.txt?

    i know for text writing, we use #include <iostream>, but im unsure as to how we loop through the map, and get each entry in the map and write it to the text file, so after the loop is done, the dbase.txt should be like this

    Helen=Femal67
    Mark=niceguy
    Rosie=Helens_sis

    I would much appreciate it if someone can help me

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    To use text files you need
    Code:
    #include <fstream>
    The text file will look like
    Code:
    Helen Femal67 Mark niceguy Rosie Helen_sis

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    You want to read this article about maps:
    http://www.devx.com/cplus/10MinuteSolution/21929
    In short, you have to use an iterator initialized to MapFunction::begin(), and then increment it until you reach end(). Then use the *it->first, *it->second to access the key and the associated value (i.e., the first and second strings) in each iteration.

    Regarding file I/O, see http://gethelp.devx.com/techtips/cpp.../10min0601.asp
    Danny Kalev

  4. #4
    Join Date
    Jan 2006
    Posts
    28
    thanks guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center