编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
输入描述:输入N个字符,字符在ACSII码范围内。
输出描述:输出范围在(0~127)字符的个数。
方法一:利用set容器。
#include<iostream>
#include<set>
using namespace std;
int main()
{
char c;
set<char> s;
while(cin>>c){
if(c>=0 && c<=127){
s.insert(c);
}
}
cout << s.size() <<endl;
}
方法二:使用数组实现hash表
#include<iostream>
using namespace std;
int main(){
char ch;
int arr[128]={0};
int count=0;
while(cin>>ch){
if(ch>=0 && ch<=127){
arr[ch]++;
}
}
for(int i=0;i<128;i++){
if(arr[i]>0)
count++;
}
cout<<count<<endl;
return 0;
}