Solved

standard template libraries

Posted on 2000-02-17
6
184 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now