// 2018/04/18 12:13:12
// vector-reserve/*size是当前vector容器真实占用的大小,也就是容器当前拥有多少个容器。capacity是指在发生realloc前能允许的最大元素数,即预分配的内存空间。当然,这两个属性分别对应两个方法:resize()和reserve()。使用resize(),容器内的对象内存空间是真正存在的。使用reserve()仅仅只是修改了capacity的值,容器内的对象并没有真实的内存空间(空间是"野"的)。此时切记使用[]操作符访问容器内的对象,很可能出现数组越界的问题。 */
#include <iostream>
#include <vector>using namespace std;int main(){vector<int> v(5, 0); // 5 elements each-value 0/*- - - - - - - - - - - - */cout << "Size of v = " << v.size() << endl;cout << "Capacity v = " << v.capacity() << endl;cout << "Value of each elements is ";for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;v[0] = 5; // new value for first elementv[1] = 8;v.push_back(3); // creates new (6th) elements of vectorv.push_back(7); // automatically increases sizecout << endl; // capacity of vector vcout << "Size of v = " << v.size() << endl;cout << "Capacity v = " << v.capacity() << endl;cout << "Value of each elements is ";for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;v.reserve(100); // increase capacity to 100cout << "Size of v1_int = " << v.size() << endl;cout << "Capacity v1_int = " << v.capacity() << endl;int size = sizeof(v); // how big is vector itselfcout << "sizeof v =" << size << endl;return 0;
}/* OUTPUT:Size of v = 5Capacity v = 5Value of each elements is 0 0 0 0 0Size of v = 7Capacity v = 7Value of each elements is 5 8 0 0 0 3 7Size of v1_int = 7Capacity v1_int = 100sizeof v =16 */
详细解决方案
vector-reserve
热度:25 发布时间:2023-12-15 04:30:07.0
相关解决方案
- 刚开始搭建SSH,使用hibernate reserve engineering时遇到了有关问题
- 关于 Vector 求解解决思路
- Vector.clear 的有关问题
- vector push_back出错(release版本),该如何处理
- vector 对象数组删除有关问题
- 一个关于"符号: 类 Vector"的问题
- 请教 Vector vetinon= new Vector();这个语句是什么意思啊?
- Vector vec=null
- ArrayList,Vector, LinkedList的储存性能和特性
- Openlayers学习札记――Vector Layer Style
- Openlayers学习札记――Vector Layer 事件类型
- Openlayers学习札记――Vector Layer Rules和Filter
- Openlayers学习札记――Vector Layer类
- java jvm 异常 Could not reserve enough space for object heap
- Vector<Message> messages; 有错吗?该如何解决
- Set,List , Map , Vector,ArrayList存储数据的应用,该怎么解决
- Vector、Connection、Statement、abstractTableModel,该如何处理
- Vector 的有关问题
- java vector iterator 正则匹配中的有关问题
- eclipse直接启动的时候提示异常:Could not reserve enough space for object heap
- java Vector 的用法解决思路
- Vector is a raw type. References to generic type Vector<E> should be parameteriz,该怎么处理
- ArrayList Vector,该如何处理
- vector hashtable过时?该如何处理
- 生手学习java中关于ArrayList,LinkedList,HashSet,Vector,TreeSet的区别和使用
- 小弟我的代码有什么东东,为什么报java.lang.String cannot be cast to java.util.Vector
- vector 实现解决方案
- 一个简单的有关问题:vector
- Vector 的排序有关问题(高手来解答)
- List list1 = new ArrayList()跟List list2 = new Vector()的疑问