在C++中发现三个字符串中所有字符的出现次数

在C++中发现三个字符串中所有字符的出现次数,c++,C++,我不明白为什么这个程序不起作用 我需要通过任何方法找到三个字符串中所有字符的出现次数 我用了计数法,但若你们能帮我找到函数,那个就更好了 #include<iostream> #include<algorithm> using namespace std; int main() { string line[3]; int count[3]; cout << "Enter three lines of text...\n\n";

我不明白为什么这个程序不起作用

我需要通过任何方法找到三个字符串中所有字符的出现次数

我用了计数法,但若你们能帮我找到函数,那个就更好了

#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    string line[3];
    int count[3];
    cout << "Enter three lines of text...\n\n";
    cin >> line[0];
    cin >> line[1];
    cin >> line[2];
    int i;
    for(char j='a'; j<=26; j++) {
        for(i=0; i<3; i++)
            count[i] = std::count(line[i].begin(), line[i].end(), j);
        cout << "\n" << j << "\t" << ":" << "\t" << count[i];
    }

    system ("pause");
    return 0;
}
小写字母“a”为96。这就是为什么你的循环不执行的原因。尝试:

for (char j = 'a'; j <= 'z'; j++)
这将只计算小写字符。如果希望出现小写和大写,可以执行以下操作:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string line[3];

    cout << "Enter three lines of text...\n\n";
    cin >> line[0];
    cin >> line[1];
    cin >> line[2];

    for(char j='a';j<='z';j++)
    {
        int upper_sum = 0;
        int lower_sum = 0;

        for(int i=0;i<3;i++)
        {
            lower_sum += std::count(line[i].begin(),line[i].end(),j);
            upper_sum += std::count(line[i].begin(),line[i].end(),j - 32); //32 = 'a' - 'A'
        }

        cout<<"\n"<<j<<"\t"<<":"<<"\t"<<lower_sum;
        cout<<"\n"<<(char)(j - 32)<<"\t"<<":"<<"\t"<<upper_sum;
    }
    return 0;
}
小写字母“a”为96。这就是为什么你的循环不执行的原因。尝试:

for (char j = 'a'; j <= 'z'; j++)
这将只计算小写字符。如果希望出现小写和大写,可以执行以下操作:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string line[3];

    cout << "Enter three lines of text...\n\n";
    cin >> line[0];
    cin >> line[1];
    cin >> line[2];

    for(char j='a';j<='z';j++)
    {
        int upper_sum = 0;
        int lower_sum = 0;

        for(int i=0;i<3;i++)
        {
            lower_sum += std::count(line[i].begin(),line[i].end(),j);
            upper_sum += std::count(line[i].begin(),line[i].end(),j - 32); //32 = 'a' - 'A'
        }

        cout<<"\n"<<j<<"\t"<<":"<<"\t"<<lower_sum;
        cout<<"\n"<<(char)(j - 32)<<"\t"<<":"<<"\t"<<upper_sum;
    }
    return 0;
}

26不是字母,char26通常小于“a”-因此循环不会执行。试试char j='a';j26不是字母,char26通常小于'a',因此循环不会执行。试试char j='a';j我喜欢这样的东西:

#include <map>
#include <string>
#include <iostream>

int main()
{
    // Given 3 strings...
    std::string s1 = "Hello";
    std::string s2 = "Cruel";
    std::string s3 = "World";

    //===============================================
    // THESE 2 LINES ARE ALL YOU NEED FOR COUNTING
    std::map<char, int> countMap;
    for (char c : (s1 + s2 + s3)) { countMap[c]++; }
    //===============================================

    // Print the output. This is if you do not care about
    // characters that do not appear at all.
    for (auto const& e : countMap)
    {
        std::cout << e.first << ": " << e.second << std::endl;
    }

    // Print the output. This is if you DO care about
    // characters that do not appear at all.
    for (char c = ' '; c <= '~'; c++)
    {
        std::cout << c << ": " << countMap[c] << std::endl;
    }
}

我会选择这样的方式:

#include <map>
#include <string>
#include <iostream>

int main()
{
    // Given 3 strings...
    std::string s1 = "Hello";
    std::string s2 = "Cruel";
    std::string s3 = "World";

    //===============================================
    // THESE 2 LINES ARE ALL YOU NEED FOR COUNTING
    std::map<char, int> countMap;
    for (char c : (s1 + s2 + s3)) { countMap[c]++; }
    //===============================================

    // Print the output. This is if you do not care about
    // characters that do not appear at all.
    for (auto const& e : countMap)
    {
        std::cout << e.first << ": " << e.second << std::endl;
    }

    // Print the output. This is if you DO care about
    // characters that do not appear at all.
    for (char c = ' '; c <= '~'; c++)
    {
        std::cout << c << ": " << countMap[c] << std::endl;
    }
}

