从零起步看算法(第十七天 5.3)
即将进行期末的复习,还债阶段。但还是想继续走下去。。。
//集合
1.基本操作
//集合
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main(){
set<string> country;// 建立集合
country.insert("China");//集合的插入,集合中没有重复元素
country.insert("America");
country.insert("France");
country.erase("America");//集合中删除某个元素
country.erase("England");
if(country.count("China")){//在集合中查找是否有某个元素 count(),有返回1,无返回0;
cout<<"China belong to country"<<endl;
}
for(set<string>::iterator it=country.begin();it!=country.end();++it)//用迭代器访问集合中的元素,自动从小到大排序
{
cout<<(*it)<<endl;
}
cout<<country.size()<<endl;//集合元素个数的获取
country.clear();//集合的清除
return 0;
}
2.计算集合的并
大水题,熟练应用集合
//计算集合的并
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main(){
set<int> s;
int n,m;
cin>>n>>m;
while(n){
int a;
cin>>a;
s.insert(a);
n--;
}
while(m){
int b;
cin>>b;
s.insert(b);
m--;
}
//set<string>::iterator it;
for(set<int>::iterator it=s.begin() ;it!=s.end();it++){
if(it!=s.begin())cout<<" ";
cout<<*it;
}
cout<<endl;
return 0;
}
3.蒜头君学英语
思路很简单,也是一道水题了。不过有两个注意点值得回顾和复习。
1.string,的初始化。
点击打https://blog.csdn.net/cool_mirror/article/details/1703084开链接
2.c++标准库中,如何转换英语字母的大小写。
点击打开链接
ac码:
//蒜头君学英语
#include<iostream>
#include<string>
#include<set>
#include<cctype>
using namespace std;
int main(){
set<string> s;
int n;
cin>>n;
while(n){
int d;
cin>>d;
if(d==0){
string word;
cin>>word;
//大小写
for(int i=0;i<word.size();i++){
word[i]=tolower(word[i]);
}
s.insert(word);
}
if(d==1){
string word;
cin>>word;
for(int i=0;i<word.size();i++){
word[i]=tolower(word[i]);
}
if(s.count(word)){cout<<"Yes"<<endl;}
else cout<<"No"<<endl;
}
n--;
}
return 0;
}