isalnum的使用
2012-10-06 16:05
99 查看
isalnum的函数功能是指出一个字符在运行时字符集中是否为字母数字。也就是这个函数可以帮我们快速判断一个字符是不是字母或数字,而不是其它标点或字符。
使用isalnum需要包含<local>头文件
isalnum第一个参数为需要判断的字符,第二参数为locale("")
运用这个函数,我们可以完成单词统计功能,在有标点的情况下也不怕
EXAMPLE
#include <iostream>
#include <istream>
#include <ostream>
#include <map>
#include <string>
#include <iomanip>
#include <locale>
int main()
{
using namespace std;
map<string,int> counts;
string word;
//从标准输入读取单词,并计算每个单词现的次数
while(cin >> word)
{
string copy;
for(string::iterator w(word.begin()); w != word.end(); ++w)
if(isalnum(*w,locale())) //判断是否为字母或数字
copy.push_back(*w);
else
{
if(! copy.empty())
++counts[copy];
copy.clear();
}
if(! copy.empty())
++counts[copy];
}
int longest(0);
for(map<string,int>::iterator iter(counts.begin()); iter != counts.end(); ++iter)
{
if(iter->first.size() > longest) //取得单词中最长的一个的长度
longest = iter->first.size();
}
const int count_size(10);
for(map<string,int>::iterator iter(counts.begin()); iter != counts.end(); ++iter)
cout<< setw(longest) << left <<iter->first //以一定的格式输出
<< "\t"
<< setw(count_size) << right << iter->second << "\n";
map<string,int>::iterator the(counts.find("as"));
if(the == counts.end())
cout<< "the not found!\n";
else
{
cout<< "the occurs " << the->second << " times!\n";
}
}
POSSIBLE OUTPUT
使用isalnum需要包含<local>头文件
isalnum第一个参数为需要判断的字符,第二参数为locale("")
运用这个函数,我们可以完成单词统计功能,在有标点的情况下也不怕
EXAMPLE
#include <iostream>
#include <istream>
#include <ostream>
#include <map>
#include <string>
#include <iomanip>
#include <locale>
int main()
{
using namespace std;
map<string,int> counts;
string word;
//从标准输入读取单词,并计算每个单词现的次数
while(cin >> word)
{
string copy;
for(string::iterator w(word.begin()); w != word.end(); ++w)
if(isalnum(*w,locale())) //判断是否为字母或数字
copy.push_back(*w);
else
{
if(! copy.empty())
++counts[copy];
copy.clear();
}
if(! copy.empty())
++counts[copy];
}
int longest(0);
for(map<string,int>::iterator iter(counts.begin()); iter != counts.end(); ++iter)
{
if(iter->first.size() > longest) //取得单词中最长的一个的长度
longest = iter->first.size();
}
const int count_size(10);
for(map<string,int>::iterator iter(counts.begin()); iter != counts.end(); ++iter)
cout<< setw(longest) << left <<iter->first //以一定的格式输出
<< "\t"
<< setw(count_size) << right << iter->second << "\n";
map<string,int>::iterator the(counts.find("as"));
if(the == counts.end())
cout<< "the not found!\n";
else
{
cout<< "the occurs " << the->second << " times!\n";
}
}
POSSIBLE OUTPUT
相关文章推荐
- 探究Python中isalnum()方法的使用
- C语言中isalnum()函数和isalpha()函数的对比使用
- 探究Python中isalnum()方法的使用
- 14.4 exportfs命令;14.5 NFS客户端问题;15.1 FTP介绍;15.2/15.3 使用vsftpd搭建ftp
- 实验4 [bx]和loop的使用
- 欢迎使用CSDN-markdown编辑器
- xshell配置ssh使用密钥公钥(publice key)登录
- 使用maven profile实现多环境可移植构建
- Linux编译OpenCV3.2.0-OCL模块并使用
- ATC:go logs 使用
- flot - jQuery 图表插件(jquery.flot)使用
- 在SSH框架中,使用Spring设置定时器Quartz来开启定时任务
- IDEA的git使用心得
- cocos2dx ImageView使用
- 使用R语言画三维图
- Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架
- Linux使用NFS服务实现远程共享
- npm使用sinopia搭建私服---错误
- 关于使用FreeMarker如何做到在浏览器弹框选择地址?
- 180122 逆向-Frida在Windows下的使用