Is it possible to use sgi hash_map in vs2005?

Is it possible to use sgi hash_map in vs2005 without conflicting existing hash_map How to do it

I want to let this code work, which is an sgi example:

struct eqstr
{
 bool operator()(const char* s1, const char* s2) const
 {
  return strcmp(s1, s2) == 0;
 }
};

int main()
{
 hash_map<const char*, int, hash<const char*>, eqstr> months;
 
 months["january"] = 31;
 months["february"] = 28;
 months["march"] = 31;
 months["april"] = 30;
 months["may"] = 31;
 months["june"] = 30;
 months["july"] = 31;
 months["august"] = 31;
 months["september"] = 30;
 months["october"] = 31;
 months["november"] = 30;
 months["december"] = 31;
 
 cout << "september -> " << months["september"] << endl;
 cout << "april   -> " << months["april"] << endl;
 cout << "june   -> " << months["june"] << endl;
 cout << "november -> " << months["november"] << endl;
}



Answer this question

Is it possible to use sgi hash_map in vs2005?

  • dvferretm

    The problem is that you don't overwrote the
    size_t operator( )( const Key& Key ) const;
    function. But this is needed to get a hash value that is equal for all same strings. In your case the hasvalueis calculated based on the pointer not on the value!



  • Yuriy T

    I wrote this simple test.

    I inserted some CString* key into a hash_map, and judged if this key exists before inserting. *Why* are there so many duplicated keys in the hash_map HOW can I avoid this

    I don't want any duplicate keys.

    This is my simple code:

    #include <hash_map>
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <atlstr.h>
    #include <vector>
    #include <algorithm>

    using namespace stdext;
    using namespace std;

    struct less_str
    {
    bool operator()(const CString* p1, const CString* p2) const
    {
    if(*p1<*p2)
    return true;
    else
    return false;
    }
    };

    typedef stdext::hash_map<const CString*,CString*, hash_compare <const CString*, less_str>> key_result_map;
    typedef std::pair<const CString*,CString*> key_result_pair;

    class ResultList
    {
    public:
    key_result_map m_HashList;
    public:
    ResultList()
    {
    }

    void Add(CString const& key, CString const& value)
    {
    if(m_HashList.find(&key)==m_HashList.end())
    m_HashList.insert(key_result_pair(new CString(key),new CString(value)));
    }

    ~ResultList()
    {
    for(key_result_map::iterator it=m_HashList.begin();
    it!=m_HashList.end();it++)
    {
    delete it->first;
    delete it->second;
    }
    }

    void Save()
    {
    vector<CString*> vs;
    vs.reserve(m_HashList.size());
    for(key_result_map::iterator it=m_HashList.begin();
    it!=m_HashList.end();it++)
    {
    vs.push_back(new CString(*(it->first)));
    }
    sort(vs.begin(),vs.end(),less_str());

    ofstream f(_T("results.txt"));
    if(!f.fail())
    {
    for(vector<CString*>::size_type i=0;i<vs.size();i++)
    {
    f<<*vsIdea<<endl;
    delete vsIdea;
    }
    }
    }
    };

    int main()
    {
    ResultList rl;
    srand((unsigned)time(NULL));
    for(int i=0;i<100000;i++)
    {
    int x=int(((double) rand() / (double) 1000) * 1000);
    CString str;
    str.Format(_T("%d"),x);
    rl.Add(str,str);
    }
    rl.Save();
    return 0;
    }



  • Raviatr

     Martin Richter wrote:

    Don't include the headers for the std::hash_map inside the VS2005.

    Why do you want to use the SGI hash_map

    Read the SGI docs what to do to replace the VC2005 STL with the SGI STL!

    MS hash_map is not in the namespace std, but stdex.

    I want to try sgi hash_map because it has different interface to ms hash_map.

    I don't understand hash_map in vs2005. It doesn't use a hash function.



  • baty

     Martin Richter wrote:
    Why do you think the hash_map in VC2005 doesn't use a hash function Sure it does! Look at the hash_compare template!

    OK, I see.

    I  just have no idea why I got duplicate items in to hash_map while I check if my_hash_map.count(item_to_insert)==0 before I insert any items in it.

    sorry I posted it twice due to the net is too slow.



  • Tellek Liberty

    Don't include the headers for the std::hash_map inside the VS2005.

    Why do you want to use the SGI hash_map

    Read the SGI docs what to do to replace the VC2005 STL with the SGI STL!



  • Dr.9

    Why do you think the hash_map in VC2005 doesn't use a hash function Sure it does! Look at the hash_compare template!

  • Is it possible to use sgi hash_map in vs2005?