isalnum的使用
来源:互联网 发布:人口数据 昆明 编辑:程序博客网 时间:2024/04/29 13:09
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的使用
- isalnum
- isalnum
- isalnum
- isalnum
- isalnum() 函数
- isalnum,isalpha,isdigit.....isxdigit,ispunt等字符函数的检测程序
- isalnum <ctype.h> <cctype>
- isalnum()和isalpha()
- isalnum ( )【C语言库函数源代码】
- ctype库中的函数isalnum
- [C-Library]ctype.h - isalnum()
- isalnum字符串测试函数应用实例
- C语言isalnum()函数:判断字符是否为英文字母或数字
- C语言字符检测函数:isalnum、isalpha、isascii、iscntrl、isdigit、isgraph、islower、isspace、ispunct、isupper
- 【C++】isalpha、islower、isupper、isalnum、isblank、isspace函数<cctype>头文件
- C语言isalnum()函数:判断字符是否为英文字母或数字
- GUID使用的使用
- 内核同步机制-读写信号量(rw_semaphore)
- 情不知所起,一往而深
- [转]Vim 复制粘贴探秘
- shell语法之变量的使用
- c++解析excel的方法(tinyXml)
- isalnum的使用
- ruby学习之线程
- java 01环境搭建及注解
- 大数加法
- IEInspector.HTTP.Analyzer.Full.Edition.v7.0.1.418.Incl.Keymaker-ZWT
- 理解sizeof()
- 香港的一個Linux聚會
- 内核同步机制-信号量(semaphore)
- C关键字volatile详解