   [STL]Map:Suggestions on how to print its contents
  Hey Guys:This is what I want to do: I'm using STL map to be able to construct a table in which I store some values(both key and values are ints in this test program but I want top be able to upgrade this to be able to use this as a Id generator with key being the name/something unique and the id is the value)What I have a problem is with this:1) I want to have a method to print the contents of the table to the console window but in doing so I came up with this problem:In a map if I need to iterate over ALL the key values WITHOUT doins a find(), which REQUIRES me to know the EXACT key I'm looking for ,HOW do I do it?To overcome this problem , I store all my keys in a vector first and then use that to find the appropriate value using the key from the vector. This is a STUPID way ( I think and I'm sure you'll tell me the same, )Any suggestions?2) Is there a built-in function or something that allows me to view the contents of the map?3) Any suggestions on how I could further improve this elementary program to be able to get what I desire.Thanks![EDIT]: eeks.. forgot to add the code... Here it is:   #pragma warning(disable: 4786)#include <iostream>#include <vector>#include <map>#include <assert.h>using namespace std;typedef std::map< int, int > TableType;class testClass{private:TableType test;vector<int> vec;public:void BuildTable( vector<int> v ){vec = v;vector<int>::iterator p;p = v.begin();while ( p!= v.end() ){assert( test.find( *p ) == test.end() );test.insert( std::make_pair( *p, *p + 10 ) );p++;}}void ModifyTable( int keyToInsertAt, int valueToInsert ){TableType::iterator p = test.find( keyToInsertAt  );assert( p != test.end());p->second = valueToInsert;}void PrintTable(){TableType::iterator p;p = test.begin();int i = 0;cout << "\n" << "\n";cout << "KEY" << "\t" << " VALUE" << "\n";while ( p != test.end() ){TableType::iterator temp = test.find( vec[i]) ;cout << vec[i] << "\t" << p->second << "\n";p++;i++;}}};int main(){testClass *ob = new testClass();vector<int> v( 10 );for (int j = 0; j < 10 ; j++ )v[j] = j + 1;ob->BuildTable( v );cout << " Table BEFORE \n";ob->PrintTable();ob->ModifyTable( 1 , 100 );cout << " Table AFTER \n";ob->PrintTable();return 0;}[edited by - hehenoobhehe on August 4, 2003 9:09:11 PM]
  I guess I''m not understanding the problem.  You can iterate a map just like any other STL container, with begin() and end().  You won''t get things sorted (necessarily), but it does go over each item.   I like pie. ;  Right. an associative container''s iterator class is an iterator which, when dereferenced, will produce a pair<key, value>. So do something like this: void printMap(const std::map<std::string, std::string> &myMap){    for(std::map<std::string, std::string>::const_iterator i = myMap.begin(); i != myMap.end(); ++i)    {   cout << "Key: " << i->first << "\tValue: " << i->second << endl;    }} How appropriate. You fight like a cow. ;quote:Original post by RenderTarget  I guess I''m not understanding the problem.  You can iterate a map just like any other STL container, with begin() and end().  You won''t get things sorted (necessarily), but it does go over each item.   I like pie. Yes, thats what Im doing as well with the following: test.begin();//name of map I usetest.end();BUT the problem ios that I still have to use the find    function to be able to access the element ==> I need to know the key in advance!Is there anyway that I could use just the iterator to be able to access the value stored at the particular key?In the function PrintTable, I use the iterator p, but am unable to access the contents of the map like I can do for a vector(for example) vector<int> v;vector<int>::iterator p;p = v.begin();while(p != v.end()){ cout << *[ <<"\n";p++;}This is the way I want to access the elemnts of the map, But I dont know how to, Does this explain a little better what I want?Sorry for not providing enough details the first time around.;quote:Original post by Sneftel  Right. an associative container''s iterator class is an iterator which, when dereferenced, will produce a pair. So do something like this:Thanks this works and this si what I wanted....Just didnt know how to express myself exactly what it was, Thanks anyways... STL is confusing for beginners.;  Map has bidirectional iterators IIRC: template<class OS, class BI>OS& write_csv(OS& os, BI begin, BI end){   BI last = end;   --last;if(begin!=end){for(; begin!=last; ++begin)os << *begin << ",";os << *last;}return os;}template<class OS, typename T1, typename T2>OS& operator<<(OS& os, std::pair<T1, T2>& p){os << "[" << p.first << ", " << p.second << "]";return os;}template<class OS, typename T1, typename T2>OS& operator<<(OS& os, std::pair<T1, T2*>& p){os << "[" << p.first << ", " << *p.second << "]";return os;}write_csv(std::cout, mymap.begin(), mymap.end());    [edited by - Magmai Kai Holmlor on August 5, 2003 1:05:42 AM];  nice ;  It just got better 