整理的C++ primer TextQuery 例题

1.07---共识机制详解(PoW、PoS、DPoS 对比) 【摘要】区块链能成为“无需信任的协作网络”,靠的正是它的“共识机制”。本节将深入剖析 PoW、PoS、DPoS 等核心共识模型,并结合以太坊从 PoW 到 PoS 的转型与 Solana 等新型方案,为你拆解这一决定区块链运行方式、性能、安全性的“发动机系统”。什么是共识机制?为何不是多数投票这么简单?三大主流共识算法(PoW、PoS、DPoS)机制对比与演化路线以太坊从 PoW 到 PoS 的转型细节(The Merge)Solana 的 PoH + PoS 混合共识机制解析。 阅读详情
//////   TextQuery.cpp

/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *     Pearson Education, Inc.
 *     Rights and Contracts Department
 *     75 Arlington Street, Suite 300
 *     Boston, MA 02216
 *     Fax: (617) 848-7047
*/
 

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


// : vertical tab; : formfeed;  : carriage return are
// treated as whitespace characters along with space, tab and newline
string TextQuery::whitespace_chars("  ");

// 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<string, set<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;   // It's also a set of line numbers
}


void TextQuery::display_map()
{
    map
< string, set<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;     // a reference to the set in the map
        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 
<< "} ";  // end list of output this word
    }

    cout 
<< endl;  // finished printing entire map
}


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





////////////////////////////////////////////////
// TextQuery.h
/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *     Pearson Education, Inc.
 *     Rights and Contracts Department
 *     75 Arlington Street, Suite 300
 *     Boston, MA 02216
 *     Fax: (617) 848-7047
*/
 

#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


///////////////////////////////////////////
// main function 
//WordCount.cpp
/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *     Pearson Education, Inc.
 *     Rights and Contracts Department
 *     75 Arlington Street, Suite 300
 *     Boston, MA 02216
 *     Fax: (617) 848-7047
*/
 

#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;
using std::ends;

string make_plural(size_t, const string&, const string&);
ifstream
& open_file(ifstream&, const string&);
ifstream
& close_file(ifstream&);
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 
<< " " << 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 
<< " (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;
    
string fname;
    cout
<<"Please input file name which wanted to query:"<<ends;
    cin
>>fname;
    
if(!open_file(infile,fname))
    
{
      cerr
<<"Open file error!"<<endl;
      
return EXIT_FAILURE;
    }


    TextQuery tq;
    tq.read_file(infile);  
// builds query map
    
    close_file(infile);

    
// 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"||"Q"==s) 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);
     }

    close_file(infile);
    
return 0;
}



string make_plural(size_t ctr,const string &word,const string &ending)
{
   
// Make some modify to adjust the application
  return (1==ctr||0==ctr)? word:word+ending;   
}



ifstream 
&open_file(ifstream &in,const string &file)
{  
  
in.open(file.c_str());
  
return in;
}


ifstream 
&close_file(ifstream &in)
{
  
in.close();
  
in.clear();
  
return in;
}

 
一个不错的题目,个人整理了一下,发上来保存吧。
我个人在里面做了小小的修改,但基本没有变化什么。
win10 跳过U8环境检测的IE WebControls.rar win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。 立即下载

相关推荐

动态数据脱敏技术分析

在当前国内信息安全热潮中,数据脱敏作为数据安全的重要一环得到了业界的认可与重视。早在2012年,数据脱敏首次作为一个单独的魔力象限由Gartner发布,Gartner在2014年又提出了:按照数据使用场景,将数据脱敏分为静态数据脱敏(Static data masking-SDM )与动态数据脱敏(Dynamic data masking-DDM )。 可能有人望文生义,认为动态数据脱敏一定比静态数据脱敏高级。非也非也,静态or动态,取决于脱敏的使用场景,主要是以使用场景为由来选择合适的数据脱敏的模式。 本

美创科技的博客 4670

《C++ Primer》中的TextQuery例子

正在看《C++ Primer》(4th),第325页给出了一个TextQuery例子,很好地总结了STL容器的用法,但是书上没有给出连续完整的代码,我实验过后把程序记录下来,便于以后查找回忆。该程序的功能是查找输入文本文件中某个单词的出现次数及其行号。 #ifndef TEXTQUERY_H_ #define TEXTQUERY_H_ #include #include /

AirkaleeXD的专栏 1532

海康威视web控件开发+vue结合做网页开发

需求:vue的项目,需要在页面展示出海康的硬盘录像机连接的摄像头的实时监控画面以及回放功能. 准备:web开发包(包含插件以及demo,在海康官网即可下载32位64位的都有.那些需要积分下载的博主良心不疼么.) 点击这里下载:链接: https://pan.baidu.com/s/12FrbycWyWWyiRJH5Yd1tPA 提取码: gam5 开始开发: 1.安装demo目录下的WebComponentsKit.exe插件(根据自己的浏览器以及系统选择32位还是64位的插件) 2.打.

