C++ map下标操作[]和insert区别

您所在的位置:网站首页 insert和append的效率 C++ map下标操作[]和insert区别

C++ map下标操作[]和insert区别

2024-07-14 04:16| 来源: 网络整理| 查看: 265

在构建map时候,我们是使用insert和[]有什么区别呢? 哪个更好呢? 哪个效率更高呢? 哪个更安全呢?

首先需要明确的是: map中不允许存在相同的key

Because map containers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value, if so, the element is not inserted and its mapped value is not changed in any way.

直接分析代码:

#include #include #include using namespace std; class Sample { static int _noOfObjects; int _objectNo; public: Sample() : _objectNo(_noOfObjects++) { std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3