C++中vector的使用
来源:互联网 发布:南京市行知实验中学 编辑:程序博客网 时间:2024/06/01 23:35
1、vector的基本增加、删除、访问
#include<iostream>#include<vector>using namespace std;int main(){ vector<double> score; for (int i = 0; i < 5; i++) { score.push_back(i); } // 访问修改值 for (int i = 0; i < score.size(); i++) { score.at(i) = 5; cout << i<<":"<<score.at(i) << " " << score[i] << endl; } // 迭代器遍历访问修改值 vector<double>::iterator pd = score.begin(); // 输出起始地址 cout <<"vector起始地址:"<< &(*pd) << endl; while (pd != score.end()) { *pd += 1; cout << *pd << " "; pd++; } cout << endl; for (int i = 0; i < score.size(); i++) { cout << &score[i] << " "; } cout << endl; cout << score.size() << endl; // 删除指定迭代器指针指向元素 score.erase(pd-1); cout << "erase删除指定元素后:"<<score.size() << endl; // 删除vector中所有元素 score.clear(); cout << "clear清空所有元素:"<<score.size() << endl; system("pause"); return 0;}
运行结果如下
2、vector 的存储空间
vector<double> v1; vector<int> v2; vector<vector<int>> v3; cout << "v1:" << sizeof(v1) << endl; cout << "v2:" << sizeof(v2) << endl; cout << "v3:" << sizeof(v3) << endl;
输出:
161616
vector是在堆上直接分配内存的,sizeof(vector)的大小只取决于vector中的四个成员变量,和vector的存储的数据类型,数据个数无关。
四个成员变量
iterator _First _Last _End
每个指针是四个字节,一共16字节
3、vector当参数传递
vector当参数传递一共有三种形式
function1(vector vec); // 传入值function2(vector &vec); // 传入引用function3(vector *vec); // 传入地址
第一种方式会发生拷贝,后两种不发生拷贝
vector<int> vec;function1(vec); // 传入值function2(vec); // 传入引用function3(&vec); // 传入地址
阅读全文
0 0
- C++STL中vector的使用
- C++----Vector的使用
- [C++] C++ Vector 的使用
- C++Vector的使用误区
- C++Vector的使用误区
- c++Vector的使用误区
- C++Vector的使用误区
- C++:使用vector的元素
- Vector 的使用(C++)
- j2me中vector的使用
- c++中vector的使用
- C++中vector的使用
- C++中vector的使用
- C++中vector的使用
- C++中vector的使用
- C++中vector的使用
- vector中函数的使用
- c++中Vector的使用
- CCNA第七天(帧中继协议)
- 容器/IO/线程知识补漏
- 报表FineReport部署Tomcat服务器
- find the mincost route(城市最小环)
- 表格与表单
- C++中vector的使用
- 深入 kernel panic 流程
- c++顶级社区
- 第18节-程序编辑器vim
- python基础五_列表生成式,生成器
- Spire.Doc 教程:将word文档中的隐藏文本保存到PDF
- mysql当前日期函数应用
- Java并发编程:volatile关键字解析
- 亲历H5移动端游戏微信支付接入及那些坑(三)——支付接入