STL9-vector容器

vector容器 动态数组 可变数组

vector容器 单口容器

vector实现动态增长:
     当插入新元素时,如果空间不足,那么vector会重新申请更大内存空间(默认二倍),将原空间数据拷贝到新空间,释放旧空间的数据,再把新元素插入新申请空间。

当我们知道我们存储的元素大概有多少时,使用reserve方法,减少vector重新申请内存-拷贝数据-释放旧空间的次数

#include<iostream>
#include<vector>
using namespace std;
void test01() {vector<int> v;int* p=NULL;int count = 0;  //统计vector容量增长次数for (int i = 0; i < 100000; i++){v.push_back(i);if (p != &v[0]) {p = &v[0];  //内存首地址发生变化,申请新内存count++;}}cout << count << endl;  
}
void test02() {vector<int> v;v.reserve(100000);int* p=NULL;int count = 0;  //统计vector容量增长次数for (int i = 0; i < 100000; i++){v.push_back(i);if (p != &v[0]) {p = &v[0];count++;}}cout << count << endl;
}
int main() {test01();test02();
}

输出:

#include<iostream>
#include<vector>
using namespace std;void PrintVector(vector<int>& v) {for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}//初始化
void test01() {vector<int> v1;  //默认构造int arr[] = { 10,20,30,40 };vector<int> v2(arr, arr + sizeof(arr) / sizeof(int));vector<int> v3(v2.begin(), v2.end());vector<int> v4(v3);PrintVector(v2);PrintVector(v3);PrintVector(v4);
}
/*
输出:
10 20 30 40
10 20 30 40
10 20 30 40
*///赋值操作
void test02()
{int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));//成员方法vector<int> v2;v2.assign(v1.begin(), v1.end());//重载=号vector<int> v3;v3 = v2;cout << "v1" << endl;PrintVector(v1);cout << endl<<"-------" << endl;int arr1[] = { 100,200,300,400 };vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));v4.swap(v1);  //将指针指向进行交换PrintVector(v1);PrintVector(v2);PrintVector(v3);PrintVector(v4);
}
/*
输出:
v1
10 20 30 40-------
100 200 300 400
10 20 30 40
10 20 30 40
10 20 30 40
*///大小操作
void test03() {int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));cout << "size:" << v1.size() << endl;if (!v1.empty()) {cout << "v1 is not empty" << endl;}PrintVector(v1);v1.resize(2);PrintVector(v1);v1.resize(6);PrintVector(v1);v1.resize(8, 1);PrintVector(v1);for (int i = 0; i < 10000; i++){v1.push_back(i);}cout << "size:" << v1.size() << endl;cout << "capacity:"<<v1.capacity() << endl;  //容量
}
/*
输出:
size:4
v1 is not empty
10 20 30 40
10 20
10 20 0 0 0 0
10 20 0 0 0 0 1 1
size:10008
capacity:12138
*///存储数据
void test04() {int arr[] = { 10,20,30,40 };vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));for (int i = 0; i < v1.size(); i++){cout << v1[i] << endl;  //不抛异常}for (int i = 0; i < v1.size(); i++){cout << v1.at(i) << endl;  //抛异常}//front第一个元素 back最后一个元素cout << "第一个元素front:" << v1.front() << endl;cout << "最后一个元素back:" << v1.back() << endl;
}
/*
输出:
10
20
30
40
10
20
30
40
第一个元素front:10
最后一个元素back:40
*///插入和删除
void test05() {vector<int> v;v.push_back(10);v.push_back(20);v.insert(v.begin(), 30);v.insert(v.end(), 50);v.insert(v.begin() + 2, 1000);//vector容器支持随机访问//支持数组下标访问,支持随机访问//迭代器可以直接+2 +3 -2等操作PrintVector(v);v.erase(v.begin());PrintVector(v);v.erase(v.begin() + 1, v.end());PrintVector(v);v.clear();cout << "size:" << v.size() << endl;}
/*
输出:
30 10 1000 20 50
10 1000 20 50
10
size:0
*/
//巧用swap缩减空间
void test06() {//vector 添加元素 自动增长 删除元素 不会自动减少vector<int> v;for (int i = 0; i < 10000; i++) {v.push_back(i);}cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl;  //容量v.resize(10);cout << "=----------------" << endl;cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl;  //容量//收缩空间// 1 vector<int>为匿名对象,// 2 vector<int>(v)用v去初始化匿名对象,会根据他自己大小去初始化自己// 3 vector<int>(v).swap(v) v与匿名对象交换内存指向,将匿名对象销毁vector<int>(v).swap(v);cout << "=----------------" << endl;cout << "size:" << v.size() << endl;cout << "capacity:" << v.capacity() << endl;  //容量}
/*
输出:
size:10000
capacity:12138
=----------------
size:10
capacity:12138
=----------------
size:10
capacity:10
*/
int main()
{cout << "-------------test01--------------" << endl;test01();cout << "-------------test02--------------" << endl;test02();cout << "-------------test03--------------" << endl;test03();cout << "-------------test04--------------" << endl;test04();cout << "-------------test05--------------" << endl;test05();cout << "-------------test06--------------" << endl;test06();
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/567950.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

函数返回值失效

#include<stdio.h> #include<stdlib.h> #include<string.h> #if 1 char* getMen2() {char buf[64]; //临时变量&#xff0c;栈区存放strcpy(buf, "abccddeeff");printf("buf:%s\n", buf);return buf; //此处并不是把内存块64个字节ret…

mysql突然出现慢sql_Mysql开启慢SQL并分析原因

第一步.开启mysql慢查询方式一:修改配置文件Windows&#xff1a;Windows 的配置文件为 my.ini&#xff0c;一般在 MySQL 的安装目录下或者 c:\Windows 下。Linux&#xff1a;Linux 的配置文件为 my.cnf &#xff0c;一般在 /etc 下在 my.ini 增加几行:[mysqlld]long_query_time…

STL10-deque容器

deque 双端队列 deque 删除操作 deque案例&#xff1a; #if 1 #include<iostream> #include<deque> using namespace std; void PrintDeque(deque<int>& d) {for (deque<int>::iterator it d.begin(); it ! d.end(); it) {cout << *it <…

php mysql 平均分_平均评级计算mysql php

我想计算每个用户的平均评分 .我有三张 table .表评级id |order_id |rate------------------1 | 52 |82 | 51 |43 | 52 |24 | 51 |7表顺序这里做的是驱动程序表IDid |did------------------52 | 551 | 735 | 644 | 8表驱动程序id |name------------------5 | test17 | test28 |…

Leedcode4-sort listnode 归并排序

#include<iostream> using namespace std;//Sort a linked list in O(n log n) time using constant space complexity.//Definition for singly-linked list. //归并排序 #if 0 struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}}; str…

vue 字典配置_vue遍历数据及字典的方法

数组&#xff1a;数值{{ item.message }}数组&#xff1a;数值加索引{{ item.msg }}{{index}}字典&#xff1a;key value index{{ value }} {{key}} {{index}}new Vue({el: #repeat,data: {object: {FirstName: John,LastName: Doe,Age: 30}}})vue遍历数组和对象的方法以及他们…

tensorflow代码中tf.app.run()什么意思

# 前面的代码省略了... 。。。 。。。 。。。 def main(argvNone):mnist input_data.read_data_sets("F:\mydata\TensorFlowData\MNIST_data", one_hotTrue)train(mnist)if __name__ __main__:tf.app.run()那么 tf.app.run()什么意思呢 &#xff1f;可以猜到&#…

java jdbc dao_Java自学-JDBC DAO

基于JDBC设计DAO的实例DAODataAccess Object数据访问对象实际上就是运用了ORM中的思路&#xff0c;把数据库相关的操作都封装在这个类里面&#xff0c;其他地方看不到JDBC的代码步骤 1 : DAO接口package jdbc;import java.util.List;import charactor.Hero;public interface DA…

java 井字棋 人机_一个井字棋tictactoe游戏的java实现 | Soo Smart!

这是一个井字棋游戏的java实现。摘录于stackoverflow。游戏规则很简单&#xff0c;只要一方棋子在水平线&#xff0c;垂直线或者对角线任意一条线上排列成功即为获胜。作者原先的代码存在着一些问题&#xff1a;代码如下&#xff1a;一共有几个类: play, player, human, comput…

C++随机数(rand和srand)函数用法详解

C 提供了一组函数以生成和使用随机数字。随机数字就是从一组可能的值中进行随机选择而获得的一个值。该组中的值都有相同的被选中的几率。 随机数字常用于许多不同类型的程序中&#xff0c;以下是一些示例&#xff1a; 计算机游戏通常要使用随机数字来模拟一些随机过程&#x…

STL11-stack容器

#if 1 #include<iostream> #include<stack> using namespace std;void test01() {//初始化stack<int> s1;stack<int> s2(s1);//stack操作s1.push(10);s1.push(20);s1.push(30);s1.push(40);cout << "栈顶元素&#xff1a;" << e…

java犀牛是什么意思_深入浅出Rhino:Java与JS互操作

2011年10月6日&#xff0c;一年一度的JavaOne大会隆重举行。JavaOne2011大会的主题之一介绍针对不同Java平台的产品路线图&#xff0c;这其中包括移动版(ME&#xff0c;Micro Edition)、标准版(SE&#xff0c;Standard Edition)以及企业版(EE&#xff0c;Enterprise Edition)。…

STL12-queue容器

queue容器 队列容器 先进先出 队列只能在一端插入 一端删除 队列不能遍历 不提供迭代器 不支持随机访问 #if 1 #include<iostream> #include<queue> using namespace std; void test01() {queue<int> q; //创建队列queue<int> q2(q);q.push(10);q.pu…

java读取csv合适文件_解析-您可以推荐一个Java库来读取(并可能写入)CSV文件吗?...

Super CSV是读取/解析&#xff0c;验证和映射CSV文件到POJO的绝佳选择&#xff01;我们(Super CSV团队)刚刚发布了一个新版本(您可以从SourceForge或Maven下载它)。读取CSV文件以下示例使用read()(我们刚刚发布的新阅读器&#xff0c;使用Dozer进行具有深度映射和基于索引的映射…

Leedcode6-binary-tree-preorder-traversal

#include<iostream> #include<vector> #include<stack> using namespace std; // Definition for binary tree 先序遍历 根左右 struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; #if 0 c…

java 对list增删_List 中正确的增删操作

这个为什么要单独说的原因是&#xff0c;在开发中的对数据库中的增删为最基本的&#xff0c;但是是不是写对了就尤为重要先来看代码:1 public voidtestLoopInList(){2 List a new ArrayList();3 a.add("1");4 a.add("2");5 a.add("w");6 for(St…

Leedcode7-binary-tree-postorder-traversal

#include<iostream> #include<vector> #include<stack> using namespace std; // Definition for binary tree 先序遍历 根左右 struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; #if 0 c…

mysql zrm_mysql数据库备份—ZRM

ZRM是Zmanda Recovery Manager的缩写&#xff0c;这是一款备份mysql的开源软件。一、安装#yum install -y MySQL-zrm二、创建备用用户#mysql -uroot -pmysql>grant select,insert,update,create,drop,reload,shutdown,alter,super,lock table,replication client on *.* …

用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识

用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势&#xff0c;因为RNN能够将加入上&#xff08;下&#xff09;文信息进行考虑。一个简单的RNN如下图所示&#xff1a; 将这个循环展开得到下图&#xff1a; 上一…

java gzip rest_RestTemplate与Gzip压缩

Gzip 是一种压缩算法&#xff0c;服务器经常通过这个算法来压缩响应体&#xff0c;再响应给客户端&#xff0c;从而减少数据体积&#xff0c;提高传输速度。客户端再通过Gzip解压缩&#xff0c;获取到原始的数据。因为需要压缩计算&#xff0c;所以会耗费额外的CPU资源。Gzip 与…