简体   繁体   English

function 将字符串作为输入并返回最常见的字符

[英]function which takes a string as input and returns the most frequent character

I wrote a feature.我写了一个功能。 And it works correctly.它工作正常。 But in my opinion it is not optimal.但在我看来,这不是最优的。 But I have not found what standard features you can use to unleash it.但是我还没有找到可以用来释放它的标准功能。 Since I made it a simple sort of values.因为我把它变成了一种简单的值。 And it takes a long time to execute而且执行需要很长时间

char getMaxOccuringChar(char* str) 
{ 
    int count[ASCII_SIZE] = {0}; 

    int len = strlen(str); 
    int max = 0; 
    char result; 

    for (int i = 0; i < len; i++) { 
        count[str[i]]++; 
        if (max < count[str[i]]) { 
            max = count[str[i]]; 
            result = str[i]; 
        } 
    } 

    return result; 
} 

Here's how I'd write this using the standard library:这是我使用标准库编写它的方式:

#include <algorithm>
#include <unordered_map>
#include <string_view>

char most_frequent_char(std::string_view str) {
    std::unordered_map<char, int> counts;
    for (auto c : str) counts[c] += 1;
    return std::max_element(
        begin(counts), end(counts),
        [](auto a, auto b) {return a.second < b.second;}
    )->first;
}

But to be honest I'm not happy with manually iterating over the string to counts its characters.但老实说,我对手动迭代字符串以计算其字符不满意。 In actual code I'd probably abstract away the creation of a frequency table , which would presumably also have a max function.在实际代码中,我可能会抽象出频率表的创建,它可能还有一个max function。 In Python this directly corresponds to the collections.Counter class.在 Python 中,这直接对应于collections.Counter class。 If we assume the existence of this utility class (left as an exercise to the reader), the implementation becomes如果我们假设存在此实用程序 class(留给读者作为练习),则实现变为

char most_frequent_char(std::string_view str) {
    return max(freq_table{str}).first;
}

Use a map and max_element使用 map 和 max_element

char getMaxOccuringChar(char* str) 
{ 
    auto s = std::string(str);
    auto cmap = std::map<char, size_t>{}
    for (auto c : s) {
      cmap[c]++;
    }

    auto found = std::max_element(cmap.begin(), cmap.end(),
        [] (auto p1, auto p2) {
            return p1.second < p2.second;
        }
    );

    return found.first;
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现一个 function ,它将一个字符串作为输入并返回一个没有用“!”替换的辅音字母的新字符串 - Implement a function which takes a string as an input and returns a new one without consonant letters replaced with “!” 范围内最常见的角色 - Most frequent character in range 创建可变函数,它接受任意函子并返回输入函子的每个返回值的元组 - Make variadic function which takes arbitary functors and returns a tuple of each return value of input functors C ++函数中的错误,它返回字符串中最常见的字符。 多字节字符? - mistake in C++ function that returns most common character in a string. Multibyte characters? Function 接受 integer 输入并返回一个字符串 - Function that takes integer inputs and returns a string function 将字符串作为输入并将其拆分为单词。 标点符号应该被忽略 - function which takes a string as input and splits it into words. Punctuation characters should be ignored 维护状态的正则表达式库,一个字符一个字符地输入,发现匹配时返回true - Regular Expression library that maintains state, takes input character by character and returns true whenever match is found 可以写一个 function 接受一个类型并返回一个不同的类型 - Can one write a function which takes a type and returns a different type 是否有函数返回.txt中某个点的字符/字符串? - Is there a function that returns the character/string at a point in a .txt? isalpha()函数为string中的字符返回false - isalpha() function returns false for character in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM