C++ //vector 容器构造 vector赋值操作 vector 容量大小 vector插入和删除 //vector数据存取 vector互换容器 vector预留空间
1 //vector 容器构造 vector赋值操作 vector 容量大小 vector插入和删除
2 //vector数据存取 vector互换容器 vector预留空间
3 #include<iostream>
4 #include<string>
5 #include<vector>
6
7 using namespace std;
8
9 //打印
10 void printVector(vector<int> &v)
11 {
12 for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
13 {
14 cout << *it << " ";
15 }
16 cout << endl;
17 }
18 //vector 容器构造
19 void test01()
20 {
21 vector<int>v1; //默认构造 无参构造
22 for (int i = 0; i < 10; i++)
23 {
24 v1.push_back(i);
25 }
26
27 printVector(v1);
28
29 //通过区间的方式进行构造
30
31 vector<int>v2(v1.begin(), v1.end());
32 printVector(v2);
33
34 //N个elem方式构造
35 vector<int>v3(10, 100);
36 printVector(v3);
37
38
39 //拷贝构造
40 vector<int>v4(v3);
41 printVector(v4);
42
43 }
44 //打印
45 void printVector1(vector<int>&v1)
46 {
47 for (vector < int>::iterator it = v1.begin(); it != v1.end();it++)
48 {
49 cout << *it << " ";
50 }
51 cout << endl;
52 }
53
54 //vector 赋值
55 void test02()
56 {
57 vector<int>v1;
58 for (int i = 0; i < 10; i++)
59 {
60 v1.push_back(i);
61 }
62 printVector1(v1);
63 //赋值 operator=
64 vector<int>v2;
65 v2 = v1;
66 printVector1(v2);
67
68 //assign
69 vector<int>v3;
70 v3.assign(v2.begin(), v2.end());
71 printVector(v3);
72
73 //n个elem 方式赋值
74 vector<int>v4;
75 v4.assign(10, 100);
76 printVector(v4);
77
78 }
79
80 //打印
81 void printVector3(vector<int>& v3)
82 {
83 for (vector<int>::iterator it = v3.begin(); it != v3.end(); it++)
84 {
85 cout << *it << " ";
86 }
87 cout << endl;
88 }
89
90 //vector 容量大小
91 void test03()
92 {
93 vector<int>v3;
94 for (int i = 0; i < 10; i++)
95 {
96 v3.push_back(i);
97 }
98 printVector3(v3);
99
100 if (v3.empty()) //为真 代表容器为空
101 {
102 cout << "v3为空!" << endl;
103 }
104 else
105 {
106 cout << "V3不为空!" << endl;
107 cout << "v3的容量为: " << v3.capacity() << endl;
108 cout << "v3的大小为:" << v3.size() << endl;
109
110 }
111 //重新指定大小
112 v3.resize(15,10);//利用重载的版本 可以指定默认填充的值,参数2
113 printVector3(v3); //如果重新指定的过长 默认用0填充新的位置
114
115
116 v3.resize(5);
117 printVector3(v3); //如果重新指定的过短,就会删除超出的部分
118
119 }
120
121
122 //打印
123 void printVector4(vector<int>& v4)
124 {
125 for (vector<int>::iterator it = v4.begin(); it != v4.end(); it++)
126 {
127 cout << *it << " ";
128 }
129 cout << endl;
130 }
131
132 //vector 插入和删除
133 void test04()
134 {
135 vector <int>v4;
136 //尾插
137 v4.push_back(10);
138 v4.push_back(20);
139 v4.push_back(30);
140 v4.push_back(40);
141 v4.push_back(50);
142
143 //遍历
144 printVector4(v4);
145
146 //尾删
147 v4.pop_back();
148 printVector4(v4);
149
150 //插入第一个 第一个参数就是迭代器
151 v4.insert(v4.begin(), 100);
152 printVector4(v4);
153
154 //插入两个 55 在开头
155 v4.insert(v4.begin(), 2, 55);
156 printVector4(v4);
157
158 //删除 开头删除了第一个 参数 迭代器
159 v4.erase(v4.begin());
160 printVector4(v4);
161
162 //删除 区间方式 类似 清空
163 //v4.erase(v4.begin(), v4.end()); // 全部删除
164 //printVector4(v4);
165
166 //清空
167 v4.clear();
168 printVector4(v4);
169 }
170
171
172 //vector数据存取
173 void test05()
174 {
175 vector<int>v5;
176 for (int i = 0; i < 10; i++)
177 {
178 v5.push_back(i);
179 }
180
181 //利用[]的方式访问数组中的元素
182 for (int i = 0; i < v5.size(); i++)
183 {
184 cout << v5[i] << " ";
185 }
186 cout << endl;
187
188 //利用at方式访问元素
189 for (int i = 0; i < v5.size(); i++)
190 {
191 cout << v5.at(i) << " " ;
192 }
193 cout << endl;
194
195 //返回第一个元素
196 cout << "第一个元素为:" << v5.front() << endl;
197
198 //获取最后一个元素
199 cout << "最后一个元素为:" << v5.back() << endl;
200 }
201
202
203 //打印
204 void printVector6(vector<int>& v6)
205 {
206 for (vector<int>::iterator it = v6.begin(); it != v6.end(); it++)
207 {
208 cout << *it << " ";
209 }
210 cout << endl;
211 }
212
213 //vector互换容器
214 //1.基本使用
215 void test06()
216 {
217 vector<int>v6;
218 for (int i = 0; i < 10; i++)
219 {
220 v6.push_back(i);
221 }
222 cout << "交换前打印!!!" << endl;
223 printVector6(v6);
224
225 vector<int>v7;
226
227 for (int i = 10; i > 0; i--)
228 {
229 v7.push_back(i);
230 }
231 printVector6(v7);
232
233
234 cout << "交换后:!!!!!!!!!!" << endl;
235
236 v6.swap(v7);
237 printVector6(v6);
238 printVector6(v7);
239
240 }
241
242 //2.实际用途
243 //巧用swap可以收缩内存空间
244 void test07()
245 {
246 vector<int>v;
247 for (int i = 0; i < 100000; i++)
248 {
249 v.push_back(i);
250 }
251
252 cout << "第一次v的容量为:" << v.capacity() << endl;
253 cout << "第一次v的大小:" << v.size() << endl;
254
255
256 v.resize(3); //重新指定大小
257 cout << "第二次v的容量为:" << v.capacity() << endl;
258 cout << "第二次v的大小:" << v.size() << endl;
259
260 //巧用swap收缩内存 // vector<int>(v) 匿名对象执行完就被系统回收
261 vector<int>(v).swap(v);
262
263 cout << "第三次v的容量为:" << v.capacity() << endl;
264 cout << "第三次v的大小:" << v.size() << endl;
265 }
266
267
268 //预留空间 reserve(int len);
269 void test08()
270 {
271 vector<int>v8;
272 //利用reserve预留空间
273 v8.reserve(100000);
274
275 int num = 0; //统计开辟次数
276 int* p = NULL;
277 for (int i = 0; i < 10000; i++)
278 {
279 v8.push_back(i);
280
281 if (p != &v8[0])
282 {
283 p = &v8[0];
284 num++;
285 }
286 }
287 cout << "num = " << num << endl;
288 }
289
290
291 int main()
292 {
293 test01();
294
295 test02();
296
297 test03();
298
299 test04();
300
301 test05();
302
303
304 test06();
305 test07();
306
307 test08();
308
309 system("pause");
310 return 0;
311 }

