STL初步

最近学STL感觉非常陌生,有点想放弃,但是坚持几天好多了,心态一定要好。


前言

简单认识STL

一、sort

快速排序

文件头

#include<algorithm>

升序排列
sort(a,a+n);

int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	sort(a,a+n);//默认升序排列 
	for(int i = 0; i < n; i++)
		cout << a[i] << " \n"[i==n-1];
	return 0;
}

特殊排列需要自己插入一个 cmp 函数
以降序为例
sort(a,a+n,cmp);

int cmp(int a, int b){
	return a > b;
} 

int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	sort(a,a+n,cmp);
	for(int i = 0; i < n; i++)
		cout << a[i] << " \n"[i==n-1];
	return 0;
}

cmp 可按需自行编辑

二、vector

vector就是一个每部封装有操作的不定长数组

1.相关函数

a.size( ) 读取它的大小
a.resize( ) 改变大小
a.push_back( ) 向尾部添加元素
a.pop_back( ) 删除最后一个元素
clear( ) 清空
empty( ) 测试是否为空

2.vector用法

vector之间可以直接赋值或者作为函数的返回值

头文件

>#include<vector>

申明类型

不定长
vector <int> a;
vector <double> b;
vector <string> s;

vector <int> pile[maxn];
//vector就像一个二维数组,只是第一维的大小是固定的(不超过maxn),
//但第二维的大小不固定。

使用

int main(){
	int n, m;
	vector <int> a;
	cin >> n;
	while(n--){
		cin >> m;
		a.push_back(m);//在最后添加m
	}
}

三、set

set就是一个集合,其特性是自动排序和插入进去的元素不会重复

1.相关函数

insert () 插入元素

string s;
set<string> dict;
{
	dict.insert(s);
}

lower_bound和upper_boundupper_bound 二分查找

int a;
int num[];
a = lower_bound(num);//a=num数组中不小于a的第一个数组下标

count(x) 判读xx是否在集合中出现过
size()size() 返回元素个数
clear()clear() 清空容器
erase(s) 在这个集合中删去元素s

这里

2.set用法

#include<set> // 头文件
set<int>  num; //申明
int main(){
	int n;
	cin >> n;
	cout << n;
	while(n--){
		num.insert(read());
	}
	
	for(set<int>::iterator it = num.begin(); it != num.end(); ++it)
		cout << *it << "\n";//你会惊奇的发现已经排好序了,好耶!
	return 0;
} 

题目:输入一个文本,按字典序从小到大输出无重复的各个单词

set<string> buf;

int main(){
	char ch;
	string s;
	while((ch = getchar()) != EOF){
		while(isalpha(ch)){
			ch = tolower(ch);
			s += ch;//将单词拼接复原 
			ch = getchar();
		}
		buf.insert(s);
		s.clear();//使用后,输出前,清除 
	}
	
	for(set<string>:: iterator it = buf.begin(); it != buf.end(); it++){
		cout << *it << endl;//利用迭代器,提取的是地址 
	}
	
	return 0;
}

利用 stream 的方法

#include<cstdio>
#include<iostream>
#include<cctype>
#include<set>
#include<string>
#include<sstream>
using namespace std;

set<string> dict;

int main(){
	string s, buf;
	while(cin >> s){//不能用cin读入会卡死 
		for(int i = 0; i < s.length(); i++){
			if(isalpha(s[i]))s[i] = tolower(s[i]);
			else s[i] = ' ';
		}
		stringstream ss(s);
		while(ss >> buf) dict.insert(buf);
	}
	
	for(set<string>::iterator it = dict.begin(); it != dict.end(); it++){
		cout << *it << endl;
	}
	
	return 0;
}

四、map

映射,E.g.

map <string int> month;
month[january] = 1;

1.相关函数

count()

find()

2.应用

题目:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入的大小写,按字典序排列。

#include<iostream>
#include<map>
#include<cctype>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

map <string, int> cnt;
vector <string> words;

string repr(const string &s) {//将 s[] 存入 ans[] 中 
	string ans = s;
	for(int i = 0; i < ans.length(); i++)
		ans[i] = tolower(ans[i]);//变小写后方便利用字典序排序 
	sort(ans.begin(), ans.end());//字典序排序 ,排除 abc bca cab 重名 
	//调用地址用 .begin() 
	return ans;
}

int main(){
	int n = 0;
	string s;
	while(cin >> s) {
		if(s[0] == '#') break;
		words.push_back(s);//所有词存入words[] 
		string r = repr(s);//对 s 排序后存入 r 
		if(!cnt.count(r))cnt[r] = 0;//没出现的单词都赋初值为0 
		// .count(r) 表示 r 在cnt中出现的次数,即 cnt[r] 
 		cnt[r]++;//cnt[对应单词] 出现次数 
	}
	vector <string> ans;//答案 
	for(int i = 0; i < words.size(); i++) {
		if(cnt[repr(words[i])] == 1) ans.push_back(words[i]);//存入满足条件的答案 
	}
	sort(ans.begin(), ans.end());//按字典序输出 
	for(int i = 0; i < ans.size(); i++)
		cout << ans[i] << "\n";
	return 0;
}

五、stack

#include<stack>
stack <int> s;
s.push();//压入栈中
s.pop();//弹出
s.top();//取栈顶但不删除

六、队列

普通队列

#include<queue>

queue<int> pq

优先队列
“priority_queue<int,vector,greater>pq”
“priority_queue<int,vector,cmp>pq”

#include<queue>
#include<vector>

priority_queue<int, vector<int>, greater<int> > pq;//注意避免与位运算弄混 > >空格隔开

插了一些 dalao 的链接,如有冒犯请告知,侵权必删。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值