Solved

standard template libraries

Posted on 2000-02-17
6
186 Views
Last Modified: 2010-04-02
I want to create a hash object. I guess I have to use the map class, but how ?

Can anybody give me the syntax to create such an object (with a char* (or a String class) as key and value), to insert a new key/value pair and to retrieve it.

Thanks
0
Comment
Question by:frey
6 Comments
 

Accepted Solution

by:
ilia239 earned 200 total points
ID: 2534108
Frey, I wrote you a whole small program
that can
1. Create hash (CString -> CString)
2. Add elements to this hash
3. Retrieve all elements from it
4. Find element by a key

ask for comments, if there are some unclear things...
and if you need whole sources - no problem.
best regards
Ilia

important things in header file:
/////////////////////////////////////////////////////////////////////////////
// CStlDlg dialog

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


typedef map<CString, CString, less<CString> > STRING_STRING_MAP;


class CStlDlg : public CDialog
{
// Construction
public:
      STRING_STRING_MAP m_Map;
      CStlDlg(CWnd* pParent = NULL);      // standard constructor



example of functions: (in cpp file)


void CStlDlg::OnInsert1()
// insert a pair of elements into m_Map
{
    m_Map.insert(STRING_STRING_MAP::value_type("Italian","English"));
      
}

void CStlDlg::OnButton3()
// just another insert
{
    m_Map.insert(STRING_STRING_MAP::value_type("English","Italian"));
      
}

void CStlDlg::OnPrintAll()
// retrieva all elements from m_Map
{
    STRING_STRING_MAP::iterator the_iterator;            
   
      CString s;

    for (the_iterator = m_Map.begin();          // find the words
         the_iterator != m_Map.end();
         the_iterator++)
    {
   
            s = s + (*the_iterator).first + "->" + (*the_iterator).second;

    }

      AfxMessageBox(s);
      
}




void CStlDlg::OnFind()
// performing a search in the map
{
    STRING_STRING_MAP::iterator the_iterator;             // iterator for Word table
   
      CString s;

    CString word = "English";


    the_iterator = m_Map.find(word);


if (the_iterator!=m_Map.end()) {
            s = (*the_iterator).second;      
      } else {
            s = "_NOT FOUND_";
      }

      AfxMessageBox(s);
      

}




0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2534337
Why do you use CString (it's MFC) and not string (plain STL)?
I would advise to use string, you even included <string>
Luc
0
 

Expert Comment

by:ilia239
ID: 2534352
LucHoltkamp, I agree with you.
You should use STL strings instead of CString in map<>. But the question was about String class (i've read it as "CString class" :-)).  By the way, MFC has its own CMap class and you can use it instead of map...

Ilia239
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2534401
map is not quite a hash (table). For hash tables one would expect the average complexity of (lookup) operations to be done in constant time, O(1). With map they are performed in logarithmic time O(log(N)).
0
 
LVL 22

Expert Comment

by:nietod
ID: 2534492
True, binary trees (maps) and hash tables hav very different properties and performances. The official STL does not have any hash table container (although many stl implimentations do have an unofficial one, often called "hashmap").  The ANSII/ISO standards committee will be meeting in a few years time to discusss extensions to the standard and will very likely add hash tables to the standard (It is already on the agenda), however the standard cannot be changed before 2007 or something like that.

If you don't want to wait :-), you will have to find a hashmap class or write your own.
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2542110
ilia, the MFC containers are not something I would advise anyone to use...
They only good thing about them is that they work, but compaired to stl, there are some ugly pieces of code...
Luc
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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