tr1::unordered_map demo

#include <tr1/unordered_map>
#include <string>
#include <iostream>
using namespace std;

/*
 * -std=c++0x
 */
int main(int argc, char** argv) {

    typedef tr1::unordered_map<std::string,std::string> HashStringType;
    HashStringType hashstring;
#if 1
    hashstring["hello"] = "world0";
    hashstring["hello1"] = "world1";
    hashstring["hello2"] = "world2";
    hashstring["hello3"] = "world3";
#endif  
#if 0  // not successful
    hashstring = {
        {"a123","a3243"},
        {"ad","a231" },
        {"as232","s231" },
    };
#endif
    // insert
   hashstring.insert(make_pair<string,string>("123","2323"));
   hashstring.insert(make_pair<string,string>("1a1223","2323"));
   typedef tr1::unordered_map<std::string,std::string>::iterator iterator_type;
   for( iterator_type it = hashstring.begin();it!= hashstring.end();++it)
   {
         std::cout<<it->first<<":"<<it->second.c_str()<<std::endl;
   }
   
   // hasher
  HashStringType::hasher fn = hashstring.hash_function();
  std::cout<<"hash(\"12342323\") = "<<fn("12342323")<<endl;
    
  // at  ---can't find 'at'
  //  hashstring.at("444444") = "555555";
 
  iterator_type it1 = hashstring.find("hello");
  if(it1!= hashstring.end())
  {
      cout<<"has found"<<endl;
  }
    
   
    return 0;
}

你可能感兴趣的:(tr1::unordered_map demo)