Andy's First Dictionary

  • isalpha©~判断是否为英文字符
  • C在ctype.h(C++中是cctype)库文件下定义的函tolower函数实现字母的大小写转换,
    函数实现原型如下:
int tolower(int c){
 if ((c >= 'A') && (c <= 'Z'))  
 return c + ('a' - 'A'); r
 eturn c;
}
  • C++中sstream库文件下定义的stringstream对象(用法)用于输入一行字符串,以 空格 为分隔符把该行分隔开来
 string str= "hello world I am very happy!"; 
 stringstream sstream(str);  //sstream<<   
  while (sstream)      {    
     string substr; 
     sstream>>substr;
     cout << substr << endl; //也可vec.push_back(substr);    
 } 
  • C++中set解释 (详解
    每个元素最多出现一次,并在内部会进行排序
    输入:set< int > s
    s.insert(item);
    注意可以重复
    输出只能使用迭代器
    .count()返回数量
    .find(k)如果容器中存在按k索引的元素,则返回指向该元素的迭代器,如果不存在返回超出末端迭代器
    .erase(k)删除容器中键为k的元素
#include<set>    
#include<iostream>    
using namespace std;
int main()
{
    set<int>s;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
 {
        int x;
        cin>>x;
        s.insert (x);
 }
 set<int>::iterator it;
    for(it=s.begin ();it!=s.end ();it++)
    {
        printf("%d\n",*it); 
    }
//s.end()没有值
     cout<<"s.begain()   "<<*s.begin ()<<endl;
    //lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器
    cout<<"lower_buond  3  "<<*s.lower_bound (3)<<endl;
//upper_bound()--返回大于某个值元素的迭代器
    cout<<"upper_bound  3  "<<*s.upper_bound (3)<<endl;
 //find()--返回一个指向被查找到元素的迭代器
    cout<<"find()  3   "<<*s.find (3)<<endl;
 cout<<"s.size()  "<<s.size ()<<endl;
    return 0;
}

运行结果图为:

在这里插入图片描述

例题 Andy’s First Dictionary

#include <iostream> 
#include <string>
#include <algorithm> 
#include <set> 
#include <sstream> 
using namespace std; 
int main()
{
    string s, buf;
    set<string>a;
    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)
	        {
	            a.insert(buf);
	        }  
   }
	for(set<string>::iterator it= a.begin();it!=a.end();it++)
	{
	 cout<<*it<<endl;
	}
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值