qt的 QMultiHash value
时间: 2024-05-12 19:20:44 浏览: 14
QMultiHash的value函数返回指定键的值列表。如果键不存在,则返回空列表。如果一个键对应多个值,则返回列表包含所有值。函数定义如下:
```cpp
QList<T> QMultiHash<Key, T>::value(const Key &key) const
```
其中,`Key`是键的数据类型,`T`是值的数据类型,`key`是要查询的键。
示例:
```cpp
QMultiHash<QString, int> multiHash;
multiHash.insert("apple", 2);
multiHash.insert("banana", 3);
multiHash.insert("apple", 4);
QList<int> appleValues = multiHash.value("apple"); // 返回 [2, 4]
QList<int> orangeValues = multiHash.value("orange"); // 返回空列表
```
上面的示例中,我们创建了一个QMultiHash对象`multiHash`,并向其中插入了3个键值对。其中,键为"apple"的有2个值,键为"banana"的有1个值。我们通过调用`value`函数来查询键"apple"和"orange"对应的值列表,并将结果保存在`appleValues`和`orangeValues`中。其中,`appleValues`的值为[2, 4],因为键"apple"对应了2个值,而`orangeValues`的值为空列表,因为键"orange"不存在于该QMultiHash对象中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)