如何实现在C计数HashMap? [英] how to implement counting hashmap in c?

查看:114
本文介绍了如何实现在C计数HashMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个常量字符串(每个5个字母)

我得到了一小部分词汇(在这6个词中)。



我想统计每个单词出现的次数。



如何在C中实现它?



我曾尝试过:

  char searchEngineNames [6] [ 5] = {waze_,faceb,fours,googl,
fueli,yello};
$ b $ static foo(const char * res_name,int success,void * context,char * last_modified){
if(success){
for(int i = 0; i< ; 6; i ++)
{
char substringFiveChars [6];

strncpy(substringFiveChars,res_name,5);

char substringFiveChars [6];

substringFiveChars [5] = 0;

if(strcmp(searchEngineNames [i],substringFiveChars)== 0)
{
...
}
..
}

例如:


wooo _,wooo _,faceb,wooo _,google

我最终会得到:





 wooo_3次

faceb1次

google1次

fours 0次

fuelil0次

yello0次


解决方案

我使用2个数组而不是1个。

  char searchEngineNames [6] [5] = {wooo_,faceb,fours,google,
fuelil,yello};

int searchEngineCounts [6] = {0,0,0,0,0,0};



static void foo(const char * res_name,
int success,void * context,char * last_modified){
if(success){

int i = 0;
for(i; i <6; i ++){
char substringFiveChars [6];

strncpy(substringFiveChars,res_name + 7,5);

substringFiveChars [5] = 0;

if(strcmp(searchEngineNames [i],substringFiveChars)== 0){
searchEngineCounts [i] ++;

if(searchEngineCounts [i] <3){

...
}
}
}

}
}


I have 6 const strings (5 letters each)

I get a stream of few words (out of these 6 words).

I want to count how many occurrences were out of each word.

How can I implement it in C ?

I have tried:

  char searchEngineNames[6][5] = { "waze_", "faceb", "fours", "googl",
        "fueli", "yello" };

    static void foo(const char* res_name, int success, void *context, char *last_modified) {
        if (success){
                for (int i=0; i<6; i++)
                {
                    char substringFiveChars[6];

                    strncpy(substringFiveChars, res_name, 5);

                            char substringFiveChars[6]; 

                            substringFiveChars[5] = 0;

                    if (strcmp(searchEngineNames[i],substringFiveChars) == 0)
                    {
                    ... 
                    }
    ..
                }

for example for this stream:

"wooo_","wooo_","faceb","wooo_","google"

I will eventually get:

"wooo_" 3 times

"faceb" 1 times 

"google" 1 times 

"fours" 0 times

"fuelil" 0 times

"yello" 0 times

解决方案

I use 2 arrays instead of 1.

char searchEngineNames[6][5] = { "wooo_", "faceb", "fours", "google",
        "fuelil", "yello" };

int searchEngineCounts[6] = {0,0,0,0,0,0};



static void foo(const char* res_name,
        int success, void *context, char *last_modified) {
    if (success) {

        int i = 0;
        for (i; i < 6; i++) {
            char substringFiveChars[6];

            strncpy(substringFiveChars, res_name+7, 5);

            substringFiveChars[5] = 0;

            if (strcmp(searchEngineNames[i], substringFiveChars) == 0) {
                searchEngineCounts[i]++;

                if (searchEngineCounts[i] < 3) {

...
                }
            }
        }

    }
}

这篇关于如何实现在C计数HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