Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

URGENT:  iterating through Hash Table

Posted on 2003-11-04
5
409 Views
Last Modified: 2010-04-01
I have an STL hashTable keyed on strings and the objects are custom objects.  What is the best way to go and get each pair (I need to ouptut both the key and value together).
0
Comment
Question by:jjacksn
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:nil_dib
ID: 9684988
sample for map (hashTable should be similar) :

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <map>

using namespace std ;

typedef map<string, int> STRING2INT;

void main()
{
    STRING2INT MyMap;
    STRING2INT::iterator MyIterator;
    string MyBuffer;

    // print the maximum number of <key,data> pairs that MyMap can hold
    cout << "MyMap is capable of holding " << MyMap.max_size()
         << " <string,int> pairs" << endl;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;
        cout << "Entering new entries in MyMap" << endl;
    // Fill in MyMap with the months of the year, mapped to their number
    // January - 1, February - 2, etc. using operator[].
    MyMap["January"] = 1;
    MyMap["February"] = 2;
    MyMap["March"] = 3;
    MyMap["April"] = 4;
    MyMap["May"] = 5;
    MyMap["June"] = 6;
    MyMap["July"] = 7;
    MyMap["August"] = 8;
    MyMap["September"] = 9;
    MyMap["October"] = 10;
    MyMap["November"] = 11;
    MyMap["December"] = 12;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

    // Ask the user for a month of the year and print the number
    // that corresponds to the month entered
    //MyIterator = MyMap.end();
    while(MyIterator == MyMap.end()){
        cout << "Enter a Month :";
        cin >> MyBuffer;
        if((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
            cout << (*MyIterator).first << " is Month Number "
                 << (*MyIterator).second << endl;
        else
            cout << "Enter a Valid Month (example: March)" << endl;
    }

    // empty MyMap - note that clear simply calls erase(begin(),end());
    MyMap.clear();

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;
        cout << "Entering new entries in MyMap" << endl;
    // Fill MyMap with the days of the week, each mapped to an int
    MyMap["Monday"] = 1;
    MyMap["Tuesday"] = 2;
    MyMap["Wednesday"] = 3;
    MyMap["Thursday"] = 4;
    MyMap["Friday"] = 5;
    MyMap["Saturday"] = 6;
    MyMap["Sunday"] = 7;

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

    // Ask the user for a day of the week and print the number
    // that corresponds to the day entered
    MyIterator = MyMap.end();
    while(MyIterator == MyMap.end()){
        cout << "Enter a Day of the Week:";
        cin >> MyBuffer;
        if((MyIterator = MyMap.find(MyBuffer)) != MyMap.end())
            cout << (*MyIterator).first << " is Day Number "
                 << (*MyIterator).second << endl;
        else
            cout <<"Enter a Valid Day of the Week(example: Monday)"<< endl;
    }
// Now clear MyMap again - this time using erase instead of clear
    MyMap.erase(MyMap.begin(), MyMap.end());

    if(!MyMap.empty())
        cout << "MyMap has " << MyMap.size() << " entries" << endl;
    else
        cout << "MyMap is empty" << endl;

}



nil_dib
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9685019
am I correct in thinking that If I make an iterator, .first will contain the key and .second will contain the value?
0
 
LVL 4

Accepted Solution

by:
nil_dib earned 300 total points
ID: 9685101
no! look at this sample:

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <map>

using namespace std;


typedef map<int, string, less<int> > INT2STRING;

void main()
{
// 1. Create a map of ints to strings
    INT2STRING theMap;
    INT2STRING::iterator theIterator;


// Fill it with the digits 0 - 9, each mapped to its string counterpart
// Note: value_type is a pair for maps...
    theMap.insert(INT2STRING::value_type(0,"Zero"));
    theMap.insert(INT2STRING::value_type(1,"One"));
    theMap.insert(INT2STRING::value_type(2,"Two"));
    theMap.insert(INT2STRING::value_type(3,"Three"));
    theMap.insert(INT2STRING::value_type(4,"Four"));
    theMap.insert(INT2STRING::value_type(5,"Five"));
    theMap.insert(INT2STRING::value_type(6,"Six"));
    theMap.insert(INT2STRING::value_type(7,"Seven"));
    theMap.insert(INT2STRING::value_type(8,"Eight"));
    theMap.insert(INT2STRING::value_type(9,"Nine"));

            
  for (theIterator=theMap.begin();theIterator!=theMap.end();theIterator++)
     cout << "key " << (*theIterator).first << " value." << (*theIterator).second << endl;
}


acces to key and value via ONE iterator ((*theIterator).first and (*theIterator).second)

nil_dib
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9685197
Isn't that exactly the question I just asked?  
0
 
LVL 4

Expert Comment

by:nil_dib
ID: 9685215
oh, sure it is!
sorry, missunderstoud your comment ...

nil_dib
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 66
C++ Language error 28 246
How can i compile this github project?? 2 90
C++ Code Issue 4 25
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question