查找单词的题目,很有意思,我提供了两个版本
2012-07-03 10:32
239 查看
//Exercise //9.39: //已知有如下 string 对象:string line1 = "We were her pride of 10 she named us:"; string line2 = "Benjamin, Phoenix, the Prodigal" string line3 = "and perspicacious pacific Suzanne"; string sentence = line1 + ' ' + line2 + ' ' + line3; //编写程序计算 sentence 中有多少个单词,并指出其中最长和最短的单词。如果有多个最长或最短的单词,则将它们全部输出。 //版本一: #include <iostream> #include <string> #include <vector> #include <sstream> using namespace std; int main() { string line1="we are her pride of 10 she named us:"; string line2="benjamin, phoenix, the prodigal"; string line3="and perspicacious pacific suzanen"; string sentence=line1+" "+line2+" "+line3; vector<string> longest_word,smallest_word; unsigned int count=0; string::size_type s_size_big=0,s_size_small=0; string word; istringstream in_stream(sentence); cout<<"The sentence is :"<<endl; while(in_stream>>word) { count++; cout<<word<<" "; string::size_type size_tmp=word.size(); if(count==1) { s_size_big=s_size_small=size_tmp; longest_word.push_back(word); smallest_word.push_back(word); } else { if(size_tmp>s_size_big) { s_size_big=size_tmp; longest_word.clear(); longest_word.push_back(word); } else { if(size_tmp==s_size_big) { longest_word.push_back(word); } else { if(size_tmp<s_size_small) { s_size_small=size_tmp; smallest_word.clear(); smallest_word.push_back(word); } else { if(size_tmp==s_size_small) smallest_word.push_back(word); } } } } } cout<<endl; cout<<"The total number of the words : "<<count<<endl; cout<<"The longest word is:"<<endl; for(vector<string>::iterator big_iter=longest_word.begin();big_iter!=longest_word.end(); big_iter++) cout<<*big_iter<<" "; cout<<endl; cout<<"The smallest word is:"<<endl; for(vector<string>::iterator small_iter=smallest_word.begin();small_iter!=smallest_word.end(); small_iter++) cout<<*small_iter<<" "; cout<<endl; } //下面是运用find_first_of和find_first_not_of的版本: /*在答案书的答案上,那个答案是错的!*/ #include <iostream> #include <string> #include <vector> using namespace std; int main() { string line1="we are her pride of 10 she named us:"; string line2="benjamin, phoenix, the prodigal"; string line3="and perspicacious pacific suzanen"; string spaces(" \t:,\n\v\r\f");//后面都一些转义字符 string sentence=line1+" "+line2+" "+line3; vector<string> longest_word,smallest_word; unsigned int count=0; string word; string::size_type s_size_big=0,s_size_small=0; string::size_type start_pos=0, end_pos=0; while((start_pos=sentence.find_first_not_of(spaces,end_pos))!=string::npos) { ++count; string::size_type size_tmp; end_pos=sentence.find_first_of(spaces,start_pos); //这个是代码的核心部分 if(end_pos==string::npos)//如果没有匹配的话就返回npos,这是一个非常大的数,比任何的合法的下标都大 size_tmp=sentence.size()-start_pos; else size_tmp=end_pos-start_pos; word.assign(sentence,start_pos,size_tmp); cout<<"the size of sentence: "<<sentence.size()<<endl; cout<<word<<" "<<"length:"<<size_tmp<<endl; if(count==1) { s_size_big=s_size_small=size_tmp; longest_word.push_back(word); smallest_word.push_back(word); } else { if(size_tmp>s_size_big)
相关文章推荐
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- Cracking the coding interview: 查找文中两个单词的距离
- 构造一个单词查找树,支持以下两个操作的数据结构: 添加和搜索,只包含字母a-z或.。
- 今天看到的两个算法题 ,第一次做这种题目,希望大家提供更好的思路!!
- 0904_两个小题目_二分查找和杨氏矩阵查找
- 今天看到的两个算法题 ,第一次做这种题目,希望大家提供更好的思路!!
- 二分查找模板总结(递归与循环遍历两个版本)
- 用MASM32写了一个查找替换的字串函数!ANSI及UNICODE两个版本
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- C和指针 第六章 指针6.2 6.3字符串中查找的两个版本
- POJ的题目分类(两个版本)
- 题目1111:单词替换(字符串查找)
- 查找两个文件中相同的单词stl
- [算法 笔记] 查找二叉树上任意两个结点的最近共同祖先(更新版本)
- [Java]关于Null面对两个重载构造的选择的小题目
- 题目1361:翻转单词顺序
- emWin洗衣机简易操作界面,含uCOS-III和FreeRTOS两个版本
- 【题目27】求两个排序数组的交集
- 题目1384:二维数组中的查找-九度
- 从一个NOI题目再学习二分查找。