面向对象程序设计(基于C++)0401 从文本中统计单词出现的行数

该博客介绍了如何使用C++编写一个程序,从文本文件中读取关键字并统计它们出现的行数。程序利用了istringstream进行字符串流处理,并通过map数据结构存储关键字及其行号。在读取文件时,将每行拆分为单词,并与关键字列表比较,将匹配项加入到结果映射中。最后,程序按照关键字升序输出并去重。涉及的技术包括文件输入、字符串处理、去重算法和C++容器操作。

题目

keywordsCounting.cpp
Define a function that, given an istream& and a const vector&, produces a map<string,vector> holding each string (keyword) and the numbers of the line on which the string appears. The line number of the first line is 1.
Print in the order of ascending keyword.
Run the program on a text file with at least 10000 lines looking for no fewer than 10 keywords.
定义一个函数,给定一个istream&和一个const vector&,生成一个map<string,vector>来保存每个字符串(关键字)和字符串所在行的编号。第一行的行号是1。
按关键字升序打印。
在至少10000行的文本文件上运行程序,查找不少于10个关键字。

测试文本

要求


Function Prototype:
map<string,vector<int>> function_name (istream&, const vector&) ;
这部分没啥好说的,就老老实实设计一个函数命返回一个map就好了

  1. Write the program
    keywordsCounting.cpp with at least two functions  keywordsCounting
  2. Prepare keywords and text2search for keywords and for text file.
  3. Execution:./keywordsCounting keywords text2search result
    这部分资源会后续补充


<sstream>
istringstream sin(string);

string read operations are identical to cin operations.
An example:
string word; while (sin>>word) …
这部分的意思就是使用sstream的头文件,然后声明 istringstream sin(str)
把str的内容转换成输入流sin,然后使用while(sin >> word)的时候会把str以"\n" " " "\t"拆开放到字符串word中


Algorithm: find an element in a container
Return iterator to found element,
else return end() if not found.

An Example: vector a; T value;
vector::iterator x;
(x=find(a.begin(),a.end(),value)) != a.end()
//可以用find来进行查重,但实际上可以使用unique函数,顺便复习一下上次的学习内容0202 排序去重

知识点

① 文件输入

#include<fstream>
	ifstream fin;
	fin.open("filename");
	string str;
	getline(fin,str);

②字符串输入

 #include <sstream>
 	istringstream sin(str);
	string word; while (sin>>word) {}

③去重

#include<algorithm>
	while((auto x = find( v.begin(), v.end(), value))  != v.end())

④map的创造和pair的使用

	const vector<string> & v
	map<string,vector<int>> m;
    vector<int> a;
    for(vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
    {
        pair<const string,vector<int>> p(*it,a);
        m.insert(p);
    }

代码

#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<bits/stdc++.h>

using namespace std;

map<string, vector<int>> func(istream& in, const vector<string> & v)
{
    map<string,vector<int>> m;
    vector<int> a;
    for(vector<string>::const_iterator it = v.begin(); it != v.end(); it++)
    {
        pair<const string,vector<int>> p(*it,a);
        m.insert(p);
    }


    int loc = 0;
    string line;
    while(getline(in,line))//文件流变成字符串
    {
        
        loc++;
        istringstream sin(line);//把字符串变成字符串流
        string word;
        
        while(sin >> word)
        {
            for(map<string, vector<int>>::iterator it = m.begin(); it != m.end(); it++)
            {
                if(it->first == word)
                {
                    it->second.push_back(loc);
                }
            }
        }
    }

    return m;
}

int main()
{
    ifstream all;
    all.open("text2search");
    ifstream key;
    
    key.open("keywords");
    vector<string>v;

    string s;
    while(key >> s)
        v.push_back(s);


    map<string,vector<int>> m = func(all, v);

    for(map<string, vector<int>>::iterator it = m.begin(); it != m.end(); it++)
    {
        sort(it->second.begin(),it->second.end());


        auto j = unique(it->second.begin(),it->second.end());
        it->second.resize(j - it->second.begin());
        //去重,用find函数可以不用这个resize
        
        
        cout << it->first <<endl;
        for(auto ite = it->second.begin(); ite != it->second.end(); ite++)
        {
            cout << *ite << " ";
        }
        cout << endl << endl;
    }

    all.close();
    key.close();
}


也可以用find函数来进行查重

while(sin >> word)
        {
            for(map<string, vector<int>>::iterator it = m.begin(); it != m.end(); it++)
            {
                if(it->first == word)
                {
                    vector<int>::iterator ite = find(it->second.begin(), it->second.end(), loc);
                    if(ite == it->second.end())
                        it->second.push_back(loc);
                }

                
            }
        }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值