简体   繁体   English

与 char 比较的二维数组

[英]2d array comparing with char

I have an array that reads data from a file, the data is binary digits such as 010011001001 and many others so the data are strings which I read in to my 2d array but I am stuck on comparing each value of the array to 0. Any help would be appreciated.我有一个从文件中读取数据的数组,数据是二进制数字,例如 010011001001 和许多其他数字,因此数据是我读入二维数组的字符串,但我坚持将数组的每个值与 0 进行比较。任何帮助将不胜感激。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string myArr[5000][12];
    int i = 0, zeroCount = 0, oneCount = 0;
    
    ifstream inFile;
    inFile.open("Day3.txt");
    
    while(!inFile.eof())
    {
        for(int i = 0; i < 5000; i++)
        {
            for(int j = 0; j < 12; j++)
            {
                inFile >> myArr[i][j];
                j++;
            }
            i++;
        }   
    }
    
    for(int j = 0; j < 12; j++)
    {
        for(int i = 0; i < 5000; i++)
        {
            if(myArr[i][j].compare("0") == 0)
            {
                zeroCount++;
            }
            else
            {
                oneCount++;
            }
            i++;
        }
        
        if(zeroCount > oneCount)
        {
            cout << "Gamma is zero for column " << i << endl;
        }
        else
        {
            cout << "Gamma is One for column " << i << endl;
        }       
        j++;
    }
}

some input from the text file: 010110011101 101100111000 100100000011 111000010001 001100010011 010000111100来自文本文件的一些输入:010110011101 101100111000 100100000011 111000010001 001100010011 010000111100

Thank you for editing you question and providing more information.感谢您编辑您的问题并提供更多信息。 Now, we can help you.现在,我们可以帮助您。 You have 2 major misunderstandings.你有两个主要的误解。

  1. How does a for loop work? for 循环是如何工作的?
  2. What is a std::string in C++ C++ 中的std::string是什么

Let us start with the for loop.让我们从 for 循环开始。 You find an explanation in the CPP reference here .您可以在此处的 CPP 参考中找到解释。 Or, you could look also at the tutorial shown here .或者,您也可以查看此处显示的教程。

The for loop has basically 3 parts: for (part1; part2; part3) . for 循环基本上有 3 个部分: for (part1; part2; part3) All are optional, you can use them, but no need to use them.都是可选的,你可以用,但没必要用。

  • part1 is the init-statement. part1 是初始化语句。 Here you can declare/define/initialize a variable.在这里您可以声明/定义/初始化一个变量。 In your case it is int i = 0 .在您的情况下,它是int i = 0 You define a variable of data type int and initialize it with a value of 0您定义一个数据类型为 int 的变量并将其初始化为 0
  • part2 is the condition.第 2 部分是条件。 The loop will run, until the condition becomes false.循环将运行,直到条件变为假。 The condition will be check at the beginning of the loop.条件将在循环开始时检查。
  • part3 is the so called iteration-expression. part3 就是所谓的迭代表达式。 The term is a little bit misguiding.这个词有点误导。 It is basically a statement that is executed at the end of the loop, before the next loop run will be executed and before the condition is checked again.它基本上是在循环结束时执行的语句,在执行下一个循环运行之前以及再次检查条件之前。

In Pseudo code it is something like this:在伪代码中是这样的:

{

    init-statement
    while ( condition ) {

        statement
        iteration-expression ;

    } 
} 

which means for the part of your code for(int j = 0; j < 12; j++)这意味着对于您的部分代码for(int j = 0; j < 12; j++)

{

    int j = 0;                 // init-statement
    while ( j < 12 ) {         // while ( condition ) {

       inFile >> myArr[i][j];  // Your loop statements
       j++;                    // Your loop statements  PROBLEM

       j++;                    // iteration-expression from the for loop
    } 
} 

And now you see the problem.现在你看到了问题。 You unfortunately increment 'j' twice.不幸的是,您将“j”增加了两次。 You do not need to do that.你不需要这样做。 The last part3 of the for loop does this for you already. for循环的最后部分 3 已经为您完成了这项工作。

