C++中hash_map用法 orz锟大神

在所有操作中hash_map比map性能优越些。

一、构造函数
      默认构造函数、部分后全部复制另外一个hash_map。
#include <ostream>
#include <hash_map>
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

typedef char * MyStr;

struct MyInt
{
    int i;
    friend ostream& operator<<(ostream& ii, MyInt& jj);
    MyInt(int j = 0) {
        i = j;
    }
};

struct greater_str
{
    bool operator()(const MyStr & x, const MyStr & y) const {
        if (strcmp(x, y) < 0)
            return true;

        return false;
    }
};


struct less_str {
    bool operator()(const MyStr & x, const MyStr & y) const
    {
        if (strcmp(x, y) > 0)
            return true;

        return false;
    }
};

ostream& operator<<(ostream& o, MyInt& j) {
    o << j.i;
    return o;
}

int main()
{
    using namespace stdext;
    typedef pair <MyStr, MyInt> Int_Pair;
    hash_map<MyStr, MyInt>::iterator hm1_Iter, hm3_Iter, hm4_Iter, hm5_Iter, hm6_Iter, hm7_iter;
    hash_map<MyStr, MyInt, hash_compare<MyStr, greater_str> >::iterator hm2_Iter;

    // Create an empty hash_map hm0 of key type integer
    hash_map<MyStr, MyInt> hm0;

    // Create an empty hash_map hm1 with the key comparison function of less than, then insert 4 elements
    hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm1;
    hm1.insert(Int_Pair("one", 0));
    hm1.insert(Int_Pair("two", 10));
    hm1.insert(Int_Pair("three", 20));
    hm1.insert(Int_Pair("four", 30));
    hm1.insert(Int_Pair("five", 40));

    // Create an empty hash_map hm2 with the key comparison function of geater than, then insert 2 elements
    hash_map<MyStr, MyInt, hash_compare<MyStr, greater_str> > hm2;
    hm2.insert(Int_Pair("one", 10));
    hm2.insert(Int_Pair("two", 20));

    // Create a hash_map hm3 with the allocator of hash_map hm1
    hash_map<MyStr, MyInt>::allocator_type hm1_Alloc;
    hm1_Alloc = hm1.get_allocator(); //成员函数get_allocator(Returns a copy of the allocator object used to construct the hash_map)
    hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm3(hash_compare<MyStr, less_str > (), hm1_Alloc);
    hm3.insert(Int_Pair("three", 30));

    // Create a copy, hash_map hm4, of hash_map hm1
    hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm4(hm1);

    // Create a hash_map hm5 by copying the range hm1[_First, _Last)
    hash_map<MyStr, MyInt>::const_iterator hm1_bcIter, hm1_ecIter;
    hm1_bcIter = hm1.begin();
    hm1_ecIter = hm1.begin();
    hm1_ecIter++;
    hm1_ecIter++;
    hash_map<MyStr, MyInt> hm5(hm1_bcIter, hm1_ecIter);

    // Create a hash_map hm6 by copying the range hm4[_First, _Last) and with the allocator of hash_map hm2
    hash_map<MyStr, MyInt>::allocator_type hm2_Alloc;
    hm2_Alloc = hm2.get_allocator();
    hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm6(hm4.begin(), ++hm4.begin(), hash_compare<MyStr, less_str > (), hm2_Alloc);

    cout << "hm1 =";
    for (hm1_Iter = hm1.begin(); hm1_Iter != hm1.end(); hm1_Iter++)
        cout << " " << hm1_Iter -> second;
    cout << endl;

    cout << "hm2 =";
    for (hm2_Iter = hm2.begin(); hm2_Iter != hm2.end(); hm2_Iter++)
        cout << " " << hm2_Iter -> second;
    cout << endl;

    cout << "hm3 =";
    for (hm3_Iter = hm3.begin(); hm3_Iter != hm3.end(); hm3_Iter++)
        cout << " " << hm3_Iter -> second;
    cout << endl;

    cout << "hm4 =";
    for (hm4_Iter = hm4.begin(); hm4_Iter != hm4.end(); hm4_Iter++)
        cout << " " << hm4_Iter -> second;
    cout << endl;

    cout << "hm5 =";
    for (hm5_Iter = hm5.begin(); hm5_Iter != hm5.end(); hm5_Iter++)
        cout << " " << hm5_Iter -> second;
    cout << endl;

    cout << "hm6 =";
    for (hm6_Iter = hm6.begin(); hm6_Iter != hm6.end(); hm6_Iter++)
        cout << " " << hm6_Iter -> second;
    cout << endl;

    // Create a copy, hash_map hm7, of hash_map hm1 by moving
    hash_map<MyStr, MyInt, hash_compare<MyStr, less_str> > hm7(move(hm1));

    cout << "hm7 =";
    for (hm7_Iter = hm7.begin(); hm7_Iter != hm7.end(); hm7_Iter++)
        cout << " " << hm7_Iter -> second;
    cout << endl;
}