C++ //vector 容器构造 vector赋值操作 vector 容量大小 vector插入和删除 //vector数据存取 vector互换容器 vector预留空间的更多相关文章
- 重点:QObject 的拷贝构造和赋值操作——私有
QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承 ...
- QObject 的拷贝构造和赋值操作
QOject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承自 ...
- kubernetes给容器生命周期设置操作事件
Kubernetes支持预启动和预结束事件. Kubernetes在容器启动的时候发送预启动事件,在容器结束的时候发送预结束事件. 定义预启动和预结束事件操作 下面是Pod的配置文件: # cat l ...
- C++标准库vector类型的使用和操作总结
vector是一种类型对象的集合,它是一种顺序容器,容器中的所有对象必须都是同一种类型.想了解顺序容器的更多内容:C++顺序容器知识总结.vector的对象是可以动态生长的,这说明它在初始化时可以不用 ...
- vector 与map的下标操作
1.vector的下标操作不会添加元素,只能针对已经存在的元素操作. 2.map的下标操作具有副作用,key不存在,会在map中添加一个具有该key的新元素,新元素的value使用默认构造方法. 3. ...
- 实战c++中的vector系列--构造、operator=和assign差别
vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...
- R: vector 向量的创建、操作等。
################################################### 问题:创建.操作向量 18.4.27 怎么创建向量 vector,,及其相关操作 ??? 解 ...
- STL——容器(Map & multimap)的拷贝构造与赋值
1. Map & multimap 的拷贝构造与赋值 map(const map &mp); //拷贝构造函数 map& operator=(con ...
- STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值
1. 默认构造 set<int> setInt; //一个存放int的set容器. set<float> setFloat; //一 ...
- Effective C++笔记:构造/析构/赋值运算
条款05:了解C++默默编写并调用哪些函数 默认构造函数.拷贝构造函数.拷贝赋值函数.析构函数构成了一个类的脊梁,只有良好的处理这些函数的定义才能保证类的设计良好性. 当我们没有人为的定义上面的几个函 ...
随机推荐
- RabbitMQ原理详解
RabbitMQ:我们通常谈到消息队列,就会联想到这其中的三者:生产者.消费者和消息队列,生产者将消息发送到消息队列,消费者从消息队列中获取消息进行处理.对于RabbitMQ,它在此基础上做了一层抽象 ...
- 我手写了一个RPC框架。成功帮助读者斩获字节、阿里等大厂offer。
本着开源精神,本项目README已经同步了英文版本.另外,项目的源代码的注释大部分也修改为了英文. 如访问速度不佳,可放在 Gitee 地址:https://gitee.com/SnailClimb/ ...
- SqlSugar分组查询
一.分组查询和使用 1.1 语法 只有在聚合对象需要筛选的时候才会用到Having,一般分组查询用不到可以去掉 var list = db.Queryable<Student>() ...
- 【深度学习项目二】卷积神经网络LeNet实现minst数字识别
相关文章: [深度学习项目一]全连接神经网络实现mnist数字识别 [深度学习项目二]卷积神经网络LeNet实现minst数字识别 [深度学习项目三]ResNet50多分类任务[十二生肖分类] 『深度 ...
- Python Requests 实现简单网络请求
Python 是一种跨平台的计算机程序设计语言,面向对象动态类型语言,Python是纯粹的自由软件,源代码和解释器CPython遵循 GPL(GNU General Public License)协议 ...
- Gin 响应方式
响应 1. 字符串方式 r.GET("/user/save", func(ctx *gin.Context) { ctx.String(http.StatusOK, "t ...
- Windows安装MongoDB6.0.2
环境 Windows 10 MongoDB 6.0.2 配置 下载mongodb 下载地址:https://www.mongodb.com/try/download/community 安装 指定目录 ...
- vue-cropper 移动端上传图片压缩裁剪
头像裁剪压缩上传流程: 点击头像--判断是否为IOS端--若是--A,否则--BA:选择图片 --CB:弹框供用户选择从相册选择或者调用相机拍照--选择图片--CC:出现cropper裁剪框,裁剪框位 ...
- JVM metaspace思维导图整理
JDK8中用元空间metaspace代替了永久代perm,原因和其特性简单介绍一下. 思维导图 图中gc log详解链接:https://www.jianshu.com/p/cd34d6f3b5b4 ...
- 【JS】强化Promise理解,从零手写属于自己的Promise.all与Promise.race
壹 ❀ 引 在一个思路搞定三道Promise并发编程题,手摸手教你实现一个Promise限制器一文中,我们在文章结尾留了一个疑问,关于第三题的实现能否解决当每次调用时间都不相等的情况(比如第二次调用要 ...