这里为什么使用引用接收返回值
大家讲道理
大家讲道理 2016-11-11 13:47:16
0
1
1074

hash_map<string, list<uint32_t> > g_hmLimits;
...
list<uint32_t>& lsErrorTime = g_hmLimits[strDomain];    
lsErrorTime.clear();
为什么返回值是:list<uint32_t>& 类型呢?
hash_map中的value存储的是一个list对象,并不是对象的指针,这个时候返回的Value是原来Value的副本吗?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
代言

先强调一点,hash_map不是C++标准库的东西,没有通行的标准,一下所说都是基于C++11的unordered_map而言的。
在unordered_map中重载[]操作符的定义为

mapped_type& operator[] ( const key_type& k );
mapped_type& operator[] ( key_type&& k );

可以看到,这里返回的就是引用,所以用引用来接受是没有问题的。


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!