u013682258的博客 8484

C++ Primer中文本查询示例Query的实现

最近在看C++ Primer复习C++的语法,看到书中15.9章中的文本查询示例时,觉得设计得非常不错,于是便动手照着实现了一个,修改了很久终于运行成功了,从中也学习到了很多的语法。下面把实现与总结分享给大家

心砚THU的专栏 4580

基于ResNet-50的猫狗分类训练

基于ResNet-50的猫狗分类python训练

weixin_46352497的博客 1004

扩增子分析|溯源分析在R中的复现:跟着Nature系列文章利用FEAST溯源分析包追溯微生物群落的起源

溯源分析由Liat Shenhav等人在2019年发表在Nature Methods杂志,他们开创的一种分析微生物群落组成结构的方法,通过量化不同微生物样本(来源)在目标微生物群落(汇)中的比例或分数识别微生物群落的潜在来源。该方法自发表以来受到微生物学界广泛关注,该论文截止2024年10月9日已经被引289次,是领域内高被引论文,该方法已经成为Nature,cell系列文章高度关注的方法。

weixin_46814750的博客 3433

jQuery is out of date

在我看来,jQuery确实已经过时了。本项目总结了绝大部分 jQuery API 替代的方法,类似项目You-Dont-Need-jQuery,并会再此基础上进行很多的补充。写这个项目主要想让自己和大家增进对javascript原生api的理解,也可以作为一个"原生jquery"的api文档随时查看。兼容ie9及以上浏览器,如不支持ie9...

weixin_33969116的博客 221

[前沿技术] AMD FSR 1.0源码解析:EASU算法实现细节与优化策略

本文深入解析了AMD FSR 1.0技术的核心——EASU(边缘自适应空间升频)算法。通过剖析其源码,详细阐述了EASU如何利用12-tap采样窗口、基于Lanczos滤波器的多项式近似以及自适应的边缘检测机制,在无需AI模型的情况下实现高质量图像放大。文章重点揭示了其极致的GPU优化策略,包括寄存器利用、采样优化和避免动态分支,为理解高性能图形算法提供了绝佳范例。

docker7nomad的博客 540

C++ Primer TextQuery 完整代码

最近看这本书, 然后查资料整理了一些这个代码, 并添加了注释 练习一些 IO 和map set吧: #include "TextQuery.h" using namespace std; ifstream &open_file(ifstream &in, const string &file); void print_results(const set& locs, const string

梦想无论怎样模糊,总潜伏在我们心底,使我们的心境永远得不到安宁. 1612

ADS基础教程16 - 存档和导入(workspace、cell、view)

选择workspace_Archive目录作为保存workspace归档的目录,点击打开进行目录选择,点击Finish保存目录。,选择cell_Archive目录作为保存cell归档的目录,点击打开进行目录选择,点击Finish保存目录。,选择view_Archive目录作为保存view归档的目录,点击打开进行目录选择,点击Finish保存目录。(2)在弹框中,可以选择点击Select将参考的条目添加进来,然后点击Next…(2)在弹框中,可以选择点击Select将参考的条目添加进来,然后点击Next…

XZ_ZC的博客 3702

【大语言模型 37】知识蒸馏深度实战:大模型向小模型迁移

本文深入探讨了知识蒸馏技术在大语言模型领域的应用,详细阐述了如何将庞大复杂的大模型知识有效迁移到轻量级小模型中。文章系统介绍了知识蒸馏的理论基础、多种蒸馏损失函数设计、特征匹配与注意力迁移技术、渐进式蒸馏策略以及全面的效果评估方法。通过实战案例和完整代码实现,读者将掌握从理论到实践的知识蒸馏全流程,了解如何在保持模型性能的同时显著降低计算资源需求,实现大语言模型的高效部署与应用。

熵数实验室 1508

C++ primer 文本查询程序 Query

C++ primer 文本查询程序,面向对象的解决

linux C/C++程序员 1182

华为 MetaERP 集成盘古大模型、天筹 AI 求解器等生成式 AI 相关技术能力,结合华为云数智融合平台的支撑,在供应链预测中围绕需求预测、物料匹配

华为 MetaERP 集成盘古大模型、天筹 AI 求解器等生成式 AI 相关技术能力,结合华为云数智融合平台的支撑,在供应链预测中围绕需求预测、物料匹配、风险预警等核心场景开展实践,大幅提升了供应链预测的精准度与效率,以下是具体实践内容及成效: 动态需求预测,降低库存偏差华为供应链涉及数万种采购物料,物料清单层级高,且市场需求波动大。MetaERP 集成华为云 ModelArts 等工具构建专属动态需求预测模型,融合深度学习算法,纳入市场趋势、历史销售、产能数据等多维度信息进行综合预测。在华为自身供应链应

金牌架构师 700

小白初学C++ PRIMER 第五版 第二章

