0

i get an "allocating unneeded temporary container" warning when i try to iterate over the values of a specific key in a QMultiMap:

QMultiHash<QString,QString> testMap;
        
        for (auto &&value : testMap.values("Specific Key"))
        {
            
        }

Is there a better way of doin this?

UrbanCee
  • 1
  • 1

1 Answers1

0

Use the snippet below to remove this warning:

QMultiHash<QString,QString> testMap;

const auto values = testMap.values("Specific Key");      
for (auto &&value : values)
{
            
}
Pamputt
  • 173
  • 1
  • 11
  • yeah, i read that too. that seems like a crude workaround though. is there no "inline" version to fix that? – UrbanCee Jul 24 '23 at 17:54
  • in a for loop, the end condition is evaluated at each step. It means a temporary container is assigned at each step when `testMap.values("Specific Key")` is called. Using a const container avoid this temporary assignation. – Pamputt Jul 25 '23 at 09:27