几种stl的应用
1.set(特点:插入后元素自动从小到大排序)
set< int > ::iterator it;//迭代器,可以指向同类型的集合
q.find(k);//其中一个元素k的地址
q.count(k);//k的个数
q.erase(k);//删除所有值为k的元素
q.erase(it);删除it指向的地址和元素
NOTE:删除后指针指向不变
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string.h>
#include<set>
using namespace std;
int main()
{
multiset<int>q;
multiset<int>::iterator it;
//*****插入*****//
for(int i=1;i<16;i+=2)
q.insert(i);
q.insert(5),q.insert(11);
for(it=q.begin();it!=q.end();it++)
printf("%-3d",*it);printf("\n");
//set自动排序1 3 5 5 7 9 11 11 13 15
//*****查询相同元素个数*****//
printf("%d\n",(int)q.count(5));//查询5的个数:2
//*****删除一个·指定元素*****//
it=q.find(5);//q中的一个5的地址
q.erase(it);//删去了一个5
for(it=q.begin();it!=q.end();it++)
printf("%-3d",*it);printf("\n");
//1 3 5 7 9 11 11 13 15
//*****删除全部指定元素*****//
q.erase(11);//删除全部11
for(it=q.begin();it!=q.end();it++)
printf("%-3d",*it);printf("\n");
//1 3 5 7 9 13 15
//*****删除尾元素*****//
it=q.end();//q,end()不存在元素
q.erase(--it);//删去q.end()前面一个地址
for(it=q.begin();it!=q.end();it++)
printf("%-3d",*it);printf("\n");
//1 3 5 7 9 13
//*****查询大于等于所选元素的地址*****//
q.insert(9);//1 3 5 7 9 9 13
it=q.lower_bound(9);printf("%d\n",*it);//9
//求大于等于9的第一个数的地址
//*****查询大于所选元素的地址*****//
it=q.upper_bound(9);printf("%d\n", *it);//13
//求大于9的第一个数的地址
return 0;
}
2.vector
sort(q.begin(),q.end())//对vector里的元素进行排序
q.erase(unique(q.begin(),q.end()),q.end()); //截取重复的元素
q.insert(q.begin()+i,a);//在第i+1个元素前插入a
q.erase(q.begin()+i,q.begin()+j);//删除区间[i,j)内元素 (区间从0开始)
NOTE:删除后指针指向下一个元素
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>v;
int main()
{
for(int i=1;i<16;i+=2)
v.push_back(i);
v.push_back(2),v.push_back(5);
//vector<int>::iterator it;
for(int i=0;i<v.size();i++)
printf("%-3d", v[i]);printf("\n");
//1 3 5 7 9 11 13 15 2 5
//****排序****//
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
printf("%-3d", v[i]);printf("\n");
//1 2 3 5 5 7 9 11 13 15
//****去重****//
v.erase(unique(v.begin(),v.end()),v.end());
for(int i=0;i<v.size();i++)
printf("%-3d", v[i]);printf("\n");
//1 2 3 5 7 9 11 13 15
//****中间插入****//
v.insert(v.begin()+1,3);//第1+1个位置插入元素3
for(int i=0;i<v.size();i++)
printf("%-3d", v[i]);printf("\n");
//1 3 2 3 5 7 9 11 13 15
//****删除区间元素****//
v.erase(v.begin()+0,v.begin()+3);//删除[0,3)区间的元素
for(int i=0;i<v.size();i++)
printf("%-3d", v[i]);printf("\n");
//3 5 7 9 11 13 15
return 0;
}
3.priority_queue(特点:入队后元素自动从大到小排序)
普通队列首元素q.front(),优先队列首元素q.top();
q.empty();//判断q是否为空,空返回1,非空返回0
q.pop();//弹出队列中的第一个元素,无返回值
q.push(x);//推入x元素
q.size();//q中的元素个数
q.top();//返回队列第一个元素
1)默认的优先队列(结构体,重载小于)
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int x,y;
bool operator < (const node &a)const{
return x<a.x;//定义x小则代表该结构体小
}
}tt[111];
priority_queue<node>q;//定义为大的在队首
int main()
{
tt[1].x=3,tt[1].y=3; tt[2].x=2,tt[2].y=1;
tt[3].x=4,tt[3].y=4; tt[4].x=1,tt[4].y=2;
for(int i=1;i<=4;i++)
printf("%d ",tt[i].x),q.push(tt[i]);
printf("\n");//3 2 4 1
while(!q.empty()){//按队列元素的降序输出
node p=q.top();
q.pop();
printf("%-2d",p.x);
}//4 3 2 1
return 0;
}
2)less和greater优先队列
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,less<int> >l;//升序
priority_queue<int,vector<int>,greater<int> >g;//降序
int main()
{
for(int i=1;i<=5;i++)
l.push(i),g.push(i);
while(!l.empty()){
int p=l.top();
l.pop();
printf("%d ",p);
}printf("\n");//1 2 3 4 5
while(!g.empty()){
int p=g.top();
g.pop();
printf("%-2d",p);
}printf("\n");//5 4 3 2 1
return 0;
}
几种stl的应用的更多相关文章
- Codeforces Round #366 (Div. 2) C Thor(模拟+2种stl)
Thor 题意: 第一行n和q,n表示某手机有n个app,q表示下面有q个操作. 操作类型1:app x增加一条未读信息. 操作类型2:一次把app x的未读信息全部读完. 操作类型3:按照操作类型1 ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...
- STL学习之路
本文面向的读者:学习过C++程序设计语言(也就是说学习过Template),但是还没有接触过STL的STL的初学者.这实际上是我学习STL的一篇笔记,老鸟就不用看了. 什么是泛型程序设计 我们可以简单 ...
- STL —— STL六大组件
注:以下内容摘自 http://blog.csdn.net/byxdaz/article/details/4633826 STL六大组件 容器(Container) 算法(Algorithm) 迭代器 ...
- STL源码分析《4》----Traits技术
在 STL 源码中,到处可见 Traits 的身影,其实 Traits 不是一种语法,更确切地说是一种技术. STL库中,有一个函数叫做 advance, 用来将某个迭代器(具有指针行为的一种 cla ...
- STL学习小结
STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...
- 学习STL-介绍一下STL
从大学时就开始学习C++,到现在近5年的时间了却很少用到STL.现在想想真得是对不起这门语言,也对不起宝贵的五年光阴.我钟爱C++,所以一定要完全搞懂它,理解它.爱一个人的前提是要懂他(她),爱一门语 ...
- 两种QMultiMap的遍历方法(最好使用只读遍历器)
留个爪,备查 QMultiMap<QString, QString>& remote_map = my_obj->m_MapVersion; // ccc 这里体现了引用的好 ...
- ACM竞赛常用STL(一)
全排列函数next_permutation STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespac ...
随机推荐
- 【NLP CS224N笔记】Lecture 2 - Word Vector Representations: word2vec
I. Word meaning Meaning的定义有很多种,其中有: the idea that is represented by a word,phrase,etc. the idea that ...
- EM算法(坐标上升算法)
原文地址:https://www.cnblogs.com/to-creat/p/6075322.html 机器学习十大算法之一:EM算法.能评得上十大之一,让人听起来觉得挺NB的.什么是NB啊,我们一 ...
- TensorFlow学习笔记:共享变量
本文是根据 TensorFlow 官方教程翻译总结的学习笔记,主要介绍了在 TensorFlow 中如何共享参数变量. 教程中首先引入共享变量的应用场景,紧接着用一个例子介绍如何实现共享变量(主要涉及 ...
- Http 请求头中 X-Requested-With 的含义
昨天看代码的时候,看到了这个一句 String requestedWith = ((HttpServletRequest) request).getHeader("X-Requested-W ...
- 统计分析与R软件-chapter2-2
2.2 数字.字符与向量 2.2.1 向量 1.向量的赋值 x<-c(10.4,5.6,3.1,6.4,21.7) 2.向量的运算 x<-c(-1,0,2);y<-c(3,8,2) ...
- 基于TCP(面向连接)的Socket编程
基于TCP(面向连接)的Socket编程 一.客户端: 1.打开一个套接字(Socket); 2.发起连接请求(connect); 3.如果连接成功,则进行数据交换(read.write.send.r ...
- CFtpConnection Class
CFtpConnection Class 1.链接http://technet.microsoft.com/zh-cn/office/2kywsafk(v=vs.80) 2.测试ftp可以用这个地 ...
- .NET之美 第一部分C#语言基础
第一章 类型基础 1 值类型与引用类型 CLR 支持两种类型:值类型和引用类型, C#的所有值类型均隐式派生自System.ValueType: 结构体:struct(直接派生于System.Valu ...
- Centos6.8上httpd配置腾讯云SSL证书
(1)先按装mod_ssl yum -y install mod_ssl /etc/httpd/conf.d/下会有一个ssl.conf的文件,打开 a)检测本地证书配置是否正确 主要是看下证书及密钥 ...
- live555运行时报错:StreamParser internal error ( 86451 + 64000 > 150000)
搭建好live555服务器后,使用 vlc播放网络视频.此时服务器端报如下错误: StreamParser internal error ( 86451 + 64000 > 150000). ...