不允许使用矢量<const int>。为什么允许 pair<const int、int>?
vector<const int> is not allowed. Why is pair<const int, int> allowed?
可能的重复项:
为什么 stackvector<const int> v; //not allowed
但是为什么
pair
允许const
第一个对象呢?事实上,这就是map
对象内部的pair
所发生的情况。我错过了什么吗?如能对这一现象作出详细和直观的解释,将不胜感激。
我认为主要原因是因为std::pair
不会重新分配对象,因此它们不必是可分配的。
更新:
实际上,向量是唯一需要可分配对象的容器。这是因为相应地,标准向量必须为其元素具有连续的存储位置。因此,如果没有空间添加更多对象,vector 将不得不将其数据重新分配到另一个地方(从而使用对象的可分配属性)。
> std::pair
只需要在尝试分配给它时可以分配它的内容。但是,std::vector
始终需要分配才能重新分配。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 呼叫运营商<<临时
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 运营商&lt;&lt;无法访问班级的私人int
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串