在地图和恒常性中插入一对
Inserting a pair into the map and constness?
在下面的样本中,是否有任何正当的理由为什么人们更喜欢一对而不是另一对而不是第三对而不是第四对?
map<const int, int> test;
test.insert(const pair<const int, int>(3, 9));
test.insert(const pair<int, int>(3, 9));
test.insert(pair<int, int>(3, 9));
test.insert(pair<const int, int>(5, 9));
基本上是的。哑编译器将在此代码中调用一个额外的转换构造函数
test.insert(pair<const int, int>(5, 9));
但是优化编译器将使其与其他替代方案相同。
我只问,谁会想写这样的代码?通常,我会写test.insert(std::make_pair(3, 9))
,不会在乎。
最后 2 对插入地图时没有区别。map中的键默认是常量,只需使用make_pair函数即可
相关文章:
- 预处理器:插入结构名称中的前一个行号
- 如何将结构插入到集合中并打印集合的成员
- push_back并插入 C++ STL 中哪个更快?
- 将包含不可复制对象的对插入到映射中
- const_cast静态数组以添加恒常性
- 自动模板参数、数据成员和恒常性
- 有没有办法检测自身内部的类恒常性?
- SqLite c++,可以创建数据库,但不能将 anthing 插入到表中
- 为什么const_cast删除指针的恒常性,而不删除指向常量的指针的恒常性?
- 如何将整数向量插入到键中,标准::映射的值
- 对指针和恒常性兼容性的引用
- 自定义容器中的括号运算符中的恒常性
- 如何忽略函数模板中的恒常性?
- 比较运算符 == 中的共享指针恒常性
- 删除模板化函数中的恒常性
- 自定义字符串实现中 c_str() 函数中的恒常性问题
- 使用C++中的模板函数确定恒常性
- C++11:完美转发中的恒常性
- 在地图和恒常性中插入一对
- 从"this"中抛弃恒常性,然后更改成员值是否会调用未定义的行为?