DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Associative Arrays in C++ - Map(C++)

An Iterator Example

The output for the program above was produced using the default stream insertion operator which comes with Maps. Suppose we prefer a prettier output, consisting of two columns of integer/string pairs. To do this, we use an iterator. Simply replace the line

   cout << count << endl;

with the following code:

   Mapiter<String,int> p(count);
   while (p.next())
           cout << p.curr()->value << '\ t'
                << p.curr()->key << endl;

Here is the resulting output when the new program is run with itself as input:

   3       #include
   1       '\ t'
   1       (cin
   1       (p.next())
   4       <<1       <Map.h>
   1       <String.h>
   1       <stream.h>
   1       >>
   1       Map<String,int>
   1       Mapiter<String,int>
   1       String
   1       count;
   1       count[word]++;
   1       cout
   1       endl;
   1       main()
   1       p(count);
   1       p.curr()->key
   1       p.curr()->value
   2       while
   1       word)
   1       word;
   1       {
   1       }

Next topic: Performance
Previous topic: Iterators

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005