Expensive use of map's bracket operator to insert or assign a value
The bracket operator of std::map
or
std::unordered_map
is used for inserting or assigning a value in the
container instead of the insert_or_assign()
method, which is more
efficient
Since R2022b
Description
This defect occurs when you use the []
operators to insert or assign a
value to a key in a std::map
or std::unordered_map
instead of using the insert_or_assign()
method. For
instance:
std::map<int, std::string> Data; Data[55] = "Fifty five"; //defect
[]
operator to insert a key value pair
in the map data
. This checker applies to code that uses C++17 or
later.Risk
When you insert or assign a value in a std::map
or
std::unordered_map
by using the []
operator, the
compiler calls the default constructor of pair
to constructs a key-value
pair
. The method insert_or_assign()
inserts or
assigns a value without calling the constructor, which makes it more efficient. Using the
less efficient []
operator makes your code inefficient.
Fix
When inserting or assigning a value in maps, use their
insert_or_assign()
method. For
instance:
std::map<int, std::string> Data; Data.insert_or_assign(55,"Fifty five");
insert_or_assign()
method inserts the key-value pair without having to
construct a pair
first, making it more efficient.Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
EXPENSIVE_MAP_INSERT_OR_ASSIGN
|
Impact: Low |
Version History
Introduced in R2022b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)