《CPlusPlusPrimer》第十二章二节编程源码——vocab功能的源码
来源:互联网 发布:linux删除虚拟网卡 编辑:程序博客网 时间:2024/05/20 06:51
用于练习泛型算法。
程序源码:
// 练习C++Primer Chapter12.2节中的泛型算法#include <iostream>#include <fstream>#include <algorithm>#include <vector>#include <string>using namespace std;typedef vector<string> textwords;typedef vector<string>::iterator VEC_STR_IT;class PrintElem{int line_len;int cnt;public:PrintElem(int len = 8): line_len(len), cnt(0) {}void operator()(const string & elem){cnt++;cout << elem << " ";if( cnt % line_len == 0){cout << '\n';}}};class LessThan{public:bool operator()(const string &str1, const string &str2){// OK,必须用size,而非length.return (str1.size() < str2.size());}};class GreatThan{int _len;public:GreatThan(int len = 6) : _len(len) {}bool operator() ( const string & s){return (s.size() > _len);}};void proccess_vocab(const vector<textwords> &vecs){vector<string> texts;vector<textwords>::const_iterator iter = vecs.begin();for( ; iter != vecs.end(); iter++ ){copy( (*iter).begin(), (*iter).end(), back_inserter( texts ));}sort( texts.begin(), texts.end() );for_each( texts.begin(), texts.end(), PrintElem());cout << "\n\n";// 消除相同词VEC_STR_IT it;it = unique( texts.begin(), texts.end());texts.erase(it, texts.end());for_each( texts.begin(), texts.end(), PrintElem());cout << "\n\n";// 根据自符串长度排序stable_sort( texts.begin(), texts.end(), LessThan());for_each( texts.begin(), texts.end(), PrintElem());cout << "\n\n";// 计算长度大于6的个数int cnt = count_if( texts.begin(), texts.end(), GreatThan());cout << "the count which's size is great than 6 is: " << cnt << endl;static string rw[] = {"and", "if", "or", "but", "the"};vector<string> remove_words ( rw, rw + 5);VEC_STR_IT it2 = remove_words.begin();for( ; it2 != remove_words.end(); it2++ ){int cnt = count ( texts.begin(), texts.end(), *it2);cout << cnt << " instances removed: " << (*it2) << endl;texts.erase(remove(texts.begin(), texts.end(), (*it2)),texts.end());}cout << "\n\n";for_each( texts.begin(), texts.end(), PrintElem());}int main( char* args[], int argc){vector<textwords> sample;vector<string> vec1, vec2;const string inf1 = "chapter12_vocab_file1.txt", inf2 = "chapter12_vocab_file1.txt";using std::ifstream;ifstream infile1(inf1.c_str());ifstream infile2(inf2.c_str());istream_iterator<string> input_set1(infile1), eos;istream_iterator<string> input_set2(infile2);copy( input_set1, eos, back_inserter(vec1));copy(input_set2, eos, back_inserter(vec2));sample.push_back( vec1 );sample.push_back( vec2 );proccess_vocab(sample);return 0;}
运行部分截图:
- 《CPlusPlusPrimer》第十二章二节编程源码——vocab功能的源码
- 《CPlusPlusPrimer》第三章编程源码——String类的简单实现
- 《CPlusPlusPrimer》第四章编程源码——iStack类的简单实现
- 《CPlusPlusPrimer》第五章编程源码——list类的简单实现
- 《CPlusPlusPrimer》第二章编程源码——Array模板简单实现
- Irrlicht引擎源码剖析——第十二天
- 购物车的功能——界面源码
- 购物车的功能——CSS源码
- 购物车的功能——JS源码
- Android开发——常用功能源码
- 功能源码源码列表,Asp.net源码|—51aspx.com
- 微信群控系统源码的功能
- 第十二章:12.3 源码包服务的管理
- java并发编程——ReentrantLock源码
- YARN编程实例—distributedshell源码分析
- YARN编程实例—distributedshell源码分析
- YARN编程实例—distributedshell源码分析
- FAAC源码阅读(5)——AAC编码模块的功能
- Case when 与Update语句的使用
- (虚幻3游戏引擎)Unreal3的Flash移植演讲代码示例!
- php 获取日期(当天,前天,明天,本周,本月,本季度,本年)
- php读取zip文件中的图片,并动态更换div的背景图片
- LInux配置ANDROID环境
- 《CPlusPlusPrimer》第十二章二节编程源码——vocab功能的源码
- 关于Flash Player 11(Molehill)的3D引擎、例子、学习资料
- 取得trace 文件名字
- Glib事件源
- php中使用head进行二进制流输出,让用户下载PDF等附件的方法
- Android提高篇之listView点击button翻页功能实现
- ContextLoader-api
- log4net使用笔记(winform程序)
- 使用Innosetup制作安装包的一些技巧