So please delete the duplicated increment statements.所以请删除重复的增量语句。


Next, the std::string接下来, std::string

A string is, as its names says, a string of characters, or in the context of programming languages, an array of characters.字符串,顾名思义,是一串字符,或者在编程语言的上下文中,是一个字符数组。

In C we used to write actually char[42] = "abc";在 C 中,我们实际上写的是char[42] = "abc"; . . So using really a array of characters.所以使用真正的字符数组。 The problem was always the fixed length of such a string.问题始终是这样一个字符串的固定长度。 Here for example 42. In such an array you could store only 41 characters.这里以 42 为例。在这样的数组中,您只能存储 41 个字符。 If the string would be longer, then it could not work.如果字符串更长,那么它就无法工作。

The inventors of C++ solved this problem. C++的发明者解决了这个问题。 They created a dynamic character array, an array that can grow, if needed.他们创建了一个动态字符数组,一个可以根据需要增长的数组。 They called this thing std::string .他们称这个东西为std::string It does not have a predefined length.它没有预定义的长度。 It will grow as needed.它将根据需要增长。

Therefore, writing string myArr[5000][12];因此,写string myArr[5000][12]; shows that you did not fully understand this concept.说明你没有完全理解这个概念。 You do not need [12] , becuase the string can hold the 12 characters already.您不需要[12] ,因为string已经可以容纳 12 个字符。 So, you can delete it.所以,你可以删除它。 They characters will implicitely be there.他们的角色将隐含地在那里。 And if you write inFile >> myString then the extractor operator >> will read characters from the stream until the next space and then store it in your myString variable, regardless how long the string is.如果你写inFile >> myString那么提取操作符>>将从 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 中读取字符直到下一个空格,然后将它存储在你的 myString 变量中,不管字符串有多长。

Please read this tutorial about strings.请阅读有关字符串的本教程

That is a big advantage over the C-Style strings.这是 C-Style 字符串的一大优势。

Then your code could look like:然后您的代码可能如下所示:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    string myArr[5000];
    int zeroCount = 0, oneCount = 0;

    ifstream inFile;
    inFile.open("Day3.txt");

    while (!inFile.eof())
    {
        for (int i = 0; i < 5000; i++)
        {
            inFile >> myArr[i];
        }
    }

    for (int i = 0; i < 5000; i++)
    {
        zeroCount = 0; oneCount = 0;
        for (int j = 0; j < 12; j++) 
        {

            if (myArr[i][j]== '0')
            {
                zeroCount++;
            }
            else
            {
                oneCount++;
            }
        }

        if (zeroCount > oneCount)
        {
            cout << "Gamma is zero for column " << i << endl;
        }
        else
        {
            cout << "Gamma is One for column " << i << endl;
        }
    }
}

But there is more.但还有更多。 You use the magic number 5000 for your array of strings.您对字符串数组使用幻数 5000。 This you do, because you think that 5000 is always big enough to hold all strings.你这样做是因为你认为 5000 总是足够大以容纳所有字符串。 But what, if not?但是,如果不是呢? If you have more than 5000 strings in your source file, then your code will crash.如果您的源文件中有超过 5000 个字符串,那么您的代码将会崩溃。

Similar to the string problem for character arrays, we have also a array for any kind of data in C++, that can dynamically grow as needed.与字符 arrays 的字符串问题类似,我们在 C++ 中也有一个数组,可以根据需要动态增长。 It is called std::vector and you can read about it here .它被称为std::vector ,您可以在此处阅读有关它的信息。 A tutorial can be found here .可以在此处找到教程。

With that you can get rid of any C-Style array at all.有了它,您就可以完全摆脱任何 C 样式数组。 But please continue to study the language C++ further and you will understand more and more.但是请继续深入学习 C++ 语言,你会越来越了解。

Ther are more subtle problems in your code like while(.inFile.eof()) , but this should be solved later.您的代码中有更微妙的问题,例如while(.inFile.eof()) ,但这应该稍后解决。

I hope I could help我希望我能帮上忙

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM