Linux下使用C语言调用C++编写的库,使用map来完成统计一篇文章的不同词数

该博客介绍了如何在Linux环境下,使用C语言调用C++编写的库,利用map统计文章中单词的出现次数和不同拼写格式。博主详细阐述了实现思路,包括区分和不区分大小写的统计,以及四种单词拼写形式的处理,并分享了遇到的问题及解决方案。
摘要由CSDN通过智能技术生成

Linux下使用C语言调用C++编写的库,使用map来完成统计一篇文章的不同词数

任务的内容还是和我之前发的博客一样,统计《哈利波特》里面不同的单词的出现次数以及一个单词的不同拼写形式
具体的见我的这篇文章
使用C语言来实现一个通用的双向链表。利用这个链表来统计一篇文章的不同词数,针对不同单词和同一单词的不同拼写形式进行排序

然后我根据这个任务,需要用map来完成,其中map中的key为单词,value为单词出现的个数

统计一篇文章中单词出现的词数(分为区分和不区分大小写)

具体思路是直接把单词放进我们的map中,因为我们的map的key是单独的,不可以冲突的,所以我们可以利用这个来统计单词。
针对每个读进来的单词,我们首先在map中找有没有以及存进去的单词,有的话就把单词的个数加1,没有的话就把这个单词存进去我们的map中,然后把值给设定为1。
思路基本很简单,如果用C++可以很容易的实现

统计一篇文章中单词的不同拼写格式

我们需要统计单词的四种拼写形式:
1、全小写
2、全大写
3、首字母大写
4、部分大写(如McDonald’s)
思路如下,我们先把文章中的单词不分大小写的存进我们的一个map中,然后再用另外一个map来统计不同单词的不同格式的出现次数

下面放我们的代码,然后我讲解我在实现过程中遇到的困难。因为我是在Linux的环境下利用C的代码(main)程序来进行测试的,所以我的代码要加一步对于C++库的封装

myclass.h

#include <iostream>
#include <map>
#ifndef _MYCLASS_H
#define _MYCLASS_H
using namespace std;

class MyClass
{
    private:
        
    public:
	map<string,int> word_count;
	MyClass();
	~MyClass();
        void wordmap(string word);
	void wordadd(string word);
	void mapvisit();
	void mapsort();
};

class wordform
{
    private:
        
    public:
	//bool flag = false;
	map<string,int> word_count_;
	wordform();
	~wordform();
        void wordmap(string word,int num);
	void mapupdate(string word);
	void mapvisit();
	void mapsort();
	bool hasnot(string word);
	
};
#endif

myclass.cc

#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include "myclass.h"
using namespace std;

typedef pair<string, double> PAIR;


struct CmpByValue {  
  bool operator()(const PAIR& lhs, const PAIR& rhs) {  
    return lhs.second > rhs.second;  
  }  
};



MyClass::MyClass(){}
MyClass::~MyClass(){}

void MyClass::wordmap(string word)
{
	    map<string,int >::iterator iter;//在里面找一遍,看看有没有符合的
            iter=word_count.find(word);
            if(iter==word_count.end())
            {
                word_count[word]=1;
                //cout<<"we do not find 112"<<endl;
            }

            else
            {
                word_count[word]++;
                //cout<<"wo find 112"<<endl;
            }
            
}

void MyClass::wordadd(string word)
{
	word_count[word]=1;
}
void MyClass::mapvisit()
{
	map<string,int >::iterator iter;
	iter = word_count.begin();
	for(int i=0; i < 5 ; i++)
            {
                cout << "[" << iter->first << "] = " << iter->second << endl;
		iter++;
            }
}

void MyClass::mapsort()
{
	vector<PAIR> name_score_vec(word_count.begin(), word_count.end()); 
	sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());
	for (int i = 0; i <5; ++i) {  
		//可在此对按value排完序之后进行操作
	cout << left << setw(10) << name_score_vec[i].first << name_score_vec[i].second << endl;  
	}
}



//下面来对wordform这个类进行实现

wordform::wordform(){}
wordform::~wordform(){}

bool wordform::hasnot(string word)
{
	map<string,int >::iterator iter;
	iter=word_count_.find(word);
        if(iter==word_count_.end()) //之前没有这个节点
        {
            return true;
        }
	return false;
}

void wordform::wordmap(string word,int num)
{
	word_count_[word] = num;
            
}

void wordform::mapupdate(string word)
{
	word_count_[word]++;
}

void wordform::mapvisit()
{
	map<string,int >::iterator iter;
	iter = word_count_.begin();
	for(int i=0; i < 5 ; i++)
            {
                cout << "[" << iter->first << "] = " << iter->second << endl;
		iter++;
            }
}

void wordform::mapsort()
{
	vector<PAIR> name_score_vec(word_count_.begin(), word_count_.end()); 
	sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());
	for (int i = 0; i <5; ++i) {  
		//可在此对按value排完序之后进行操作
	cout << left << setw(10) << name_score_vec[i].first << name_score_vec[i].second << endl;  
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值