c++ - 这里为什么使用引用接收返回值
巴扎黑
巴扎黑 2017-04-17 14:51:17
0
1
365

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)
伊谢尔伦

First of all, I would like to emphasize that hash_map is not part of the C++ standard library, and there is no universal standard. What I said below are all based on C++11's unordered_map.
Overloading the unordered_map operator in [] is defined as

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

As you can see, what is returned here is a reference, so there is no problem in accepting it by reference.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template