• 9484阅读
  • 11回复

qhash qmap default -value 怎么回事 [复制链接]

上一主题 下一主题
离线water_wf
 

只看楼主 倒序阅读 楼主  发表于: 2010-11-24
   Normal  0        7.8 pt  0  2    false  false  false    EN-US  ZH-CN  X-NONE                                                      MicrosoftInternetExplorer4                                                                                                                                                                                                                                                                                                                         T & QHash::operator[] ( const Key & key )

Returns the value associated with the key as amodifiable reference.
If the hash contains no item with the key, thefunction inserts a default-constructedvalue into the hash with the key, and returns a reference to it. Ifthe hash contains multiple items with the key, this function returns areference to the most recently inserted value.
See also insert() and value
这个default-constructedvalue 啥意思,比如QHash<QString,QColor>怎么设置default value?
离线dbzhang800

只看该作者 1楼 发表于: 2010-11-24
没什么可设置的,QColor 的 default-constructedvalue就是 QColor()
离线water_wf

只看该作者 2楼 发表于: 2010-11-24
谢谢回复,那我像设一个QColor(0,0,100);怎么办
离线dbzhang800

只看该作者 3楼 发表于: 2010-11-24
引用第2楼water_wf于2010-11-24 11:47发表的  :
谢谢回复,那我像设一个QColor(0,0,100);怎么办

那就说明你找错函数了

QHash::value ( const Key & key, const T & defaultValue )

题外,多看Manual
离线water_wf

只看该作者 4楼 发表于: 2010-11-24
你再看看说明[]操作符返回的是引用, value返回的copy构造的。
Returns the value associated with the key as amodifiable reference.
If the hash contains no item with the key, thefunction inserts a default-constructedvalue into the hash with the key, and returns a reference to it.
离线water_wf

只看该作者 5楼 发表于: 2010-11-24
std::map 似乎没有返回引用的接口
离线dbzhang800

只看该作者 6楼 发表于: 2010-11-24
引用第4楼water_wf于2010-11-24 12:31发表的  :
你再看看说明[]操作符返回的是引用, value返回的copy构造的。
Returns the value associated with the key as amodifiable reference.
If the hash contains no item with the key, thefunction inserts a default-constructedvalue into the hash with the key, and returns a reference to it.  

这不是重点,重点的你的需求是什么?你要做什么
离线water_wf

只看该作者 7楼 发表于: 2010-11-24
看回帖,已经明白了,defaultconstructed value应该不可手工设置的,只能用default 构造函数来构造。
离线water_wf

只看该作者 8楼 发表于: 2010-11-24
这个接口蛮好用的,大量节约时间,特别是pixmap时候,不用多次copy
离线water_wf

只看该作者 9楼 发表于: 2010-11-24
说明一点,T 必须有一个不带任何参数构造函数。
离线dbzhang800

只看该作者 10楼 发表于: 2010-11-24
引用第8楼water_wf于2010-11-24 12:45发表的  :
这个接口蛮好用的,大量节约时间,特别是pixmap时候,不用多次copy

其实 QPixmap 这些东西,倒不怕反复 copy,因为使用的是 Implicit Sharing
离线water_wf

只看该作者 11楼 发表于: 2010-11-24
哦,那怪不得。
快速回复
限100 字节
 
上一个 下一个