c++ hash_map用法总结
2016-06-19 20:54
519 查看
c++ STL库里有自定义的hash_map 方法,但是使用起来并不是那么方便
hash_map主要的方法有
find(),insert()
我结合官方API说明一下他们的用法
一、需要特别注意的地方,
1.头文件的引用
2.如何插入一个<key,value>键值对(参考一下代码)
3.find()的返回值
4.如何获取某一个key值相应的value值
分别用。first,和,second,指代key和value的值
二、hash_map使用string和long long做key的问题
当hash_map中使用string为key时,需用户扩展命名空间,否则报错如下:
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/ext/hashtable.h:518: error: no match for call to `(const __gnu_cxx::hash<std::string>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
表明编译器错误
解决方法:
#include <ext/hash_map>
namespace __gnu_cxx
{
template<> struct hash< std::string >
{
size_t operator()( const std::string& x ) const
{
return hash< const char* >()( x.c_str() );
}
};
template<> struct hash<long long>
{
size_t operator()(long long x) const
{
return x;
}
};
}
hash_map主要的方法有
find(),insert()
我结合官方API说明一下他们的用法
一、需要特别注意的地方,
1.头文件的引用
2.如何插入一个<key,value>键值对(参考一下代码)
3.find()的返回值
4.如何获取某一个key值相应的value值
hm1_RcIter -> second
分别用。first,和,second,指代key和value的值
// hash_map_find.cpp
// compile with: /EHsc
#define _DEFINE_DEPRECATED_HASH_CLASSES 0
#include <hash_map>
#include <iostream>
int main( )
{
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_AcIter, hm1_RcIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_RcIter = hm1.find( 2 );
cout << "The element of hash_map hm1 with a key of 2 is: "
<<hm1_RcIter -> second << "." << endl;
// If no match is found for the key, end( ) is returned
hm1_RcIter = hm1.find( 4 );
if ( hm1_RcIter == hm1.end( ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key of 4." << endl;
else
cout << "The element of hash_map hm1 with a key of 4 is: "
<<hm1_RcIter -> second << "." << endl;
// The element at a specific location in the hash_map can be found
// using a dereferenced iterator addressing the location
hm1_AcIter = hm1.end( );
hm1_AcIter--;
hm1_RcIter = hm1.find( hm1_AcIter -> first );
cout << "The element of hm1 with a key matching "
<< "that of the last element is: "
<<hm1_RcIter -> second << "." << endl;
}
Output
The element of hash_map hm1 with a key of 2 is: 20. The hash_map hm1 doesn't have an element with a key of 4. The element of hm1 with a key matching that of the last element is: 30.
二、hash_map使用string和long long做key的问题
当hash_map中使用string为key时,需用户扩展命名空间,否则报错如下:
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/ext/hashtable.h:518: error: no match for call to `(const __gnu_cxx::hash<std::string>) (const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
表明编译器错误
解决方法:
#include <ext/hash_map>
namespace __gnu_cxx
{
template<> struct hash< std::string >
{
size_t operator()( const std::string& x ) const
{
return hash< const char* >()( x.c_str() );
}
};
template<> struct hash<long long>
{
size_t operator()(long long x) const
{
return x;
}
};
}
相关文章推荐
- C++中函数指针的使用
- C++中函数指针的使用
- 冒泡排序(c语言实现)
- C++指针
- error C2144: 语法错误:“int”的前面应有“;”
- C语言中getchar()和putchar()的实现细节
- I00029 C语言程序-打印九九乘法表
- C语言函数传参汇总
- CSv纯C++解析
- 【C】C语言输入输出缓冲区
- C语言的标准输入输出
- 指针学习路线
- c++DLL编程详解
- C++Primer笔记(一):C++基本数据类型及其取值范围
- 第16周项目2--用文件保存学生的名单
- C++ STL map的使用
- 这是我开通博客的原因;虽然不是什么大问题;关于指针与对象(C++)
- C++中结构体与类的区别
- 基于C的2048游戏
- 为什么在C++中需要虚函数