二、基本操作方法
      访问元素——正向迭代和反向迭代:begin()、end() 与 rbegin()、rend()
      返回迭代器——hash_map::iterator(最常用的) 和 hash_map::const_iterator、hash_map::reverse_iterator(双向迭代器)

      操作符operator[ ](Inserts an element into a hash_map with a specified key value)
   typedef pair <const int, int> cInt2Int;
   hash_map <int, int> hm1;
   hash_map <int, int> :: iterator pIter;
   
   // Insert a data value of 10 with a key of 1 into a hash_map using the operator[] member function
   hm1[ 1 ] = 10;

   // Compare other ways to insert objects into a hash_map
   hm1.insert ( hash_map <int, int> :: value_type ( 2, 20 ) );
   hm1.insert ( cInt2Int ( 3, 30 ) );

   cout << "The keys of the mapped elements are:";
   for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout << "The values of the mapped elements are:";
   for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // If the key already exists, operator[] changes the value of the datum in the element
   hm1[ 2 ] = 40;

   // operator[] will also insert the value of the data type's default constructor if the value is unspecified
   hm1[5];

   cout << "The keys of the mapped elements are now:";
   for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout << "The values of the mapped elements are now:";
   for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

    hash_map的大小:size()成员函数;
    重新置hash_map为空:clear()成员函数;
    交换两个hash_map之间的元素:swap(有两种版本:一种是hash_map自身的成员函数;一种是<algrithm>中方法)
   hash_map <int, int> hm1, hm2, hm3;
   hash_map <int, int>::iterator hm1_Iter;
   typedef pair <int, int> Int_Pair;

   hm1.insert ( Int_Pair ( 1, 10 ) );
   hm1.insert ( Int_Pair ( 2, 20 ) );
   hm1.insert ( Int_Pair ( 3, 30 ) );

   hm2.insert ( Int_Pair ( 10, 100 ) );
   hm2.insert ( Int_Pair ( 20, 200 ) );

   hm3.insert ( Int_Pair ( 30, 300 ) );

   hm1.swap( hm2 );//此时,hs1中的元素就是100,200

   swap( hm1, hm3 );//此时,hs1中的元素就是300

    查找某个元素:find()成员函数;
    判断是否为空:empty()成员函数;
    统计hash_map中某个元素的个数:count( element );要么是1(即出现了该元素),要么是0(即没出现这样的元素)

    比较两个hash_map之间的关系——操作符operator==、operator!=、operator>=、operator<=
   hash_map <int, int> hm1, hm2, hm3, hm4;
   typedef pair <int, int> Int_Pair;

   for ( int i = 1 ; i < 3 ; i++ )
   {
      hm1.insert ( Int_Pair ( i, i ) );
      hm2.insert ( Int_Pair ( i, i * i ) );
      hm3.insert ( Int_Pair ( i, i - 1 ) );
      hm4.insert ( Int_Pair ( i, i ) );
   }

   if ( hm1 == hm2 )
      cout << "The hash_maps hm1 and hm2 are equal." << endl;
   else
      cout << "The hash_maps hm1 and hm2 are not equal." << endl;

   if ( hm1 <= hm2 )
      cout << "The hash_map hm1 is less than or equal to the hash_map hm2." << endl;
   else
      cout << "The hash_map hm1 is greater than the hash_map hm2." << endl;

   if ( hm1 != hm2 )
      cout << "The hash_maps hm1 and hm2 are not equal." << endl;
   else
      cout << "The hash_maps hm1 and hm2 are equal." << endl;
   if ( hm1 >= hm3 )
      cout << "The hash_map hm1 is greater than or equal to the hash_map hm3." << endl;
   else
      cout << "The hash_map hm1 is less than the hash_map hm3." << endl;

   if ( hm1 == hm4 )
      cout << "The hash_maps hm1 and hm4 are equal." << endl;
   else
      cout << "The hash_maps hm1 and hm4 are not equal." << endl;

    移除元素:erase()成员函数(Removes an element or a range of elements in a hash_map from specified positions or removes elements that match a specified key)

    hash_map<int, int> hm1, hm2, hm3;
    hash_map<int, int> :: iterator pIter, Iter1, Iter2;
    hash_map<int, int>::size_type n;
    typedef pair<int, int> Int_Pair;

    for ( int i = 1; i < 5; i++)
    {
        hm1.insert(Int_Pair (i, i));
        hm2.insert(Int_Pair (i, i*i));
        hm3.insert(Int_Pair (i, i-1));
    }

    // The 1st member function removes an element at a given position
    Iter1 = ++hm1.begin();
    hm1.erase(Iter1);

    // The 2nd member function removes elements n the range [_First, _Last)
    Iter1 = ++hm2.begin();
    Iter2 = --hm2.end();
    hm2.erase(Iter1, Iter2);

    // The 3rd member function removes elements with a given _Key
    n = hm3.erase(2);

    // The dereferenced iterator can also be used to specify a key
    Iter1 = ++hm3.begin();
    hm3.erase(Iter1);

      成员函数make_value()和value_type()用法:
#include <cliext/hash_map> 

typedef cliext::hash_map<wchar_t, int> Myhash_map; 
int main() 

    Myhash_map c1; 
    c1.insert(Myhash_map::make_value(L'a', 1)); 
    c1.insert(Myhash_map::make_value(L'b', 2)); 
    c1.insert(Myhash_map::make_value(L'c', 3)); 

    // display contents " [a 1] [b 2] [c 3]" 
    for each (Myhash_map::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
    return (0); 
}

   成员函数to_array():
cli::array<hash_map<Key, Mapped>::value_type>^ to_array();