UVa10815 Andy's First Dictionary

题意:输入一个文本,找出所有不同的单词(连续字母序列),按字典序从小到大输出。单词不分大小写。

#include<string>
#include<set>
#include<sstream>
#include<iostream>
using namespace std;set<string> dict;
string s, buf;int main()
{while(cin >> s) {for(int i = 0; i < s.length(); i++)if(isalpha(s[i])) s[i] = tolower(s[i]);  else s[i] = ' ';stringstream ss(s);while(ss >> buf) dict.insert(buf);}for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)cout << *it << "\n";return 0;
}

isalpha

函数名称: isalpha

函数原型: int isalpha(char ch);

函数功能: 检查ch是否是字母.

函数返回: 是字母返回非0(在vs2015中为2) ,否则返回 0.

参数说明:

所属文件 <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

int main()

{

    char ch1='*';

    char ch2='a';

    if(isalpha(ch1)!=0)

        printf("%c is the ASCII alphabet\n",ch1);

    else

        printf("%c is not the ASCII alphabet\n",ch1);

    if(isalnum(ch2)!=0)

        printf("%c is the ASCII alphabet\n",ch2);

    else

        printf("%c is not the ASCII alphabet\n",ch2);

    return0;

}

iscntrl

函数名称: iscntrl

函数原型: int iscntrl(int ch);

函数功能: 检查ch是否控制字符(其ASCII码在0和0x1F之间,数值为 0-31).

函数返回: 是返回非0,否则返回 0.

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include<stdio.h>

#include<ctype.h>

char chars[]={'A',0x09,'Z'};

#define SIZE sizeof(chars)/sizeof(char)

int main()

{

    int i;

    for(i=0;i<SIZE;i++)

    {

        printf("Char%cis%saControlcharacter\n",

        chars[i],(iscntrl(chars[i]))?"":"not");

    }

    return 0;

}

isdigit

函数名称: isdigit

函数原型: int isdigit(char ch);

函数功能: 检查ch是否是数字(0-9)

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

int main()

{

char ch1='1';

char ch2='a';

if(isdigit(ch1)!=0)

printf("%c is the ASCII number\n",ch1);

else

printf("%c is not the ASCII number\n",ch1);

if(isdigit(ch2)!=0)

printf("%c is the ASCII number\n",ch2);

else

printf("%c is not the ASCII number\n",ch2);

return0;

}

[1]

isgraph

函数名称: isgraph

函数原型: int isgraph(int ch);

函数功能: 检查ch是否可显示字符(其ASCII码在0x21到0x7E之间),不包括空格

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

int main()

{

charch1='';

charch2='a';

if(isgraph(ch1)!=0)

printf("%cistheASCIIprintablecharacter\n",ch1);

else

printf("%cisnottheASCIIprintablecharacter\n",ch1);

if(isgraph(ch2)!=0)

printf("%cistheASCIIprintablecharacter\n",ch2);

else

printf("%cisnottheASCIIprintablecharacter\n",ch2);

return0;

}

islower

函数名称: islower

函数原型: int islower(int ch);

函数功能: 检查ch是否小写字母(a-z)

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

#include<stdio.h>

#include<ctype.h>

charchars[]={'A','a','z','Z'};

#defineSIZEsizeof(chars)/sizeof(char)

int main()

{

int i;

for(i=0;i<SIZE;i++){

printf("Char%cis%salowercasecharacter\n",chars[i],(islower(chars[i]))?"":"not");

}

return0;

}

isupper

函数名称: isupper

函数原型: int isupper(int ch);

函数功能: 检查ch是否是大写字母(A-Z)

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

#include<stdio.h>

#include<ctype.h>

charchars[]={'A','a','z','Z'};

#defineSIZEsizeof(chars)/sizeof(char)

int main()

{

inti;

for(i=0;i<SIZE;i++){

printf("Char%cis%sanuppercasecharacter\n",

chars[i],(isupper(chars[i]))?"":"not");

}

return0;

}

tolower

函数名称: tolower

函数原型: int tolower(int ch);

函数功能: 将ch字符转换为小写字母

函数返回: 返回ch所代表的字符的小写字母

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

intmain()

{

charx='a',y='b',z='A',w='*';

printf("Character%ctoloweris%c\n",x,tolower(x));

printf("Character%ctoloweris%c\n",y,tolower(y));

printf("Character%ctoloweris%c\n",z,tolower(z));

printf("Character%ctoloweris%c\n",w,tolower(w));

return0;

}

toupper

函数名称: toupper

函数原型: int toupper(int ch);

函数功能: 将ch字符转换成大写字母

函数返回: 与ch相应的大写字母

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

int main()

{

charx='a',y='b',z='A',w='*';

printf("Character%ctoupperis%c\n",x,toupper(x));

printf("Character%ctoupperis%c\n",y,toupper(y));

printf("Character%ctoupperis%c\n",z,toupper(z));

printf("Character%ctoupperis%c\n",w,toupper(w));

return0;

}

isalnum

函数名称: isalnum

函数原型: int isalnum(int ch);

函数功能: 检查ch是否是字母或数字

函数返回: 是字母或数字返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

intmain()

{

charch1='*';

charch2='a';

if(isalnum(ch1)!=0)

printf("%cistheASCIInumberoralphebet\n",ch1);

else

printf("%cisnottheASCIInumbernoralphebet\n",ch1);

if(isalnum(ch2)!=0)

printf("%cistheASCIInumberoralphebet\n",ch2);

else

printf("%cisnottheASCIInumbernoralphebet\n",ch2);

return0;

}

isprint

函数名称: isprint

函数原型: int isprint(int ch);

函数功能: 检查ch是否是可打印字符(包括空格),其ASCII码在0x20到0x7E之间

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

intmain()

{

charch1='\n';

charch2='a';

if(isprint(ch1)!=0)

printf("%cistheASCIIprintablecharcater\n",ch1);

else

printf("%cisnottheASCIIprintablecharcater\n",ch1);

if(isprint(ch2)!=0)

printf("%cistheASCIIprintablecharcater\n",ch2);

else

printf("%cisnottheASCIIprintablecharcater\n",ch2);

return0;

}

ispunct

函数名称: ispunct

函数原型: int ispunct(int ch);

函数功能: 检查ch是否是标点字符(不包括空格),即除字母,数字和空格以外的所有可打印字符

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

intmain()

{

charch1=',';

charch2='a';

if(ispunct(ch1)!=0)

printf("%cistheASCIIpunct\n",ch1);

else

printf("%cisnottheASCIIpunct\n",ch1);

if(ispunct(ch2)!=0)

printf("%cistheASCIIpunct\n",ch2);

else

printf("%cisnottheASCIIpunct\n",ch2);

return0;

}

isspace

函数名称: isspace

函数原型: int isspace(int ch);

函数功能: 检查ch是否是空格符和跳格符(控制字符)或换行符

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

intmain()

{

charch1='';

charch2='a';

if(isspace(ch1)!=0)

printf("%cisthespacecharcater\n",ch1);

else

printf("%cisnotthespacecharcater\n",ch1);

if(isspace(ch2)!=0)

printf("%cisthespacecharcater\n",ch2);

else

printf("%cisnotthespacecharcater\n",ch2);

return0;

}

isxdigit

函数名称: isxdigit

函数原型: int isxdigit(int ch);

函数功能: 检查ch是否是一个16进制数学字符(即0-9,或A-F,或a-f)

函数返回: 是返回非0,否则返回0

参数说明:

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include<stdio.h>

#include<ctype.h>

intmain()

{

charch1='1';

charch2='a';

if(isxdigit(ch1)!=0)

printf("%cistheASCIIhexadecimalnumber\n",ch1);

else

printf("%cisnottheASCIIhexadecimalnumber\n",ch1);

if(isxdigit(ch2)!=0)

printf("%cistheASCIIhexadecimalnumber\n",ch2);

else

printf("%cisnottheASCIIhexadecimalnumber\n",ch2);

return0;

}

isascii

函数名称: isascii

函数原型: int isascii(int ch)

函数功能: 测试参数是否是ASCII码0-127

函数返回: 是返回非0,否则返回0

参数说明: ch-被测参数

所属文件: <ctype.h>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include<stdio.h>

#include<ctype.h>

charchars[]={'A',0x80,'Z'};

#defineSIZEsizeof(chars)/sizeof(char)

intmain()

{

inti;

for(i=0;i<SIZE;i++)

{

printf("Char%cis%sanASCIIcharacter\n",

chars[i],(isascii(chars[i]))?"":"not");

}

return0;

}

stringstream是字符串流。它将流与存储在内存中的string对象绑定起来。

在多种数据类型之间实现自动格式化。

1 stringstream对象的使用

#include<sstream>
#include<iostream>
using namespace std;
int main()
{string line,word;while(getline(cin,line)){stringstream stream(line);cout<<stream.str()<<endl;while(stream>>word){cout<<word<<endl;}}return 0;
}

输入:shanghai no1 school 1989

输出:shanghi no1 school 1989

      shanghai

    no1

    school

    1989

2stringstream提供的转换和格式化

程序:

#include<sstream>
#include<iostream>
using namespace std;
int main()
{int val1 = 512,val2 =1024;stringstream ss;ss<<"val1: "<<val1<<endl          //“val1: "此处有空格,字符串流是通过空格判断一个字符串的结束<<"val2: "<<val2<<endl;cout<<ss.str();string dump;int a,b;ss>>dump>>a>>dump>>b;cout<<a<<" "<<b<<endl;return 0;
}

输出为:val1: 512

    val2: 1024

    512 1024

第一处黑体字部分:将int类型读入ss,变为string类型

第二处黑体字部分:提取512,1024保存为int类型。当然,如果a,b声明为string类型,那么这两个字面值常量相应保存为string类型

3其他注意

stringstream不会主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消 耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str("") )

#include <cstdlib>
#include<iostream>
#include<sstream>
using namespace std;
int main()
{stringstream ss;string s;ss<<"shanghai no1 school";ss>>s;cout<<"size of stream = "<<ss.str().length()<<endl;cout<<"s: "<<s<<endl;ss.str("");cout<<"size of stream = "<<ss.str().length()<<endl;
}

输出:

size of stream = 19
s: shanghai
size of stream = 0

《STL》— UVa10815 Andy's First Dictionary相关推荐

  1. 算法竞赛入门经典(第二版) | 例题5-3 安迪的第一个字典 (紫书牛啤!)(UVa10815,Andy's First Dictionary)

    概述: 输入一个文本,找出所有不同的单词,按字典序排序,去重后,输出,单词不分大小写. 储备知识: 1.sstring头文件的用法→sstring头文件函数详解 2.cctype头文件的用法→ccty ...

  2. 《STL》— NYOJ 55 懒省事的小明

    题目55 题目信息 运行结果 本题排行 讨论区 懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的 ...

  3. 10815 - Andy's First Dictionary

    Andy's First Dictionary PS:因为该题排版较麻烦,这里给出OJ网址:UVa10815 - Andy's First Dictionary 输入一个文本,找出所有不同的单词(连续 ...

  4. 《STL源码剖析》学习--六大组件

    stl 提供了六大组件,分别为:容器.算法.迭代器.仿函数.适配器和配置器. 容器通过配置器取得数据存储空间,算法通过迭代器存取容器的内容,仿函数可以协助算法完成不同的策略,配接器可以修饰或者嵌套仿函 ...

  5. 《STL源码剖析》学习-- 1.9-- 可能令你困惑的C++语法1

    最近在看侯捷的<STL源码剖析>,虽然感觉自己c++看得比较深一点,还是感觉还多东西不是那么明白,这里将一些细小的东西或者概念记录一下. 有些东西是根据<C++编程思想>理解的 ...

  6. 《STL源码剖析》学习--6章--_rotate算法分析

     最近在看侯捷的<STL源码剖析>,其中有许多不太明白之处,后经分析或查找资料有了些理解,现记录一下. <STL源码剖析>学习--6章--random access ite ...

  7. 《STL源码剖析》学习--6章--power算法分析

    最近在看侯捷的<STL源码剖析>,其中有许多不太明白之处,后经分析或查找资料有了些理解,现记录一下. 6章--power算法分析 书本中的算法如下所示: template <clas ...

  8. 《Effective STL》学习笔记(第一部分)

    本书从STL应用出发,介绍了在项目中应该怎样正确高效的使用STL.本书共有7个小节50个条款,分别为 (1) 容器:占12个条款,主要介绍了所有容器的共同指导法则 (2) vector和string: ...

  9. 《STL源码剖析常见面试问题》

    1.  当vector的内存用完了,它是如何动态扩展内存的?它是怎么释放内存的?用clear可以释放掉内存吗?是不是线程安全的? (1). vector内存用完了,会以当前size大小重新申请2*si ...

最新文章

  1. 微博达人硅谷之歌:Testin云測移动搜索性能測试非常是让人信服
  2. 开需求评审会,你会出汗吗?
  3. 51nod 1847 奇怪的数学题(数论/min25筛/杜教筛/斯特林数)
  4. 在Android中显示GIF动画
  5. STM32F103单片机RTC实时时钟的使用
  6. 转:在Linux中Oracle安装成功后,首次启动使用时,会出现的一些问题总结和解决办法...
  7. 腾讯笔试题编程题——纸牌游戏
  8. 怎么裁剪PDF页面,PDF如何调整页面大小
  9. 使用FFmpeg进行摄像头视频采集
  10. 让python pip使用国内镜像安装模块
  11. base64和jpg/png互转
  12. ip地址、DNS服务器、子网掩码、默认网关之间关系
  13. paip 破解网站手机验证码
  14. slite的一些基本介绍
  15. MINIO(一)简介
  16. 创建虚拟机、安装centos6,centos7系统,图形化界面
  17. 神秘鸭,让语音操作电脑不再神秘 小爱同学
  18. 22届春季校招实习试水之路2(前端)
  19. 前端学习笔记 - 用CSS实现一个背景色为红色,半径为200px的圆,并设置不停的上下移动动画
  20. Flex/FlashBuilder4.5破解

热门文章

  1. ffmpeg 编译 for android
  2. Javacript中父节点、子节点、兄节点的简单用法[0306]
  3. 使用自己的INDEMIND相机来运行ORBSLAM2单目,双目和深度模式(小觅相机和realsense通用)
  4. IT修真院 Task1全资料[Java篇]
  5. 获取seekbar thumbar位置_运汽车-牡丹江到昆明汽车托运公司-查看位置
  6. tabbar图片位置大小修改
  7. 对脏写、脏读、不可重复度、幻读的理解笔记
  8. 【运筹学】线性规划 人工变量法 阶段总结 ( 使用 人工变量法 求解 线性规划 全过程详细解析 ) ★
  9. windows10突然连不上打印机问题
  10. 多张表合并一张表union all