Solved

standard template libraries

Posted on 2000-02-17
6
188 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
Template syntax for variable length arrays 9 75
convert char array to number in c 5 86
DCT of  2D array using fftw in c++ 9 72
I could not set window to top 4 23
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

808 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