STL初步

排序与检索:

sort(a,a+n);
sort(v.begin(),v.end());
lower_bound 查找大于或者等于x的第一个位置
unique函数可以删除有序数组中的重复元素

不定长数组:vector

a.clear();清空函数
a.empty():测试是否为空
a.size():
a.resize();改变大小
a.push_back(); 向尾部添加元素
a.pop_back();删除最后一个元素
是一个模板类,声明形式vector

集合:set

安迪的第一个字典(Andy’s First Dictionary,Uva 10815)
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单 词不区分大小写。

样例输入:

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the road.

The sign read: “Disneyland Left.” So they went home.

样例输出(为了节约篇幅只保留前5行):

a

adventures

blondes

came

disneyland

#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std;
//Adventures in Disneyland Two blondes were going to Disneyland when they came to a fork in the road.The sign read: "Disneyland Left." So they went home.
//输入后在下一行输入 ctrl+z 表示结束标志 
set<string> dict;
int main(){
	string s,buf;
	while(cin>>s){
		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;	
} 

isalpha是一种函数:判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。
str.isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。

tolower是一种函数,功能是把字母字符转换成小写,非字母字符不做出处理。str[i] = tolower(str[i]);

stringstream 主要是用在將一個字串分割,可以先用 clear( )以及 str( ) 將指定字串設定成一开始的內容,再用 >> 把个別的资料输出,例如:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string s;
	stringstream ss;
	int a, b, c;
	getline(cin, s);
	ss.clear();
	ss.str(s);
	ss >> a >> b >> c;
	cout<<a<<" "<<b<<" "<<c<<endl;

	return 0;
}

下面我們看到一個使用 stringstream 的例子:

題目:输入的第一行有一个数字 N 代表接下來有 N 行資料,每一行資料里有不固定個數的整數(最多 20 個,每行最大 200 個字元),請你寫一個程式將每行的总和印出來。

輸入:

3
1 2 3
20 17 23 54 77 60
111 222 333 444 555 666 777 888 999

輸出:

6
251
4995

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string line;
	while(getline(cin, line))
	{
		int sum = 0, x; 
		stringstream ss(line); // 将 line 复制到 stringstream ss 中 
		while(ss >> x) sum += x; // 相当于输入一个个的单词,自动将其转化换为数字 
		cout << sum << "\n";
	}
	return 0;
}

映射:map

map就是从键(key)到值(value)的映射。重载了[]运算符,map像是数组的“高级版”
map<string,int> mouth_name;
month_name[“July”] = 7;

反片语(map)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=92
输入一些单词,找出所有满足如下条件的单词:

该单词不能通过字母重排,得到输入文本中的另外一个单词。

在判断是否满足条件时,不区分大小写,但输出保留输入中的大小写,按字典序进行排列(所有大写字母在小写字母的前面)

样例输入:

ladder came tape soon leader acme RIDE lone Dreis peat

ScALE orb eye Rides dealer NotE derail LaCeS drIed

noel dire Disk mace Rob dires

样例输出:

Disk
NotE
derail
drIed
eye
ladder
soon

分析:把每个单词“标准化”,即全部转化为小写字母后再进行排序,然后再放到map中进行统计

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

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

string repr(const string &s){  //标准化:化为小写 并按照字母顺序给字母重新排序 
	string ans=s;
	for(int i=0;i<ans.length();i++){
		ans[i] = tolower(ans[i]);
	}
	sort(ans.begin(),ans.end());
	return ans;
}
int main(){
	int n=0;
	string s;
	while(cin>>s){
		if(s[0]=='#')break;
		words.push_back(s);
		string r=repr(s);
		if(!cnt.count(r)) cnt[r]=0;   //count(n)返回容器中n出现的次数 
		cnt[r]++;
	}
	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]<<endl;
	return 0;
}

map.count(Key)返回值为1或者0,1返回存在,0返回不存在,返回的是布尔类型的值,因为在map类型中所有的数据的Key值都是不同的,所以被count的数要么存在1次,要么不存在

集合栈计算机(The SetStack Computer)
Time limit: 3.000 seconds
题目是这样的:

   有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且支持以下操作:

PUSH:空集“{}”入栈
DUP:把当前栈顶元素复制一份后再入栈
UNION:出栈两个集合,然后把两者的并集入栈
INTERSECT:出栈两个集合,然后把二者的交集入栈
ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈
每次操作后,输出栈顶集合的大小(即元素个数)。例如栈顶元素是A={ {}, {{}} }, 下一个元素是B={ {}, {{{}}} },则:
UNION操作将得到{ {}, {{}}, {{{}}} },输出3.
INTERSECT操作将得到{ {} },输出1
ADD操作将得到{ {}, {{{}}}, { {}, {{}} } },输出3.
(输入:先输入测试次数,再输入操作次数,再输入具体操作)
Sample Input
2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT

Sample Output
0
0
1
0
1
1
2


0
0
1
0
0


【分析】
为每个不同的集合分配一个唯一的ID,则每个集合都可以表示成所包含元素的ID集合,这样就可以用STL的set来表示了,而整个栈则是一个stack。

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

#define ALL(x) x.begin(),x.end()  //所有的内容
#define INS(x) inserter(x,x.begin()) //插入迭代器
typedef set<int> Set;
map<Set,int> IDcache; //把集合映射成ID
vector<Set> Setcache; //根据ID取集合

//查找给定集合x的ID,如果找不到,分配一个新ID 
int ID(Set x){
	if(IDcache.count(x))  return IDcache[x];
	Setcache.push_back(x);
	return IDcache[x] = Setcache.size() -1;
}
int main(){
	int T;
	cin>>T;
	while(T--){
		stack<int> s;
		int n;
		cin>>n;
		for(int i=0;i<n;i++){
			string op;
			cin>>op;
			if(op[0]=='P') s.push(ID(Set()));
			else if(op[0]=='D') s.push(s.top());
			else{
				Set x1 = Setcache[s.top()]; s.pop();
				Set x2 = Setcache[s.top()]; s.pop();
				Set x;
				if(op[0] == 'U') set_union(ALL(x1),ALL(x2),INS(x));
//set_union是一个求并集的方法set_union(A.begin(),A.end(),B.begin(),B.end(),C.begin())并集插入的地方c.begin() 

				if(op[0] == 'I') set_intersection(ALL(x1),ALL(x2),INS(x));
//set_intersection是一个求交集的方法set_intersection(A.begin(),A.end(),B.begin(),B.end(),C.begin())交集插入的地方是c.begin() 
				if(op[0] == 'A') {
					x = x2;
					x.insert(ID(x1));
				}
				s.push(ID(x));
			}
			cout<<Setcache[s.top()].size() <<endl;
		}
		cout<<"***"<<endl;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值