vector容器的简单应用,我们可以用vector维护一个有序数组,每次对要插入的数用upper_bound或者lower_bound来
为这个数找一个应该插入到vector的位置。另外再找一个数组来维护插入数的顺序,来面对pop操作
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,
找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,
找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,
找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,
找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
void push_back(const T& x):向量尾部增加一个元素X
iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
 
构造函数

vector():创建一个空vector

vector(int nSize):创建一个vector,元素个数为nSize

vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t

1 //int型vector,包含3个元素且每个元素都是9
2 vector<int> vecIntB(3,9);

vector(const vector&):复制构造函数

vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中

 
vector容器的查找
find(r.begin(),r.end(),要查找的值),注意这个find相当于一个“类函数”,即不需要vector.find()
返回值可赋值给迭代器(比如it),it-=r.begin()会得到一个下标,这个下标就是要查找的值在vector中的位置(从0开始)
如果没有找到会返回r.end()(r.end()位置的值是一个随机数)。所以可以与r.end()比较来判断这个值存不存在
 
vector元素的删除
iterator erase(iterator it):删除向量中迭代器指向元素
 
遍历vector
 1 #include<iostream>
2 #include<queue>
3 #include<vector>
4 #include<string.h>
5 #include<stdio.h>
6 #include<algorithm>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=5e4+10;
10 const int N=1e4+10;
11 int main()
12 {
13 vector<int>r;
14 vector<int>::iterator it;
15 r.push_back(1);
16 r.push_back(2);
17 r.push_back(3);
18 for(it=r.begin();it!=r.end();it++)
19 cout<<(*it)<<endl;
20 }
21 /*
22 结果:
23 1
24 2
25 3
26
27 */
 
更多vector操作见:https://blog.csdn.net/qq_31858735/article/details/82623110
 
 
本题代码:
 1 /*
2 vector容器的简单应用,我们可以用vector维护一个有序数组,每次对要插入的数用upper_bound或者lower_bound来
3 为这个数找一个应该插入到vector的位置。另外再找一个数组来维护插入数的顺序,来面对pop操作
4
5
6 在从小到大的排序数组中,
7 lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,
8 找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
9
10 upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,
11 找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
12
13
14 在从大到小的排序数组中,重载lower_bound()和upper_bound()
15 lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,
16 找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
17
18 upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,
19 找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
20
21
22
23 void push_back(const T& x):向量尾部增加一个元素X
24 iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
25 iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
26 iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
27 更多vector操作见:https://blog.csdn.net/qq_31858735/article/details/82623110
28 */
29 #include<stdio.h>
30 #include<string.h>
31 #include<iostream>
32 #include<algorithm>
33 #include<map>
34 #include<queue>
35 #include<vector>
36 using namespace std;
37 const int maxn=1005;
38 const int N=1e4+10;
39 int main()
40 {
41 int n;
42 vector<int> v1,v;
43 scanf("%d",&n);
44 vector<int>::iterator it;
45 while(n--)
46 {
47 char ch[15];
48 scanf("%s",ch);
49 string s = ch;
50 if(s == "Push")
51 {
52 int temp;
53 scanf("%d",&temp);
54 v1.push_back(temp);
55 it = lower_bound(v.begin(),v.end(),temp); //获取大于等于temp这个值位置
56 v.insert(it,temp); //vector插入到it的前面
57 }
58 else if(s == "Pop")
59 {
60 if(v1.size() == 0)
61 printf("Invalid\n");
62 else
63 {
64 it = lower_bound(v.begin(),v.end(),v1[v1.size()-1]);
65 v.erase(it);
66 printf("%d\n",v1[v1.size()-1]);
67 v1.pop_back();
68 }
69 }
70 else if(s == "PeekMedian")
71 {
72 if(v1.size() == 0)
73 {
74 printf("Invalid\n");
75 continue;
76 }
77 if(v.size() % 2 == 0)
78 printf("%d\n",v[v.size()/2-1]);
79 else
80 printf("%d\n",v[v.size()/2]);
81 }
82 }
83 return 0;
84 }

L3-002 特殊堆栈 (30分) vector容器的模拟、vector容器的一些用法的更多相关文章

  1. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  2. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  3. PTA 07-图5 Saving James Bond - Hard Version (30分)

    07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie ...

  4. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  5. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  6. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

  7. 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器

    一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...

  8. 1111 Online Map (30 分)

    1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...

  9. stack堆栈容器、queue队列容器和priority_queue优先队列容器(常用的方法对比与总结)

    stack堆栈是一个后进先出的线性表,插入和删除元素都在表的一端进行. stack堆栈的使用方法: 采用push()方法将元素入栈: 采用pop()方法将元素出栈: 采用top()方法访问栈顶元素: ...

随机推荐

  1. Centos 打开ssh 密码验证 和 root 登录

    # 1 打开系统的密码验证功能: vim /etc/ssh/sshd_config #允许使用密码登录(注释此行 就是允许证书登录) PasswordAuthentication yes # 2 打开 ...

  2. 【Nginx】使用keepalive和nginx搭载高可用

    首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现web服务端的高可用. Keepa ...

  3. 【IMP】IMP导入表的时候,如果表存在怎么办

    在imp导入的时候,如果表存在的话,会追加数据在表中, 所以如果不想追加在表中的话,需要将想导入的表truncate掉后,在imp SQL: truncate table TEST1; imp tes ...

  4. Electron小白入门自学笔记(一)

    码文不易啊,转载请带上本文链接呀,感谢感谢 https://www.cnblogs.com/echoyya/p/14297176.html 一.从Hello Electron开始 创建一个空的文件夹, ...

  5. 二. SpringCloud基本Rest微服务工程搭建

    1. 父工程构建 1.1 Maven项目搭建 环境 版本 JDK 1.8 Maven 3.6+ Maven模板 maven-archetype-size 删除父工程src文件 1.2 父工程pom文件 ...

  6. 人工智能"眼睛"——摄像头

    摄像头机器视觉人工智能的"眼睛",其重要性在嵌入式领域不言而喻.但是如何理解和使用摄像头却是一个非常棘手的问题.本文主要针对调试摄像头过程中遇到的问题,对摄像头的基本原理及概述进行 ...

  7. [从源码学设计] Flume 之 memory channel

    [从源码学设计] Flume 之 memory channel 目录 [从源码学设计] Flume 之 memory channel 0x00 摘要 0x01 业务范畴 1.1 用途和特点 1.2 C ...

  8. 干货 | 质量保障新手段,携程回归测试平台实践 原创 Sedro 携程技术 2021-01-21

    干货 | 质量保障新手段,携程回归测试平台实践 原创 Sedro 携程技术 2021-01-21

  9. git commit前检测husky与pre-commit 提交钩子

    git commit前检测husky与pre-commit git commit前检测husky与pre-commit - 简书 https://www.jianshu.com/p/f0d31f92b ...

  10. 这些年来,一直不知道Code Fisrt的真实意义。

    目录 Code First 是一个糟糕的名字 放弃 EDMX,但继续实行数据库优先 Code First 是一个糟糕的名字 很多人依据它的名字认为,它是在代码定义模型,然后从模型生成数据库. Code ...