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

隨筆 - 70  文章 - 160  trackbacks - 0

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

常用鏈接

留言簿(8)

隨筆檔案

文章檔案

搜索

  •  

積分與排名

  • 積分 - 180078
  • 排名 - 147

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

這是在《C++ Primer》上第十章最后的一個(gè)小節(jié)。以前把這里漏掉了,剛才看了下,覺(jué)得這個(gè)程序很不錯(cuò),便于對(duì)vector, map, set的基本掌握。特地把這一個(gè)小程序記錄下來(lái)。

/*
 *目的:一個(gè)簡(jiǎn)單的文本查詢程序
 *作用:程序?qū)⒆x取用戶指定的任意文本文件,然后允許用戶從該文件中查找單詞。
 *查詢的結(jié)果是該單詞出現(xiàn)的次數(shù),并列出每次出現(xiàn)所在的行。
 *如果某單詞在同一行中多次出現(xiàn),程序?qū)⒅伙@示該行一次。
 *行號(hào)按升序顯示,即第 7 行應(yīng)該在第 9 行之前輸出,依此類推。
 
*/

/*思路:
 *1.使用一個(gè) vector<string> 類型的對(duì)象存儲(chǔ)整個(gè)輸入文件的副本。
 *   輸入文件的每一行是該 vector 對(duì)象的一個(gè)元素。
 *   因而,在希望輸出某一行時(shí),只需以行號(hào)為下標(biāo)獲取該行所在的元素即可。
 *2.將每個(gè)單詞所在的行號(hào)存儲(chǔ)在一個(gè) set 容器對(duì)象中。
 *   使用 set 就可確保每行只有一個(gè)條目,而且行號(hào)將自動(dòng)按升序排列。
 *3.使用一個(gè) map 容器將每個(gè)單詞與一個(gè) set 容器對(duì)象關(guān)聯(lián)起來(lái),
 *   該 set 容器對(duì)象記錄此單詞所在的行號(hào)。
 
*/

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;
}

主函數(shù)

#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) 評(píng)論(4)  編輯 收藏 引用

FeedBack:
# re: 一個(gè)簡(jiǎn)單的文本查詢程序—摘至《C++ Primer》 2010-11-12 13:50 xinqikan.com
有源碼下載看看嗎  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的文本查詢程序—摘至《C++ Primer》 2010-11-12 15:43 Tanky Woo
@xinqikan.com
額。那個(gè)不是源碼嗎?  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的文本查詢程序—摘至《C++ Primer》 2010-11-25 13:34 cometrue
@xinqikan.com
犀利  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的文本查詢程序—摘至《C++ Primer》[未登錄](méi) 2013-02-18 19:17 ming
要達(dá)到真實(shí)狀態(tài)的存在,其實(shí)就是對(duì)于有效存在的健康的安全,增長(zhǎng),效果的一種反映機(jī)制的產(chǎn)生,并且融入自己的真實(shí)的屬于自己的真實(shí)的生活細(xì)節(jié)當(dāng)中去反映些須能夠觸及的模式  回復(fù)  更多評(píng)論
  

