青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 70  文章 - 160  trackbacks - 0

公告:
知識共享許可協議
本博客采用知識共享署名 2.5 中國大陸許可協議進行許可。本博客版權歸作者所有,歡迎轉載,但未經作者同意不得隨機刪除文章任何內容,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 具體操作方式可參考此處。如您有任何疑問或者授權方面的協商,請給我留言。

常用鏈接

留言簿(8)

隨筆檔案

文章檔案

搜索

  •  

積分與排名

  • 積分 - 180080
  • 排名 - 147

最新評論

閱讀排行榜

評論排行榜

這是在《C++ Primer》上第十章最后的一個小節。以前把這里漏掉了,剛才看了下,覺得這個程序很不錯,便于對vector, map, set的基本掌握。特地把這一個小程序記錄下來。

/*
 *目的:一個簡單的文本查詢程序
 *作用:程序將讀取用戶指定的任意文本文件,然后允許用戶從該文件中查找單詞。
 *查詢的結果是該單詞出現的次數,并列出每次出現所在的行。
 *如果某單詞在同一行中多次出現,程序將只顯示該行一次。
 *行號按升序顯示,即第 7 行應該在第 9 行之前輸出,依此類推。
 
*/

/*思路:
 *1.使用一個 vector<string> 類型的對象存儲整個輸入文件的副本。
 *   輸入文件的每一行是該 vector 對象的一個元素。
 *   因而,在希望輸出某一行時,只需以行號為下標獲取該行所在的元素即可。
 *2.將每個單詞所在的行號存儲在一個 set 容器對象中。
 *   使用 set 就可確保每行只有一個條目,而且行號將自動按升序排列。
 *3.使用一個 map 容器將每個單詞與一個 set 容器對象關聯起來,
 *   該 set 容器對象記錄此單詞所在的行號。
 
*/

TextQuery.H文件

#ifndef TEXTQUERY_H
#define TEXTQUERY_H
#include 
<string>
#include 
<vector>
#include 
<map>
#include 
<set>
#include 
<iostream>
#include 
<fstream>
#include 
<cctype>
#include 
<cstring>
 
