Loading a hash table from .txt

codebear
codebear used Ask the Experts™
on
I have a .txt file filled with information such as first name, last name, title, and person number. I need to load a hash table with this information and use the chaning method to handle any conflict that arises.  How do I go about this?
Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Steps:
-Open the file.
-Extract what variables you need for your recordset.
-Test the std::map for existance of the key you want to add.
-If it's here do some error handling
-Else proceed to load the key in your map. Code goes like:

#include <string>
#include <map>
#include <fstream>
#include <iostream>
using namespace std;

struct record{
     string name, prename, title;
     record(){}
     record( string n, string p, string t ):name(n),prename(p),title(t){}
};

typedef map<int, record> db;
typedef map<int, record>::iterator dbIter;

void main()
{
     db Mydb;
     ifstream infile;

     infile.open( "test.txt", ios_base::in );
     if( !infile.good() ) return;
     while( infile.good() )
     {
          string name, prename, title;
          int id;
          infile >> id >> name >> prename >> title;
          dbIter Hit = Mydb.find( id );
          if( Hit == Mydb.end() || Mydb.empty() )
          {
               Mydb[id] = record( name, prename, title );
          }
          else
          {
               cout << "Id " << id << " already in database" << endl;
          }
     }
     cout << "The Database size is:" << Mydb.size() << " Records" << endl;
     for( dbIter i=Mydb.begin(); i!=Mydb.end(); i++ )
     {
          cout << "Name:" << i->second.name << endl;
     }
}


Sample data is like:

1     Somelast     Somefirst     Anytitle
2     Hans          Zimmer          Composer
1     Wrong          asdf          asdfasdf
3     John          Woo          Director

Commented:
Homework?

Commented:
Um, std::map is a sorted associative container and thus cannot be implemented using a hash table (it uses a tree).  The difference may seem trivial but don't expect the constant time complexity (on average) of a hashed container from std::map.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

EOL

Commented:
well, nobody keeps you from implementing your own template hash that works to the synopsis of the stl, or use the cgi stl that provides a hash in the traditional sense.

Commented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "EOL"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial