cctype的用法、count count_if函数用法浅谈

看紫书看到了cctype头文件,因为以前一直用bits 所以对这些不是很了解,为了避免到时候不让用bits 我还是记录一下把!
我在网上找了一下,发现了一篇文章是专门写这个的,看过以后感觉这个头文件非常的有用啊,前几天有个题如果用了isspace 再配合一下模拟就可以很轻松做出来,如果不知道的话就很难判断了 先看看这道题:
2669: 鹦鹉学舌

小明家养了一只鹦鹉,每天小明都训练鹦鹉学说话,经过一段时间训练后,鹦鹉能够说出来比较简短的字词,可是鹦鹉说不了太长的话,每次只能重复小明说的话中最后一个单词。

输入

首先输入若干行字符串(每行不超过100个字符),每行字符串中有若干个单词(至少一个,多个单词之间以空格分隔)。

输出

输出共n行,每行都是小明说的话里最后一个单词。

样例输入
复制样例数据 3
Hello word
You are the best
wonderful

样例输出
word
best
wonderful

这道题的意思是,输入一行句子,然后输出最后一个单词,刚开始没有任何思路啊!问了下同学 他告诉我可以从后往前看一下,然后判断一下有没有空格,如果有的话就输出最后一个单词就行了,我按照这个思路发现写出来以后没法判断只有一个空格的情况,于是上面加了个特判,如果只有一个单词的话就结束程序 代码如下:

#include
using namespace std;
int main() {
  int n;
  int f=1;
  char a[200];
  cin>>n;
  getchar();
  while(n--) {
    f=1;
    int k=0;
    gets(a);
    int l=strlen(a);
    for(int i=0;i<l;i++)
    if(isspace(a[i]))
    k++;
    if(k==0) {
      for(int i=0;i<l;i++)
      cout<<a[i];
      cout<<endl;
      continue;
    }
    for(int i=l-1;i>=0;i--) {
      if(a[i]==' ') {
        for(int j=i+1;j<l;j++)
        cout<<a[j];
        cout<<endl;
        f=0;
        break;
      }
      if(f==0)
      continue;
    }
  }
}

从这题就能看出来isspace 的好用之处,它使得思路很清晰,下面来说一下cctype头文件的用法
1.isalnum() 
检查字符c是十进制数还是大写还是小写字母。如果是,则返回true;如果不是,则返回false#include
#include
using namespace std;
int main() {
  char c;
  cin>>c;
  cout<<isalnum(c);
}
通过对这个的测试我发现一个问题:如果输入的是数字,返回值是4,如果是大写字母,返回值是1,如果是小写字母,返回值
是4,总之他们都是true,如果输入一个·的话返回值是0,即false2.isalpha() 
检查字符c是否是字母。如果是,则返回true;如果不是,则返回false3.isblank() 
检查字符c是否为空白字符。空白字符是用于分隔文本行内的单词的空格字符。如果是,则返回true;如果不是,则返回false4.iscntrl() 
检查c是否是控制字符。控制字符是不占用显示器上打印位置的字符(这与可打印字符相反,用isprint检查)。对于标准ASCII字符集
(由“C”语言环境使用),控制字符是ASCII码0x00(NUL)和0x1f(US)之间加上0x7f(DEL)的字符。

5.isdigit() 
检查字符是否为十进制数字,检查c是否是十进制数字字符。小数位是以下任何一个:0 1 2 3 4 5 6 7 8 9

6.isgraph() 
检查字符是否可以图形表示,检查c是否是具有图形表示的字符。带有图形表示的字符是除了空格字符(”)以外都可以打印的
字符(由isprint确定)。

7.islower() 
检查字符是否为小写字母,检查c是否是小写字母。

请注意,所考虑的字母可能取决于所使用的语言环境; 在默认的“C”语言环境中,小写字母是以下任何一种:a b c d e f g h i j k 
l m n o p q r s t u v w x y z。

其他语言环境可能会将字符的不同选择视为小写字符,但永远不会对iscntrl,isdigit,ispunct或isspace返回true8.isprint() 
检查字符是否可打印,检查c是否是可打印的字符。可打印字符是在显示器上占据打印位置的字符(这与控制字符相反,使用iscntrl
进行检查)。

对于标准ASCII字符集(由“C”语言环境使用),打印字符全部使用大于0x1f(US)的ASCII码,但0x7f(DEL)除外。 
isgraph对于与isprint相同的情况返回true,除了空格字符(”),它在使用isprint检查时返回true,但在使用isgraph检查时
返回false9.ispunct() 
检查字符是否是标点符号,检查c是否是标点符号。标准的“C”语言环境认为标点字符是非字母数字(如isalnum)中
的所有图形字符(如isgraph)。

