C ++,计算字符串中的重复单词并显示 [英] C++, count repeated words in the string and display

查看:216
本文介绍了C ++,计算字符串中的重复单词并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串说:走路说话,不会不会说话。我想计算收录的单词并显示。
注意:它不区分大小写。

I have string say "walk talk, can't won't Won't woN'T talk." I want to count the reapeated words and display. Note: it is not case sensitive.

我用过分度计

strtok(string, ",.;:\"!? -_\n\t*()@#=+");

并将其保存在

char *temp[100];

现在如何检查单词的重复?并显示如下

Now how can I check for repeatation of words? And display as below

3 won't
2 talk
1 can't
1 walk

应从最高重复显示到最低重复,如果重复相同,则显示字母顺序。

it should display from highest repeat to lowest. And if the repeatation is same then display alphabetic order.

对不起,我的英语不好。

Sorry for my bad english.

推荐答案

使用std :: string保留 strtok()。然后创建一个 std :: map< string,int> 来保存字符串的计数(密钥)已发生。

Use a std::string to hold the result of the strtok(). Then create a std::map<string, int> to hold the count of the times the string (the key) has occurred.

您可以使用以下命令填充地图:

You can populate the map with:

std::map<string, int> myMap;
myMap[tokenizedWord]++; //Increase count of word.

然后您可以循环浏览地图内容并打印出整数值大于2的位置。 / p>

You can then cycle through the map content and print out wherever the integer value is greater than 2.

for (std::map<string, int>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    if (iter->second > 1)
        std::cout << "Duplicated word: " << iter->first << " count = " << iter->second;
}

我会让您知道如何依次遍历。您可以将值放入向量或其他内容中,并在打印或任何您喜欢的其他东西之前使用 std :: sort 。不幸的是,地图是关联容器,您无法对它们进行排序,因为它破坏了它们的内部顺序。

I'll let you figure out how to traverse it in order. You can put the values in a vector or something and use std::sort before printing or whatever else you like. Maps, unfortunately, are associative containers and you can't sort them as it breaks their internal ordering.

std :: map 的背景信息

映射是一个关联数组,这意味着每个键都映射到特定值,并且键是唯一的。实际上,您可以创建键不是唯一的多重地图,因此这很重要。

A map is an associative array meaning that every key maps to a specific value, and keys are unique. You can actually create a multimap where keys are not unique, so that's why this is important.

基本上,由于键是唯一的,因此您可以通过以下方式访问或创建元素

Basically, since keys are unique, you can access or create an element just by using the key as the array index.

例如:

//Create a map and insert a couple things into it - prices of meat?
std::map<string, float> myMap;
myMap["Chicken"] = 4.99;
myMap["Turkey"] = 6.99;

//Retrieve the price of something using the key.
std::cout << "Chicken costs " << myMap["Chicken"] << std::end;

您也可以在地图上执行标准的插入和定位操作,但是关联数组语法更简单,那又何必呢? :)

You can do standard insertion and location operations on a map too, but the associative array syntax is just simpler, so why bother? :)

PS:为了完全回答您的评论,以防万一,myMap [tokenizedWord] ++末尾的++是只是说要为该键存储的整数值增加1。您也可以执行myMap [tokenizedWord] = myMap [tokenizedWord] + 1或也可以执行myMap [tokenizedWord] + = 1。

PS: To fully answer your comment, just in case, the ++ at the end of myMap[tokenizedWord]++ is just saying to increment the value of the integer value stored for that key by 1. You could just as well do myMap[tokenizedWord] = myMap[tokenizedWord] + 1 OR you could also do myMap[tokenizedWord] += 1.

这篇关于C ++,计算字符串中的重复单词并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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