第二章感觉还是有点绕人的,尤其是指针跟引用,慢慢适应吧,整体感觉C基础多! 准备学完基础慢慢看些其他的简单程序,一点点熟悉,学以致用。 再给自己加个油! 整理了一些第二章的练习代买VS2013下的,留个脚印,方便自己查找。 先mark!

northjax的博客 363

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

C/C++ 头文件相对位置的包含

    对于多个的C/C++头文件的包含,一般情况下放在工程的目录下编译器就可以正确地找到。 但是对于在不同文件夹下的头文件的包含,当然可以写完整的路径,但是这样有可能会出现将工程拷到别人的机子上运行不了的问题。如果在这个程里同写上相对的路径,那么不仅在自己的机子上能运行,别人拷了过去也能正确运行。     例如我的一个工程头文件如下:      c:/F/Work/test/te

HelloDan的专栏 7363

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SHBrowseForFolder 的一些使用

   SHBrowseForFolder是一个可以用于获取文件夹路径的Windows API。使用起来可以方便很多,近来在写程序时用到了,现在总结一下。   首先当然要有一些基本认识才行。所以先看一下下面所列的MSDN。http://msdn.microsoft.com/en-us/library/bb762115%28VS.85%29.aspxhttp://msdn.micr

HelloDan的专栏 5298

SHGetSpecialFolderLocation 的使用方法

详情,请查看MSDN:http://msdn.microsoft.com/en-us/library/bb762203%28VS.85%29.aspx 这里只提供一小段代码: void CDlgTestDlg::OnBnClickedOk(){ LPITEMIDLIST pidl; TCHAR szPath [MAX_PATH]; if(SUCCEEDED( S

HelloDan的专栏 4262

阻止Windows注消 (log off or logging off)

 先看下MSDN http://msdn.microsoft.com/en-us/library/aa376876%28VS.85%29.aspx 示例代码: LRESULT CLangTestDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam){ if(message==WM_QUERYENDS

HelloDan的专栏 2214

ActiveX in Visual studio 2008

   近来看书看到了ActiveX,由于环境换成了Visual studio 2008,找了半天没有找到ActiveX Test container,到网上找了一下才发现ActiveX Test container in Visual studio 2008是要自己编译的,下面是MS所介绍的。 http://msdn.microsoft.com/en-us/library/f9adb5t5

HelloDan的专栏 1515

cin EOF Linux

 我为什么写这个题目呢,因为我今天要写的博与这三者都有关系,至少我个人认为是这样的。有个人问了一个这样的小程序,说有问题:#include #include #include using namespace std;int main(){ vector spvec; string str; cout <<"input string" <<endl;

HelloDan的专栏 1417

几个关于语言和区域的函数

 GetUserDefaultUILanguageThis function retrieves the language identifier (LANGID) for the current users UI language selection.http://msdn.microsoft.com/en-us/library/aa913797.aspx   Set

HelloDan的专栏 1036

SHBrowseForFolder的使用,获取其中一个button的ID

 关于其他方面的东西,可以参考上一篇:http://blog.youkuaiyun.com/HelloDan/archive/2009/10/26/4729693.aspx  HWND hChild=GetWindow(hwnd, GW_CHILD); while(hChild) { GetWindowText(hChild,szWindo

HelloDan的专栏 1009

SHBrowseForFolder 上的network不能展开???

 关于SHBrowseForFolder的使用麻烦翻看我前面所写的博文。 但后来我改了什么我倒不知道,只是那样在win7下NetWork是不能展开看到文件夹的,但是奇怪的是XP可以。我找了半天,后面发现别人的一个例子可以做到,我的不可以,但他的与我所写的基本一样,这样我就晕了,难道是我的设置错误吧。  找了一下,果然,我错误的代码是这样的:  BROWSEINFO bi;// ...

HelloDan的专栏 929

C++ primer English Fourth edition P322 Exercise 9.18

 #includeiostream>#includevector>#includelist>#includealgorithm>using namespace std;/**//*// The procedure to compare a vector and a list is find if they are equal.bool compare(const vector &vec,c

HelloDan的专栏 908

ACE调试信息的输出

/**00470 * Format a message to the thread-safe ACE logging mechanism. Valid00471 * options (prefixed by %, as in printf format strings) include:00472 * - A: p

HelloDan的专栏 878

C++ primer english fourth edition page330

#includeiostream> //#include#includelist>#includevector>using namespace std; int main() ...{    listchar*> clist(5,"Hello Dan");   vectorstring> svec(7,"HelloWord");   svec.assign(clist.begin(),clis

HelloDan的专栏 871

using switch case block not just under the integral type

#includeiostream>   #includestring>   #includemap>using namespace std;int main() ...{      mapstring,int> strint;   strint.insert(make_pair("Banana",1));   strint["Apple"]=2;      strint.insert(maps

HelloDan的专栏 863
上一篇: C++ primer English Fourth edition page 349 exercises
下一篇: Iterator
HelloDan
博客等级 码龄20年 187粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值