其他语言环境可能会将不同的字符选择为标点符号,但无论如何它们都是isgraph而不是isalnum。

10.isspace() 
检查字符是否为空白,检查c是否是空格字符。
11.isupper() 
检查字符是否为大写字母,检查参数c是否为大写字母。
1.tolower() 
将字母转换为小写。

2.toupper() 
将字母转换成大写。
这里需要注意的是如果只写这个的话输出的是阿斯克吗:
#include
#include
using namespace std;
int main() {
  char c;
  cin>>c;
  cout<<isalnum(c)<<endl;
  cout<<tolower(c);
}
这样的话输入A输出97 
如果改成这样 就可以输出a
#include
#include
using namespace std;
int main() {
  char c;
  cin>>c;
  cout<<isalnum(c)<<endl;
  cout<<(char)tolower(c);
}


下面开始说一下vector中的count_if
这里说一道题 是紫书上的 uva156
Ananagrams

Most crossword puzzle fans are used to anagrams — groups of words with the same letters in different
orders — for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this
attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think
that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
domain would be the entire English language, but this could lead to some problems. One could restrict
the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative
ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
“rearranged” at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
across lines. Spaces may appear freely around words, and at least one space separates multiple words
on the same line. Note that words that contain the same letters but of differing case are considered to
be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
consisting of a single ‘#’.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
be at least one relative ananagram.
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries

Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon

我是一点都看不懂2333我来翻译一下吧 反片语就是这个单词不能通过单词重新排列,得到输入的文本的另外一个单词,在判断条件的时候,字母不粉大写小写,输出的时候应该保留输入的大小写,按字典序排序(这里就可以用一下sort的字典序排序)

这题基本思路就是先把string类型的输入全部存入vector中,然后设置一个函数,让他们全部变成小写的,然后对每个string单独排
序,这样一来如果出现过的话就可以很轻松的判断出来,然后之后加一个判断条件,由于vector已经提前给存进去了,所以输出的时候
得以用原来的形式输出 思想就是这样 lrj代码:
#include
#include
#include
#include
#include
#include
using namespace std;
int n;
map<string,int>mp;
vector<string>words;
string repr(const string& s) {
  string ans=s;
  for(int i=0;i<ans.size();i++)
  ans[i]=tolower(ans[i]);
  sort(ans.begin(),ans.end());
  return ans;
}
int main() {
  string s;
  while(cin>>s) {
    if(s[0]=='#')
    break;
    words.push_back(s);
    string r=repr(s);
    if(!mp.count(r))//在mp中找一下r
    mp[r]=0;
    mp[r]++;
  }
  vector<string>a;
  for(int i=0;i<words.size();i++)
  if(mp[repr(words[i])]==1)
  a.push_back(words[i]);
  sort(a.begin(),a.end());
  for(int i=0;i<a.size();i++)
  cout<<a[i]<<"\n";
  return 0;
}

这里用到了mp.const的意思就是统计括号内的东西在点前面那个东西里面出现的次数,如果出现过就返回出现过几次,本题用的就是这个特性了。
下面有一段代码,作用是输入n个数,然后输入一个数,从输入的数中看一下出现过多少次:

#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    vector <int> V;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int temp;
        cin>>temp;
        V.push_back(temp);
    }
    int ask;
    while(cin>>ask)
    {
        int num=count(V.begin(),V.end(),ask);
        cout<<num<<endl;
    }
    return 0;
}
eg:输入5 1 2 3 3 5 然后再输入3 就输出2

还有一个函数就是count_if 这里面可以加一个比较函数cmp,然后调用的时候就可以直接加一个参数cmp
这里看到了个写的很好的大佬的博客,于是贴一下,先把他博客贴这里吧大佬博客
讲的是用count_if函数来找到分数大于90的学生的个数

#include 
#include 
#include 
#include 
#include 
using namespace std;
struct student
{
    string name;
    int score;
};
bool compare(student a)
{
    return 90<a.score;
}
int main()
{
    int n;
    cin>>n;
    vector<student> V;
    for(int i=0;i<n;i++)
    {
        student temp;
        cin>>temp.name>>temp.score;
        V.push_back(temp);
    }
    cout<<count_if(V.begin(),V.end(),compare)<<endl;
    return 0;
}

这就是本次总结的所有内容了,谢谢观看2333

你可能感兴趣的:(函数集结)