如何按降序c+打印字符串中每个字母的频率+;? #包括 #包括 使用名称空间std; int main(){ int-cnt[26]{}; char-alpha[26]; 字符串s=“abcdefffgghiii”; 对于(int i=0;irhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。std::sort(cnt.begin(),cnt.end(),[](const-LetterFreq&lhs,const-LetterFreq&rhs){return lhs.freq>rhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { // Create result container auto x = vector<pair<char, int>>(); std::string s = "abcdefffggghiii"; for (auto& l : s) { // Find the item that corresponds to letter auto pLetter = find_if(x.begin(), x.end(), [&l](pair<char, int> &arg) { return arg.first == l; }); if (pLetter != x.end()) pLetter->second++; // If item corresponding to letter is found, increment count else { x.push_back(make_pair(l, 1)); // Otherwise, create a new result entry } } // Sort results by count in descending order std::sort(x.begin(), x.end(), [](auto &left, auto &right) { return left.second > right.second; }); for (auto i = x.begin(); i != x.end(); ++i) std::cout << i->first << ':' << i->second << '\n'; } f:3 g:3 i:3 a:1 b:1 c:1 d:1 e:1 h:1

如何按降序c+打印字符串中每个字母的频率+;? #包括 #包括 使用名称空间std; int main(){ int-cnt[26]{}; char-alpha[26]; 字符串s=“abcdefffgghiii”; 对于(int i=0;irhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。std::sort(cnt.begin(),cnt.end(),[](const-LetterFreq&lhs,const-LetterFreq&rhs){return lhs.freq>rhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { // Create result container auto x = vector<pair<char, int>>(); std::string s = "abcdefffggghiii"; for (auto& l : s) { // Find the item that corresponds to letter auto pLetter = find_if(x.begin(), x.end(), [&l](pair<char, int> &arg) { return arg.first == l; }); if (pLetter != x.end()) pLetter->second++; // If item corresponding to letter is found, increment count else { x.push_back(make_pair(l, 1)); // Otherwise, create a new result entry } } // Sort results by count in descending order std::sort(x.begin(), x.end(), [](auto &left, auto &right) { return left.second > right.second; }); for (auto i = x.begin(); i != x.end(); ++i) std::cout << i->first << ':' << i->second << '\n'; } f:3 g:3 i:3 a:1 b:1 c:1 d:1 e:1 h:1,c++,string,C++,String,您可以使用pairs,但看起来您使用的是更基本的类型。在这种情况下,您可能必须使用嵌套循环。继续查找频率最高的字符,打印它,然后将其频率设置为-1,以指示您已经处理了它。您可以使用pairs,但看起来您正在这样做更基本的类型。在这种情况下,您可能必须使用嵌套循环。继续查找频率最高的字符,打印它,然后将其频率设置为-1,以表示您已经处理了它。以下是我可以做的。您只需将字母和计数保持在一起 #include <iostream> #include <string> using

您可以使用pairs,但看起来您使用的是更基本的类型。在这种情况下,您可能必须使用嵌套循环。继续查找频率最高的字符,打印它,然后将其频率设置为-1,以指示您已经处理了它。

您可以使用pairs,但看起来您正在这样做更基本的类型。在这种情况下,您可能必须使用嵌套循环。继续查找频率最高的字符,打印它,然后将其频率设置为-1,以表示您已经处理了它。

以下是我可以做的。您只需将字母和计数保持在一起

#include <iostream>
#include <string>
using namespace std;
int main () {
    int cnt[26] {};
    char alpha[26];
        string s = "abcdefffggghiii";
        for (int i = 0; i < s.length(); i++) {
                cnt[s[i] - 'a']++;
        }

    for (int i = 'a'; i <= 'z'; i++) {
        alpha[i - 'a'] = i;
    }
    for (int i = 0; i < 26; i++) {
        if (cnt[i]) {
            cout << alpha[i] << " " << cnt[i] << endl;
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
结构LetterFreq
{
字符字母;
国际频率;
};
int main()
{
std::载体cnt(26);
对于(size_t i=0;i右侧频率;
});
用于(自动和项目:cnt)
{
如果(item.freq==0)
{
打破
}

std::cout我可以这样做。你只需要把信和计数放在一起

#include <iostream>
#include <string>
using namespace std;
int main () {
    int cnt[26] {};
    char alpha[26];
        string s = "abcdefffggghiii";
        for (int i = 0; i < s.length(); i++) {
                cnt[s[i] - 'a']++;
        }

    for (int i = 'a'; i <= 'z'; i++) {
        alpha[i - 'a'] = i;
    }
    for (int i = 0; i < 26; i++) {
        if (cnt[i]) {
            cout << alpha[i] << " " << cnt[i] << endl;
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
结构LetterFreq
{
字符字母;
国际频率;
};
int main()
{
std::载体cnt(26);
对于(size_t i=0;i右侧频率;
});
用于(自动和项目:cnt)
{
如果(item.freq==0)
{
打破
}
标准::cout
您可以运行它。它使用C++14 lambdas作为find_if和sort谓词。此解决方案与@Retired Ninja的非常类似,只是结果向量只包含那些计数非零的字母的项。这意味着它可以扩展到wstring,而不需要大的结果向量


您可以运行它。它使用C++14 lambdas作为find_if和sort谓词。此解决方案与@Retired Ninja非常类似,只是结果向量只包含那些计数非零的字母的项。这意味着它可以扩展到wstring,而不需要大的结果向量。

cnt
a进行排序rray将无法跟踪元素的原始索引,因此您需要以某种方式跟踪它。此外,从字母中减去
'a'
也不能保证为所有字符集提供介于
0
25
之间的值(它适用于ASCII或兼容字符集,但不适用于其他字符集)。对
cnt
数组进行排序将丢失元素的原始索引,因此您需要以某种方式跟踪它。此外,从字母中减去
'a'
也不能保证为所有字符集提供介于
0
25
之间的值(它适用于ASCII或兼容字符集,但不适用于其他字符集)。
std::sort(cnt.begin(),cnt.end(),[](const-LetterFreq&lhs,const-LetterFreq&rhs){return lhs.freq>rhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写<代码>运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。
std::sort(cnt.begin(),cnt.end(),[](const-LetterFreq&lhs,const-LetterFreq&rhs){return lhs.freq>rhs.freq;}我以前从来没有见过这个特性,它是基于C++ 14的吗?它是lambda函数。你需要C++ 11。我在类中使用过或重写<代码>运算符> /Cube >,并且用任何一种方式都可能混淆。LAMBDA可以做的比简单的多,但对于排序来说,它会为O节省大量的样板代码。运算符或函数,代码就在您可以看到的地方。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {

  // Create result container    
  auto x = vector<pair<char, int>>();

  std::string s = "abcdefffggghiii";
  for (auto& l : s) {    
    // Find the item that corresponds to letter
    auto pLetter =
        find_if(x.begin(), x.end(), [&l](pair<char, int> &arg) {
          return arg.first == l;
        });

    if (pLetter != x.end())
      pLetter->second++;   // If item corresponding to letter is found, increment count
    else {
      x.push_back(make_pair(l, 1)); // Otherwise, create a new result entry
    }
  }

  // Sort results by count in descending order
  std::sort(x.begin(), x.end(),
            [](auto &left, auto &right) { return left.second > right.second; });

  for (auto i = x.begin(); i != x.end(); ++i)
    std::cout << i->first << ':' << i->second << '\n';
}
f:3
g:3
i:3
a:1
b:1
c:1
d:1
e:1
h:1