简体   繁体   English

在`QMap中搜索的有效方法<qstring, ...> ` 使用 `QStringView`</qstring,>

[英]efficient way to search in `QMap<QString, ...>` using `QStringView`

I have a QVariantMap , which is an alias for QMap<QString, QVariant> .我有一个QVariantMap ,它是QMap<QString, QVariant>的别名。 And I have a function which takes QStringView key as argument.我有一个 function ,它将QStringView key作为参数。 I want to search a value in the map by the key.我想通过键在 map 中搜索一个值。

QVariantMap map;
QStringView key;
QVariant v = map.value(key);

But the third line does not compile.但是第三行没有编译。 Of course I could convert the string view to QString but this means one extra memory allocation.当然,我可以将字符串视图转换为QString ,但这意味着需要额外分配一个 memory。 Is there any way around this which avoids the unnecessary allocation?有什么办法可以避免不必要的分配? How to lookup the value by just the string view?如何仅通过字符串视图查找值?

there is no other solution,没有其他解决方案,

  1. change the keys type to QStringView or将键类型更改为 QStringView 或
  2. QVariant v = map.value(key.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM