我不能使用hash_map :: operator []来读取哈希映射中的值? [英] I cannot use the hash_map::operator[] to read the value in the hash map?

查看:76
本文介绍了我不能使用hash_map :: operator []来读取哈希映射中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>来自MSDN关于哈希映射的引用:


operator []将元素插入到具有指定

键值的hash_map中。 br />

给出了这样的例子:


hash_map< int,inthm1;

hm1 [2] = 40 ;


但是,没有如下例子:


int a = hm1 [2];

上面的行是否正确?

如果没有,如何实现函数读取对应的值

存储在哈希映射中的键2 ?


我正在寻找的就像Java的HashMap中的get()方法。

>From the reference of MSDN about hash map:

operator[] Inserts an element into a hash_map with a specified
key value.

And such example is given:

hash_map <int, inthm1;
hm1[ 2 ] = 40;

However, there is no example like the following:

int a = hm1[2];

Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?

What I am looking for is just like the get() method in HashMap of Java.

推荐答案

xz写道:
xz wrote:

>>来自MSDN关于哈希映射的引用:
>>From the reference of MSDN about hash map:



operator []将一个元素插入到一个带有指定

键值的hash_map中。


这样的前给出了足够的内容:


hash_map< int,inthm1;

hm1 [2] = 40;


operator[] Inserts an element into a hash_map with a specified
key value.

And such example is given:

hash_map <int, inthm1;
hm1[ 2 ] = 40;



like km1.put(2,40);在Java

like km1.put(2, 40); in Java


>

但是,没有像下面这样的例子:


int a = hm1 [2];
>
However, there is no example like the following:

int a = hm1[2];



like int a =(int)hm1.get(2);在Java

like int a = (int) hm1.get(2); in Java


>

上面的行是否正确?

如果没有,请如何实施读取存储在哈希映射中的键2对应值的函数




我正在寻找的就像get()方法一样Java的HashMap。
>
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?

What I am looking for is just like the get() method in HashMap of Java.


8月1日晚上10点58分,Barry< dh ... @ 126.comwrote:
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:

xz写道:
xz wrote:

>来自MSDN关于哈希映射的引用:
>From the reference of MSDN about hash map:


operator []将元素插入到具有指定

键值的hash_map中。
operator[] Inserts an element into a hash_map with a specified
key value.


给出了这样的例子:
And such example is given:


hash_map< int ,inthm1;

hm1 [2] = 40;
hash_map <int, inthm1;
hm1[ 2 ] = 40;



喜欢km1.put(2,40);在Java中


like km1.put(2, 40); in Java


但是,没有如下例子:
However, there is no example like the following:


int a = hm1 [2];
int a = hm1[2];



如int a =(int)hm1.get(2);在Java中


like int a = (int) hm1.get(2); in Java


上面的行是否正确?

如果没有,如何实现该函数来读取相应的值

存储在哈希映射中的密钥2?
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?


我正在寻找的就像Java的HashMap中的get()方法。
What I am looking for is just like the get() method in HashMap of Java.



谢谢。


我应该包含< hash_mapor< hash_map.h?


我认为它应该是

#include< hash_map>


但事实证明:

错误:hash_map:没有这样的文件或目录

Thanks.

And should I include <hash_mapor <hash_map.h?

I thought it should be
#include<hash_map>

but turns out:

error: hash_map: No such file or directory


8月1日晚上10点58分,Barry< dh ... @ 126.comwrote:
On Aug 1, 10:58 pm, Barry <dh...@126.comwrote:

xz写道:
xz wrote:

>来自MSDN的参考关于哈希映射:
>From the reference of MSDN about hash map:


operator []将元素插入到具有指定

键值的hash_map中。
operator[] Inserts an element into a hash_map with a specified
key value.


给出了这样的例子:
And such example is given:


hash_map< int ,inthm1;

hm1 [2] = 40;
hash_map <int, inthm1;
hm1[ 2 ] = 40;



喜欢km1.put(2,40);在Java中


like km1.put(2, 40); in Java


但是,没有如下例子:
However, there is no example like the following:


int a = hm1 [2];
int a = hm1[2];



如int a =(int)hm1.get(2);在Java中


like int a = (int) hm1.get(2); in Java


上面的行是否正确?

如果没有,如何实现该函数来读取相应的值

存储在哈希映射中的密钥2?
Is the line above correct at all?
If not, how to implement the function to read the value corresponding
to key 2 stored in the hash map?


我正在寻找的就像Java的HashMap中的get()方法。
What I am looking for is just like the get() method in HashMap of Java.



错误:''class __gnu_cxx :: hash_map< double,double,

__gnu_cxx :: hash< double>, std :: equal_to< double>,std :: allocator< double>

error: ''class __gnu_cxx::hash_map<double, double,
__gnu_cxx::hash<double>, std::equal_to<double>, std::allocator<double>


>''没有名为''put'的成员'
>'' has no member named ''put''


这篇关于我不能使用hash_map :: operator []来读取哈希映射中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