stl中hash_map的使用

gisupc 2012-01-12 01:31:44
我采用如下方式定义一个结构体
struct POINT{
double x;
double y;
};
#include<hash_map>
using namespace stdext;

hash_map<int ,POINT> PtHash;
然后想通过hash_map的find函数查找当前点POINT是否与hashmap中的相等,该如何做呢?求指点,谢谢!
...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
youngwolf 2012-01-12
  • 打赏
  • 举报
回复
既然是map,当然要使用其高速查找的特性,如果你通过遍历去查找,当然也是能查找的,此时map容器就退化为vector了,甚至比vector效率还差。

每个容器都有自己的优点缺点,如果你非要用其弱点,那就本末倒置了。
gisupc 2012-01-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yang79tao 的回复:]

你的key是int,却要找一个POINT,设计有问题,非要找的话,只能自己遍历来找。
map的key才是用来查找的。
[/Quote]
你的意思是将POINT 设置为key才能进行查找是吧!
buyong 2012-01-12
  • 打赏
  • 举报
回复
用hash_map::iterator遍历比较
youngwolf 2012-01-12
  • 打赏
  • 举报
回复
你的key是int,却要找一个POINT,设计有问题,非要找的话,只能自己遍历来找。
map的key才是用来查找的。
Eleven 2012-01-12
  • 打赏
  • 举报
回复
// 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;
}

Copy Code
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.

MSDN上的例子代码,可以参考一下~
gisupc 2012-01-12
  • 打赏
  • 举报
回复
可以参考的例子是http://msdn.microsoft.com/en-us/library/8zz3703d(v=VS.80).aspx

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