基础数据结构(2)集合

从零起步看算法(第十七天  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;
	}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值