Solved

standard template libraries

Posted on 2000-02-17
6
198 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

615 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