STL基础1:vector
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std; bool findCondition(int x)
{
return x%2==0;
} //仿函数
template<typename T>
struct display{
void operator()(const T &x)const
{
cout<<x<<" ";
}
}; int main()
{ int i; vector<int> iv;//vector 追加和随机访问效率高,插入删除效率低 int ia[]={0,1,2,3,4,5,6,6,6,7,8};
vector<int> iv2(ia,ia+sizeof(ia)/sizeof(int)); //数组转换成向量
for_each(iv2.begin(),iv2.end(),display<int>());
cout<<endl; iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
iv.push_back(4);
iv.push_back(5);
iv.pop_back(); //推出 5 ,返回void for_each(iv.begin(),iv.end(),display<int>());
cout<<endl; cout<<"元素的数量"<<iv.size()<<"容量"<<iv.capacity()<<endl; //随机读写
iv[0] = 99;
cout<<iv[0]<<endl; vector<int>::iterator it,it2;
it=iv.begin();//iv.begin()指向第一个元素
iv.insert(it,3,-1);//在it所指的单元前插入3个-1 it=iv.end()-1;//iv.end()指向了下一个最后一个元素的下一个元素
iv.erase(it);//删掉倒数第1个元素,也可以删除区间的元素
cout<<"迭代器输出 "<<*it<<endl;//输出的是刚才那个被删掉的元素4,也就是说删掉元素之后,it并没有改变,而且指向了一个被删除的元素 iv2.assign(iv.begin(),iv.end());//复制iv到iv2
if(iv==iv2)
{
cout<<"iv == iv2"<<endl;
}
iv2.clear();//删除容器中的所有元素,iv.size()变成0,而iv.capacity()不变,也就是内存并未归还给OS
iv2.swap(vector<int>());//iv.capacity()也变为0,删除并归还内存给OS的常用方法 //非变异算法
it = find(iv.begin(),iv.end(),3);//在[first,last)(闭开区间)上查找等于3值的元素
if(it==iv.end()){
cout<<"Not found 3"<<endl;
}else{
cout<<"Found in iv["<<it-iv.begin()<<"]"<<endl;
} it = find_if(iv.begin(),iv.end(),findCondition);//查找被2整除的元素
if(it==iv.end()){
cout<<"Not found %2"<<endl;
}else{
cout<<"Found in iv["<<it-iv.begin()<<"]"<<endl;
} cout<<"count -1 is "<<count(iv.begin(),iv.end(),-1)<<endl; //求和,第三个值0是用于函数辨识数据类型
cout<<"sum = "<<accumulate(iv.begin(),iv.end(),0)<<endl;
//string sum = accumulate(v.begin() , v.end() , string(" "));这个可以把向量字符串连接起来 //变异算法
cout<<"翻转 前";
for_each(iv.begin(),iv.end(),display<int>());
cout<<endl;
reverse(iv.begin(),iv.end());
cout<<"翻转 后";
for_each(iv.begin(),iv.end(),display<int>());
cout<<endl; replace(iv.begin(),iv.end(),-1,200);//将所有的-1替换成200
fill_n(iv.begin(),3,44);//在开头填充3个44,如果空间不足就会报错
fill_n(iv.begin()+4,2,2);
//generate_n(iv.begin(),5,rand);//条件填充
//it = remove_if(iv.begin(),iv.end(),findCondition);//删除条件内的元素,依次填充到头,然后返回一个it,这个it是尾部,实际的iv.size()是不变的
cout<<"unique 前的值"<<endl;
for_each(iv.begin(),iv.end(),display<int>());
cout<<endl;
it = unique(iv.begin(),iv.end());//只是把唯一的元素全部移到了前面,iv的值并不改变
cout<<"unique 后的值"<<endl;
for_each(iv.begin(),iv.end(),display<int>());
cout<<endl;
for(it2=iv.begin();it2!=it;it2++)
{
cout<<*it2<<endl;
}
cout<<iv.size()<<" 2 "<<iv2.size()<<endl;
unique_copy(iv.begin(),iv.end(),back_inserter(iv2));//将结果拷贝到iv2,并且iv2要有足够的空间,所以这里需要back_inserter。
for_each(iv2.begin(),iv2.end(),display<int>());
cout<<endl;
//排序算法 //转换成堆,排序
make_heap(iv.begin(),iv.end());
sort_heap(iv.begin(),iv.end()); sort(iv.begin(),iv.end());//等同于sort(vect.begin(), vect.end(), less<int>() );降序排序sort(iv.begin(),iv.end(),greater<int>());
stable_sort(iv.begin(),iv.end());//使相同的值在原来的位置不变,参数传入的是结构体时,会发现两者之间的明显区别
partial_sort(iv.begin(),iv.begin()+4,iv.end());//取top4
random_shuffle(iv.begin(),iv.end());//打乱顺序 for (i=0;i<iv.size();i++)
{
cout<<"0+"<<iv[i]<<endl;
}
cout<<"元素的数量"<<iv.size()<<endl; return 0;
}
STL基础1:vector的更多相关文章
- 【STL基础】vector
vector 构造函数: //default: vector<T> v; //空的vector //fill: vector<T> v(n); //n个元素的vector,元素 ...
- 转:用STL中的vector动态开辟二维数组
用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...
- STL基础复习
stl容器:vector,deque,list,map/multimap,set 特殊容器:stack,queue,priority_queue 通用操作 size() 返回当前容器元素数量 emp ...
- STL基础--基本介绍
为什么要使用C++标准库 /* * 为什么使用C++标准库: * 1. 代码重用,不用重新造轮子 * 2. 效率(快速,且使用更少的资源). 现代C++编译器经常对C++标准库的代码有优化 * 3. ...
- 【c++基础】vector初始化的几种方式
前言 STL中的vector有几种初始化方式,根据不同的需求选择合适的初始化方式. 源码 // constructing vectors #include <iostream> #incl ...
- STL中的Vector相关用法
STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...
- (转)C++ STL中的vector的内存分配与释放
C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...
- STL中的vector实现邻接表
/* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include ...
- 2.1实现简单基础的vector
2.1实现简单基础的vector 1.设计API 我们参考下C++ <std> 库中的vector, vector中的api很多,所以我们把里面用的频率很高的函数实现; 1.1 new&a ...
随机推荐
- Win7 访问win2008 远程桌面提示:您的凭证不工作
背景: win7 远程桌面连接 服务器 windows 2008 报错,“您的凭证不工作”,但是 xp 系统却可以正常连接. 解决方法: 1.在“运行” 中执行 secpol.msc-->进入本 ...
- PAT1066(AVL树)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT1021(dfs 连通分量)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 贪吃蛇Global Java实现(二)
package cn.tcc.snake.util; public class Global {public static final int CELL_SIZE=20;public static f ...
- 吉哥系列故事——恨7不成妻(数位DP)
吉哥系列故事——恨7不成妻 http://acm.hdu.edu.cn/showproblem.php?pid=4507 Time Limit: 1000/500 MS (Java/Others) ...
- Jmeter 录制脚本(一)
第一种方法:使用Badboy来录制脚本 1. 启动Badboy, 工具栏上的红色圆形按钮是默认启动的,在地址栏直接输入被测试WEB项目的地址,然后点击右边的箭头. 2.录制完成后,点击工具栏上的黑色按 ...
- JMeter监控内存及CPU ——plugin插件监控被测系统资源方法
jmeter中也可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 1.需要的插件准备 JMeterPlugins-Standard-1.3.1.zip 下载 ...
- 在CentOS7.4上手动编译安装Mysql-5.7.20
实验环境:CentOS 7.4 mysql软件: mysql-boost-5.7.20.tar.gz 1.安装编译工具 yum -y install \ncurses \ncurses-devel \ ...
- PTA 7-6 列出连通集(深搜+广搜)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- f5 Seldom used
1.负载均衡算法 2)最快响应速度(Fastest) •优先查看7层请求的连接数,然后查看4层连接数 •需要在virtual server上关联7层的profile,否则与最小连接数相同 •后台服务器 ...