只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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图片| 亚洲欧美日韩一区在线观看| 国产一区二区三区av电影| 免费视频亚洲| 欧美日韩国产综合新一区| 亚洲欧美日韩精品一区二区| 性一交一乱一区二区洋洋av| 亚洲精品1234| 亚洲一区二区免费视频| 伊人婷婷欧美激情| 一二三区精品| 在线观看日韩| 亚洲五月婷婷| 亚洲欧洲视频| 欧美一区二区三区精品电影| 99成人免费视频| 欧美一区在线视频| 一区二区福利| 久久一区精品| 久久精品一本| 欧美日韩中文字幕日韩欧美| 久久一区二区三区超碰国产精品| 欧美精品一区三区| 久久久亚洲精品一区二区三区| 欧美精品免费在线观看| 久久在精品线影院精品国产| 欧美日韩亚洲一区| 免费成人高清视频| 国产日产欧产精品推荐色| 亚洲欧洲日产国产综合网| 激情综合亚洲| 午夜精品一区二区三区在线 | 欧美18av| 久久久之久亚州精品露出| 欧美日韩国产色综合一二三四| 久久久免费av| 国产精品中文字幕欧美| 91久久午夜| 亚洲精品美女在线| 久久免费观看视频| 久久视频这里只有精品| 国产九色精品成人porny| 亚洲乱码国产乱码精品精天堂| 亚洲丰满在线| 久久久亚洲精品一区二区三区| 先锋亚洲精品| 国产精品实拍| 亚洲欧美成人| 久久精品日韩欧美| 在线亚洲一区观看| 亚洲一级黄色| 欧美视频精品在线观看| 日韩亚洲欧美高清| 亚洲调教视频在线观看| 国产精品成人aaaaa网站| 日韩视频永久免费观看| 一区二区三区|亚洲午夜| 欧美日本乱大交xxxxx| 亚洲精品久久视频| 亚洲视频精选| 国产精品视频精品| 久久9热精品视频| 欧美成人国产| 亚洲精品欧美精品| 欧美日韩理论| 亚洲欧美日韩国产精品| 欧美在线精品免播放器视频| 国产日韩欧美在线一区| 久久麻豆一区二区| 亚洲国产日韩一区二区| 一本色道精品久久一区二区三区| 欧美午夜激情视频| 欧美在线视频网站| 亚洲成在人线av| 亚洲性色视频| 国产一区视频观看| 欧美电影免费观看大全| 一本不卡影院| 久久精品欧洲| 日韩天堂av| 国产午夜精品福利| 欧美不卡福利| 亚洲一区二区视频在线| 蜜桃av综合| 亚洲视频在线观看视频| 国产一区二区精品| 欧美激情第二页| 香蕉av777xxx色综合一区| 欧美国产日韩xxxxx| 亚洲无亚洲人成网站77777| 国产亚洲精品成人av久久ww| 理论片一区二区在线| 亚洲香蕉伊综合在人在线视看| 久久天天躁狠狠躁夜夜av| 99re热这里只有精品视频| 国产一区二区三区四区| 欧美日韩在线播放一区| 久久精品女人的天堂av| 在线亚洲免费| 亚洲国产天堂久久综合| 久久久久久久性| 亚洲宅男天堂在线观看无病毒| 影音先锋另类| 国产伦一区二区三区色一情| 欧美母乳在线| 久久性天堂网| 欧美影院视频| 亚洲欧美激情一区| 在线一区二区三区四区| 亚洲高清在线观看一区| 久久久久久久一区二区| 亚洲欧美一区二区三区久久| 亚洲精品美女免费| 尤物yw午夜国产精品视频明星| 国产欧美一区二区三区在线看蜜臀| 欧美激情在线播放| 美女诱惑黄网站一区| 久久精品国语| 久久久精品性| 久久精品国产一区二区三| 亚洲欧美日韩综合| 亚洲性线免费观看视频成熟| 亚洲日本理论电影| 欧美激情在线免费观看| 亚洲欧美激情视频| 99国产精品99久久久久久粉嫩 | 亚洲欧美日韩第一区| 亚洲美女av网站| 亚洲大胆女人| 亚洲国产二区| 亚洲精品一区二区三区99| 在线看国产日韩| 亚洲高清二区| 亚洲日本一区二区| 日韩亚洲视频| 这里只有视频精品| 亚洲欧美日韩在线| 午夜日韩电影| 久久国产高清| 久久一区二区三区av| 免费看黄裸体一级大秀欧美| 男女精品网站| 亚洲国产精品久久久久| 亚洲精品在线电影| 一区二区三区国产精品| 国产精品99久久久久久久vr| 亚洲在线播放| 欧美中文在线观看国产| 久色婷婷小香蕉久久| 欧美岛国激情| 国产精品激情电影| 国产日韩欧美在线播放| 亚洲电影天堂av| 亚洲精品孕妇| 午夜精品亚洲一区二区三区嫩草| 久久精品天堂| 亚洲第一中文字幕在线观看| 亚洲乱码国产乱码精品精天堂| 亚洲欧美日韩区| 久久久久久97三级| 欧美三级特黄| 国产自产2019最新不卡| 亚洲美女电影在线| 欧美亚洲专区| 亚洲黄色免费| 亚洲视频专区在线| 久久午夜激情| 国产精品一卡二卡| 最新中文字幕一区二区三区| 亚洲视频在线看| 欧美成人午夜剧场免费观看| 99xxxx成人网| 免费观看欧美在线视频的网站| 欧美午夜精品理论片a级按摩| 国内激情久久| 亚洲欧美日产图| 欧美激情精品久久久久久久变态| 在线一区二区三区做爰视频网站| 久久久精品2019中文字幕神马| 欧美日韩在线亚洲一区蜜芽| 樱桃成人精品视频在线播放| 亚洲一区二区免费视频| 欧美成人在线影院| 小嫩嫩精品导航| 欧美日韩一区二区在线| 亚洲欧洲日韩在线| 久久久噜噜噜久久人人看| 一二三四社区欧美黄| 欧美国产精品| 91久久国产自产拍夜夜嗨| 久久午夜视频| 欧美一区2区三区4区公司二百|