以下是您的程序的正确版本:

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
string line[3];
int count[3];
/*cout<<"Enter three lines of text...\n\n";
cin>>line[0];
cin>>line[1];
cin>>line[2];*/

      line[0] = "aaaaabbbbbbbbbbb";
      line[1] = "ccccccddddddddddd";
      line[2] = "kkkkkkkkkkkkk";

     int i;
     for(char j='a';j<= 'z';j++) // You can loop through the alphabet, but you need to go 'a' to 'z' and not 26
     { 

             for(i=0;i<3;i++) 
             { // You were missing these braces,  you need them if your loop contains multiple lines
               count[i]= std::count(line[i].begin(),line[i].end(),j);
               cout<<"\n"<<j<<"\t"<<":"<<"\t"<<count[i];
             }
    }

    system ("pause");
    return 0;
}


这是您的程序的正确版本:

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
string line[3];
int count[3];
/*cout<<"Enter three lines of text...\n\n";
cin>>line[0];
cin>>line[1];
cin>>line[2];*/

      line[0] = "aaaaabbbbbbbbbbb";
      line[1] = "ccccccddddddddddd";
      line[2] = "kkkkkkkkkkkkk";

     int i;
     for(char j='a';j<= 'z';j++) // You can loop through the alphabet, but you need to go 'a' to 'z' and not 26
     { 

             for(i=0;i<3;i++) 
             { // You were missing these braces,  you need them if your loop contains multiple lines
               count[i]= std::count(line[i].begin(),line[i].end(),j);
               cout<<"\n"<<j<<"\t"<<":"<<"\t"<<count[i];
             }
    }

    system ("pause");
    return 0;
int i;
for(char j='a'; j<=26; j++) {
    for(i=0; i<3; i++)
        count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}
}

在这里行动

int i;
for(char j='a'; j<=26; j++) {
    for(i=0; i<3; i++)
        count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}
最后一行不会编译,因为我在循环结束时超出了范围。我假设您错误地认为cout是循环的一部分,部分原因是代码格式不好,其次是因为我在更大的范围内声明

要更正此问题,请使用{和}为for循环创建一个块。这将使i保持在块内所有语句的范围内,并在循环的每次迭代中重复这些语句

for(int i=0; i<3; i++) {
    count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}
最后一行不会编译,因为我在循环结束时超出了范围。我假设您错误地认为cout是循环的一部分,部分原因是代码格式不好,其次是因为我在更大的范围内声明

要更正此问题,请使用{和}为for循环创建一个块。这将使i保持在块内所有语句的范围内,并在循环的每次迭代中重复这些语句

for(int i=0; i<3; i++) {
    count[i] = std::count(line[i].begin(), line[i].end(), j);
    cout << "\n" << j << "\t" << ":" << "\t" << count[i];
}


如果您正确格式化了代码,那么几个错误中至少有一个会立即显现出来。@user1885560我不明白,您能显示您想要的输出格式吗?我需要以一种方式输出,每个字符的出现次数应该是分开的…像这样。。。a:号码b:号码。。。等等……如果您正确格式化了代码,那么几个错误中至少有一个会立即显现出来。@user1885560我不明白,您能显示您想要的输出格式吗?我需要以一种方式输出,每个字符的出现次数应该是分开的……如下所示。。。a:号码b:号码。。。以此类推…但这样我不会得到三行的所有计数…它只会给出第三行的输出。。。因为在cout语句中,i的值是3。。。我需要计数[0]+计数[1]+计数[2]。。。我将如何得到这个..?为i=3的东西编辑。好的,那么你想要所有字符串中一个字母的总计数?是的,是的。。。确切地现在你得到了我想要计算的。。。所有三个字符串中每个字母的编号。。组合..谢谢。。。让我试试这个。。。我会在一小时后告诉你它是否有效…这个to0不起作用。。。它的输出像。。1940080每个字符都有这么多值…但这样我就不会得到三行的所有计数…它只会给出第三行的输出。。。因为在cout语句中,i的值是3。。。我需要计数[0]+计数[1]+计数[2]。。。我将如何得到这个..?为i=3的东西编辑。好的,那么你想要所有字符串中一个字母的总计数?是的,是的。。。确切地现在你得到了我想要计算的。。。所有三个字符串中每个字母的编号。。组合..谢谢。。。让我试试这个。。。我会在一小时后告诉你它是否有效…这个to0不起作用。。。它的输出像。。1940080每个字符有这么多值…for循环下面的cout行在循环之外。。。我从来不想让它成为循环的一部分。。这不是糟糕的编程至少我知道。。。我没有放括号,因为我希望它保持在循环之外。。。好了,我已经完成了这个程序,我把count[]重新初始化为零…但是cout在那里没有意义,因为它输出的是count[3],这是未定义的。有效值为count[0]、count[1]和count[2]。for循环下面的cout行在循环外部。。。我从来不想让它成为循环的一部分。。这不是糟糕的编程至少我知道。。。我没有放括号,因为我希望它保持在循环之外。。。好了,我已经完成了这个程序,我把count[]重新初始化为零…但是cout在那里没有意义,因为它输出的是count[3],这是未定义的。有效值为count[0]、count[1]和count[2]。循环不需要大括号。。。为什么每个人
当我不想让cout线包含在循环中时,我需要放大括号…我不需要循环中的大括号。。。为什么当我不希望cout行包含在循环中时,每个人都在放大括号。。。