C++Primer6.8节可执行console程序代码

  1. #pragma   warning(disable:4786)
  2. #include <string>
  3. #include <iostream>
  4. #include <fstream> 
  5. #include <vector>
  6. using namespace std;
  7. typedef pair<short,short> location;
  8. typedef vector<location> loc;
  9. typedef vector<string> text;
  10. typedef pair<text*,loc*> text_loc;
  11. // 返回值是指向string vector 的指针
  12. text*
  13. retrieve_text()
  14. {
  15.     string file_name;
  16.     cout << "please enter file name: ";
  17.     cin >> file_name;
  18.     // 打开文本文件以便输入 ...
  19.     ifstream infile( file_name.c_str(), ios::in );
  20.     if ( ! infile )
  21.     {
  22.         cerr << "oops! unable to open file "
  23.         << file_name << " -- bailing out!/n";
  24.         exit( -1 );
  25.     }
  26.     else cout << '/n';
  27.     text *lines_of_text = new text;
  28.     string textline;
  29.     typedef pair<string::size_type, int> stats;
  30.     stats maxline;
  31.     int linenum = 0;
  32.     while ( getline( infile, textline, '/n' )) 
  33.     {
  34.         cout << "line read: " << textline << '/n';
  35.         if ( maxline.first < textline.size() ) 
  36.         {
  37.             maxline.first = textline.size();
  38.             maxline.second = linenum;
  39.         }
  40.         lines_of_text->push_back( textline );
  41.         linenum++;
  42.     }
  43.     cout<<"The max_line size is "<<maxline.first<<";"<<endl;
  44.     cout<<"The max_line_num is "<<maxline.second<<";"<<endl;
  45.     return lines_of_text;
  46. }
  47. text_loc*
  48. separate_words( const text *text_file )
  49. {
  50.     // words: 包含独立单词的集合
  51.     // locations: 包含相关的行/列信息
  52.     vector<string> *words = new vector<string>;
  53.     vector<location> *locations = new vector<location>;
  54.     short line_pos = 0; // current line number
  55.     // iterate through each line of text
  56.     for ( ; line_pos < text_file->size(); ++line_pos )
  57.     {
  58.         // textline: 待处理的当前文本行
  59.         // word_pos: 文本行中的当前列位置
  60.         short word_pos = 0;
  61.         string textline = (*text_file)[ line_pos ];
  62.         string::size_type pos = 0, prev_pos = 0;
  63.         while (( pos = textline.find_first_of( ' ', pos ))
  64.         != string::npos )
  65.         {
  66.             // 存储当前单词子串的拷贝
  67.             words->push_back(
  68.             textline.substr( prev_pos, pos - prev_pos ));
  69.             // 将行/列信息存储为pair
  70.             locations ->push_back(
  71.             make_pair( line_pos, word_pos ));
  72.             // 为下一次迭代修改位置信息
  73.             prev_pos = ++pos;
  74.             word_pos = pos;
  75.         }
  76.         // 现在处理最后一个单词
  77.         words->push_back(
  78.         textline.substr( prev_pos, pos - prev_pos ));
  79.         locations->push_back(
  80.         make_pair( line_pos, word_pos ));
  81.     }
  82.     return new text_loc( words, locations );
  83. }
  84. void main()
  85. {
  86.     text* txt_file = retrieve_text();
  87.     text_loc* txt_loc_map = separate_words(txt_file);
  88.     text::iterator iter = (txt_loc_map->first)->begin();
  89.     loc::iterator iter2 = (txt_loc_map->second)->begin();
  90.     for(;iter != (txt_loc_map->first)->end(); iter++,iter2++)
  91.     {
  92.         cout<<*iter<<" ("<<iter2->first<<","<<iter2->second<<");"<<endl;
  93.     }
  94. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值