Solved

standard template libraries

Posted on 2000-02-17
6
183 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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