题目一:
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
例如:第一个字符串是"They are students.",第二个字符串是”aeiou"。删除之后的第一个字符串变成"Thy r stdnts."。
保证两个字符串的长度均不超过100。
方法一:使用map函数解题
#include <iostream>
#include <string>
#include <map>//map()函数库
using namespace std;
string a, b;
int main() {
getline(cin, a);
cin >> b;
map<char,int>m;
for(auto i:b)m[i]++;
for(auto i : a) {
if (!m.count(i)) cout << i;
}
return 0;
}
补充:
1.for (auto) 用法:
for(auto i:a)中b为一个容器,效果是利用a遍历并获得b容器中的每一个值,但是a无法影响到b容器中的元素。
for(auto &i:a)中加了引用符号,可以对容器中的内容进行赋值,即可通过对a赋值来做到容器b的内容填充。
for instance :
#include<iostream>
using namespace std;
int main()
{
string s( "hello world" );
for (auto c:s)
c= 't' ;
cout<<s<<endl; //结果为hello world
for (auto &c:s)
c= 't' ;
cout<<s<<endl; //结果为ttttttttttt
}
2. map()函数:
定义:map是C++中STL中的一个关联容器,以键值对来存储数据,数据类型自己定义。
使用时需要加入#include <map>函数库。
语法:map<下标类型,值类型> 变量名;
定义map类型,是有模板的,他接受三个参数:
第一个参数是键的数据类型
第二个参数是值的数据类型
第三个参数是排序规则,不写的话就按照默认的排序规则,也就是按照键的升序
举例:
map<int,int>mp;
定义了一个叫mp的map类型,并且键值都是int类型
map()函数使用模板:
#include<bits/stdc++.h>
using namespace std;
map<string,int>m;
int main()
{
m["uiui"]=100;
m["kkkkk"]=999;
m["asas"]=78;
m["ns"]=1;
//第一种遍历输出
map<string,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
cout<<"键="<<it->first<<" 值="<<it->second<<endl;
}
cout<<"-----------------------"<<endl;
//第二种遍历输出
for(auto i=m.begin();i!=m.end();i++){
cout<<"键="<<i->first<<" 值="<<i->second<<endl;
}
return 0;
}
map()函数的增删查:
1.增
map<string,int>m;//定义m
1:使用insert添加元素
m.insert(pair<string,int>("sd",19));
2:直接使用数组下标
m["sd"]=19;
2.删
删除map中的数据用到的是erase函数啦,erase里的参数可以直接写键,也可以写迭代器。
erase(m.begin(),m.end());//这句话代表清空m中的内容
#include<bits/stdc++.h>
using namespace std;
map<string,int>m;
int n;
int main()
{
cout<<"请输入要添加的元素个数:";
cin>>n;
for(int i=0;i<n;i++){
string s;
int id;
cout<<"键:";
cin>>s;
cout<<"值:";
cin>>id;
m[s]=id;
}
//第一种遍历输出
map<string,int>::iterator it;
cout<<"map中的键值对如下 :"<<endl;
for(it=m.begin();it!=m.end();it++){
cout<<"键="<<it->first<<" 值="<<it->second<<endl;
}
m.erase("uuu");
map<string,int>::iterator ii;
ii=m.find("opop");
if(ii!=m.end()){
m.erase(ii);//存在opop键,就删除
}
cout<<"map中的键值对如下 :"<<endl;
for(it=m.begin();it!=m.end();it++){
cout<<"键="<<it->first<<" 值="<<it->second<<endl;
}
return 0;
}
3.查
查找数据有两种办法,一个使用find函数还有一个是用count函数(当然了,你查找数据,很明显要查找的,肯定是键吧,没有查找值的吧,哈哈哈)
(1)find函数
find函数查找成功会返回指向它的迭代器,没有找到的话,返回的是end这个迭代器
(2)count函数
count函数的意思就是查找这个键的出现次数,map中键是唯一的,所以它的值要么是0
要么是1,是1不就是查找成功吗,不过它的缺点也可以知道,它可以确定是否存在这个
键,可是却不能确定这个键的位置
#include<bits/stdc++.h>
using namespace std;
map<string,int>m;
int n;
int main()
{
cout<<"请输入要添加的元素个数:";
cin>>n;
for(int i=0;i<n;i++){
string s;
int id;
cout<<"键:";
cin>>s;
cout<<"值:";
cin>>id;
m[s]=id;
}
//第一种遍历输出
map<string,int>::iterator it;
cout<<"map中的键值对如下 :"<<endl;
for(it=m.begin();it!=m.end();it++){
cout<<"键="<<it->first<<" 值="<<it->second<<endl;
}
m.erase("uuu");
map<string,int>::iterator ii;
ii=m.find("opop");
if(ii!=m.end()){
m.erase(ii);//存在opop键,就删除
}
cout<<"map中的键值对如下 :"<<endl;
for(it=m.begin();it!=m.end();it++){
cout<<"键="<<it->first<<" 值="<<it->second<<endl;
}
return 0;
}
方法二:暴力解题
#include <iostream>
#include <string>
using namespace std;
string a, b;
int main() {
getline(cin, a);
cin >> b;
for (auto &i : a) {
bool flag = false;
for (auto &j : b) {
if (i == j) {
flag = true;
}
}
if (flag == false) cout << i;
}
return 0;
}