class TextQuery {
    
// as before
public:
    
// typedef to make declarations easier
    typedef std::string::size_type str_size;
    typedef std::vector
<std::string>::size_type line_no;
 
    
/* interface:
     *    read_file builds internal data structures for the given file
     *    run_query finds the given word and returns set of lines on which it appears
     *    text_line returns a requested line from the input file
    
*/
    
void read_file(std::ifstream &is
               { store_file(
is); build_map(); }
    std::
set<line_no> run_query(const std::string&const
    std::
string text_line(line_no) const;
    str_size size() 
const { return lines_of_text.size(); }
    
void display_map();        // debugging aid: print the map
 
private:
    
// utility functions used by read_file
    void store_file(std::ifstream&); // store input file
    void build_map(); // associated each word with a set of line numbers
 
    
// remember the whole input file
    std::vector<std::string> lines_of_text; 
 
    
// map word to set of the lines on which it occurs
    std::map< std::string, std::set<line_no> > word_map;  
    
// characters that constitute whitespace
    static std::string whitespace_chars;     
    
// canonicalizes text: removes punctuation and makes everything lower case
    static std::string cleanup_str(const std::string&);
};
#endif

TextQuery.CPP 文件

#include "TextQuery.h"
#include 
<sstream>
#include 
<string>
#include 
<vector>
#include 
<map>
#include 
<set>
#include 
<iostream>
#include 
<fstream>
#include 
<cctype>
#include 
<cstring>
#include 
<stdexcept>
 
using std::istringstream;
using std::set;
using std::string;
using std::getline;
using std::map;
using std::vector;
using std::cerr;
using std::cout;
using std::cin;
using std::ifstream;
using std::endl;
using std::ispunct;
using std::tolower;
using std::strlen;
using std::out_of_range;
 
string TextQuery::text_line(line_no line) const
{
    
if (line < lines_of_text.size())
        
return lines_of_text[line];
    
throw std::out_of_range("line number out of range");
}
 
// read input file: store each line as element in lines_of_text 
void TextQuery::store_file(ifstream &is)
{
    
string textline;
    
while (getline(is, textline))
       lines_of_text.push_back(textline);
}
 
// \v: vertical tab; \f: formfeed; \r: carriage return are
// treated as whitespace characters along with space, tab and newline
string TextQuery::whitespace_chars(" \t\n\v\r\f");
 
// finds whitespace-separated words in the input vector
// and puts the word in word_map along with the line number
void TextQuery::build_map()
{
    
// process each line from the input vector
    for (line_no line_num = 0
                 line_num 
!= lines_of_text.size();
                 
++line_num)
    {
        
// we'll use line to read the text a word at a time
        istringstream line(lines_of_text[line_num]);
        
string word;
        
while (line >> word)
            
// add this line number to the set;
            
// subscript will add word to the map if it's not already there
            word_map[cleanup_str(word)].insert(line_num);
    }
}
 
set<TextQuery::line_no>
TextQuery::run_query(
const string &query_word) const
{
    
// Note: must use find and not subscript the map directly
    
// to avoid adding words to word_map!
    map<stringset<line_no> >::const_iterator 
                          loc 
= word_map.find(cleanup_str(query_word));
    
if (loc == word_map.end()) 
        
return set<line_no>();  // not found, return empty set
    else
        
// fetch and return set of line numbers for this word
        return loc->second;
}
 
void TextQuery::display_map()
{
    map
< stringset<line_no> >::iterator iter = word_map.begin(),
                                       iter_end 
= word_map.end();
 
    
// for each word in the map
    for ( ; iter != iter_end; ++iter) {
        cout 
<< "word: " << iter->first << " {";
 
        
// fetch location vector as a const reference to avoid copying it
        const set<line_no> &text_locs = iter->second;
        
set<line_no>::const_iterator loc_iter = text_locs.begin(),
                                     loc_iter_end 
= text_locs.end();
 
        
// print all line numbers for this word
        while (loc_iter != loc_iter_end)
        {
            cout 
<< *loc_iter;
 
            
if (++loc_iter != loc_iter_end)
                 cout 
<< "";
 
         }
 
         cout 
<< "}\n";  // end list of output this word
    }
    cout 
<< endl;  // finished printing entire map
}
 
 
// lower-case to upper-case
string TextQuery::cleanup_str(const string &word)
{
    
string ret;
    
for (string::const_iterator it = word.begin(); it != word.end(); ++it) {
        
if (!ispunct(*it))
            ret 
+= tolower(*it);
    }
    
return ret;
}

主函數

#include "TextQuery.h"
#include 
<string>
#include 
<vector>
#include 
<map>
#include 
<set>
#include 
<iostream>
#include 
<fstream>
#include 
<cctype>
#include 
<cstring>
#include 
<cstdlib>
 
using std::set;
using std::string;
using std::map;
using std::vector;
using std::cerr;
using std::cout;
using std::cin;
using std::ifstream;
using std::endl;
 
string make_plural(size_t, const string&const string&);
ifstream
& open_file(ifstream&const string&);
 
void print_results(const set<TextQuery::line_no>& locs, 
                   
const string& sought, const TextQuery &file)
{
    
// if the word was found, then print count and all occurrences
    typedef set<TextQuery::line_no> line_nums; 
    line_nums::size_type size 
= locs.size();
    cout 
<< "\n" << sought << " occurs "
         
<< size << " "
         
<< make_plural(size, "time""s"<< endl;
 
    
// print each line in which the word appeared
    line_nums::const_iterator it = locs.begin();
    
for ( ; it != locs.end(); ++it) {
        cout 
<< "\t(line "
             
// don't confound user with text lines starting at 0
             << (*it) + 1 << ""
             
<< file.text_line(*it) << endl;
    }
}
 
 
// program takes single argument specifying the file to query
int main()
{
    
// open the file from which user will query words
    ifstream infile;
    
if (!open_file(infile, "Tanky_Woo.txt")) {
        cerr 
<< "No input file!" << endl;
        
return EXIT_FAILURE;
    }
 
    TextQuery tq;
    tq.read_file(infile);  
// builds query map
 
    
// iterate with the user: prompt for a word to find and print results
    
// loop indefinitely; the loop exit is inside the while
    while (true) {
        cout 
<< "enter word to look for, or q to quit: ";
        
string s;
        cin 
>> s;
 
        
// stop if hit eof on input or a 'q' is entered
        if (!cin || s == "q"break;
 
        
// get the set of line numbers on which this word appears
        set<TextQuery::line_no> locs = tq.run_query(s);
 
        
// print count and all occurrences, if any
        print_results(locs, s, tq);
     }
    
return 0;
}
 
string make_plural (size_t ctr , const string &word , 
const string &ending) 

    
return ( ctr == 1 ) ? word : word + ending; 

 
ifstream
& open_file(ifstream &inconst string &file)
{
    
in.close();  // close in case it was already open
    in.clear();  // clear any existing errors
 
    
// if the open fails, the stream will be in an invalid state
    in.open(file.c_str()); // open the file we were given
 
    
return in// condition state is good if open succeeded
}
posted on 2010-11-11 20:16 Tanky Woo 閱讀(2694) 評論(4)  編輯 收藏 引用

FeedBack:
# re: 一個簡單的文本查詢程序—摘至《C++ Primer》 2010-11-12 13:50 xinqikan.com
有源碼下載看看嗎  回復  更多評論
  
# re: 一個簡單的文本查詢程序—摘至《C++ Primer》 2010-11-12 15:43 Tanky Woo
@xinqikan.com
額。那個不是源碼嗎?  回復  更多評論
  
# re: 一個簡單的文本查詢程序—摘至《C++ Primer》 2010-11-25 13:34 cometrue
@xinqikan.com
犀利  回復  更多評論
  
# re: 一個簡單的文本查詢程序—摘至《C++ Primer》[未登錄] 2013-02-18 19:17 ming
要達到真實狀態的存在,其實就是對于有效存在的健康的安全,增長,效果的一種反映機制的產生,并且融入自己的真實的屬于自己的真實的生活細節當中去反映些須能夠觸及的模式  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              欧美激情在线有限公司| 亚洲天堂av在线免费| 久久视频一区二区| 性欧美8khd高清极品| 欧美成人一区在线| 欧美电影美腿模特1979在线看| 亚洲精品久久久久久久久| 欧美在线短视频| 欧美日韩三级一区二区| 狠狠色伊人亚洲综合网站色| 欧美福利一区| 国产综合香蕉五月婷在线| 一本色道久久88精品综合| 亚洲电影第1页| 蜜臀a∨国产成人精品| 国产精品美女在线| 亚洲视频网在线直播| 亚洲国产精品激情在线观看| 久久野战av| 亚洲美女毛片| 亚洲一区网站| 在线国产精品播放| 亚洲日本欧美| 免费欧美在线视频| 亚洲综合日韩中文字幕v在线| 性高湖久久久久久久久| 亚洲国产第一页| 欧美午夜久久| 亚洲第一福利视频| 国产女人水真多18毛片18精品视频| 久久露脸国产精品| 另类亚洲自拍| 久久露脸国产精品| 欧美国产日韩在线| 亚洲国产精品免费| 一区二区欧美亚洲| 国产精品a久久久久久| 欧美福利精品| 在线成人国产| 久久精品72免费观看| 新狼窝色av性久久久久久| 噜噜噜噜噜久久久久久91| 国产亚洲人成网站在线观看| 久久av一区二区三区亚洲| 欧美在线视频在线播放完整版免费观看| 久久全球大尺度高清视频| 久久久999成人| 国产精品美女主播| 亚洲最新视频在线| 99ri日韩精品视频| 日韩一级视频免费观看在线| 欧美成人精品影院| 久久日韩精品| 亚洲黄色av一区| 欧美激情视频给我| 国产精品国产三级国产aⅴ无密码| 亚洲高清视频中文字幕| 在线精品一区| 亚洲激情在线播放| 亚洲神马久久| 国产一区二区三区久久悠悠色av| 在线看日韩av| 亚洲精品免费在线| 99视频精品| 国产乱码精品一区二区三区不卡| 亚洲国产欧美日韩| 久久综合给合久久狠狠狠97色69| 亚洲国产精品成人一区二区 | 久久人人97超碰国产公开结果| 悠悠资源网久久精品| 欧美激情综合| 99精品欧美| 香蕉免费一区二区三区在线观看| 久久国内精品自在自线400部| 亚洲人成网站777色婷婷| 校园春色国产精品| 伊人久久综合97精品| 91久久精品美女高潮| 亚洲网站在线| 亚洲欧美日韩视频二区| 西西人体一区二区| 一本色道久久综合狠狠躁篇的优点 | 午夜精品在线视频| 亚洲国产成人在线| 在线不卡中文字幕播放| 国产精品另类一区| 欧美性大战久久久久久久| 女主播福利一区| 欧美一级一区| 久久www成人_看片免费不卡| 亚洲专区一区| 国产精品亚洲а∨天堂免在线| 亚洲大胆在线| 亚洲国产精品久久久久久女王| 欧美激情精品久久久久久久变态 | 欧美国产先锋| 蜜臀a∨国产成人精品| 黄色小说综合网站| 国模精品娜娜一二三区| 亚洲第一天堂无码专区| 亚洲日本中文| 亚洲欧美日本日韩| 麻豆精品传媒视频| 一本久道久久综合狠狠爱| 亚洲自拍偷拍网址| 亚洲精品久久7777| 免费人成精品欧美精品| 亚洲午夜伦理| 国产情人综合久久777777| 国产精品视频网址| 宅男精品导航| 夜夜精品视频一区二区| 欧美国产日本在线| 亚洲电影专区| 欧美电影免费网站| 久久免费少妇高潮久久精品99| 欧美综合77777色婷婷| 欧美视频二区36p| 亚洲影院在线| 欧美一级欧美一级在线播放| 国产精品美女一区二区| 欧美一区二区三区在线视频| 一区二区三区精品视频在线观看| 欧美视频精品一区| 99精品久久| 久久精品天堂| 久久成人免费| 亚洲国产网站| 99热在线精品观看| 国产精品永久免费在线| 久久狠狠亚洲综合| 国产一区二区你懂的| 欧美日韩在线播放三区四区| 亚洲少妇自拍| 久久免费视频在线观看| 91久久久亚洲精品| 黄色免费成人| 亚洲精品乱码久久久久久久久 | 亚洲精品孕妇| 欧美一区二区女人| 亚洲视频一区二区在线观看 | 老色鬼久久亚洲一区二区| 美女999久久久精品视频| 日韩一级裸体免费视频| 亚洲无线观看| 欧美成人激情在线| 亚洲在线视频| 免费欧美电影| 欧美国产高清| 国产精品日韩欧美大师| 亚洲人成网站在线观看播放| 亚洲综合第一页| 亚洲无线视频| 欧美三级午夜理伦三级中视频| 男人天堂欧美日韩| 雨宫琴音一区二区在线| 久久九九国产精品| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲精品日韩一| 欧美日韩国产系列| 亚洲国产一区二区三区高清| 亚洲激情啪啪| 欧美日韩免费| 亚洲一区二区三区在线观看视频| 在线综合+亚洲+欧美中文字幕| 欧美激情在线狂野欧美精品| 欧美激情中文字幕一区二区| 亚洲欧洲日本国产| 欧美日韩国产在线一区| 亚洲一区二区影院| 亚洲欧美国产高清va在线播| 国产欧美日本一区二区三区| 久久久久在线| 亚洲国产精品一区制服丝袜| 欧美伦理91i| 亚洲——在线| 亚洲国产欧美一区二区三区丁香婷| 一卡二卡3卡四卡高清精品视频| 欧美日韩人人澡狠狠躁视频| 欧美一区二区三区啪啪| 国产一区二区0| 亚洲欧美伊人| 亚洲三级电影全部在线观看高清| 亚洲欧美制服另类日韩| 在线观看成人网| 欧美视频国产精品| 免费在线看成人av| 欧美一区二区三区四区高清| 亚洲第一网站| 久久亚洲捆绑美女| 亚洲综合首页| 亚洲最新在线| 欧美日韩精品欧美日韩精品 | 国产人成一区二区三区影院| 蜜臀va亚洲va欧美va天堂 | 亚洲精品一区二区三区婷婷月| 国产精品久久久久久久久借妻| 久久成人精品电影| 亚洲一区二区精品视频| 久久综合国产精品台湾中文娱乐网 |