题目:给两个字符串,判断交并补
重点: 字符串的读取和处理,交并补的判断
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cstdio>
#include<iostream>
#include<math.h>
#include <map>
#include <vector>
#include <algorithm>//注意find不属于vector
#define M 10 //注意用define最后没有句号
using namespacestd;
int main(){
string a,b,temp;
vector<string> numa;
vector<string> numb;
vector<string> ::iterator it;//注意那两个点
while(getline(cin,a)){
getline(cin,b);
istringstream record(a); //1️⃣利用sstream。把getline读到的串用空格分开。push进去
while(record >> temp){
numa.push_back(temp);
}
istringstream recordb(b);
while(recordb >> temp){
numb.push_back(temp);
}
unsignedlong size = numb.size();
for(int i =0; i < size; i ++) {
it = find(numa.begin(), numa.end(), numb[i]); //2️⃣find的使用方法,返回迭代器,find里面是两个迭代器(开始和结束)➕查找的元素
if(it != numa.end()){ //通过判断迭代器是不是指到最后判断
numb.erase(numb.begin()+i); //3️⃣erase的使用方法也是利用迭代器
numa.erase(it);
i--;
}
}
if((numb.empty() ==1) && (numa.empty() ==1) ) cout<<"A equals B"<<endl;
elseif((numb.empty() ==1) && (numa.empty() ==0) ) cout<<"B is a proper subset of A"<<endl;//4️⃣如果是空,empty返回true
elseif((numb.empty() ==0) && (numa.empty() ==1) ) cout<<"A is a proper subset of B"<<endl;
elseif(numb.size() == size)cout<<"A and B are disjoint"<<endl;
elsecout<<"I'm confused!"<<endl;
numa.clear();
numb.clear();//如果不清理干净。之前如果vector非空,会把元素继续往后加入5️⃣
}
return0;
}
/*
55 27
55 27
9 24 1995
9 24
1 2 3
1 2 3 4
1 2 3
4 5 6
1 2
2 3
输出:
A equals B
B is a proper subset of A
A is a proper subset of B
A and B are disjoint
I'm confused!