简体   繁体   English

字符串C ++的迭代向量

[英]iterating vector of strings C++

The code is to read instructions from text file and print out graphic patterns. 该代码是从文本文件中读取指令并打印出图形图案。 One is my function is not working properly. 一是我的功能无法正常工作。 The function is to read the vectors of strings I've got from the file into structs. 功能是将我从文件中获得的字符串向量读入结构。

Below is my output, and my second, third, and sixth graphs are wrong. 以下是我的输出,第二,第三和第六张图是错误的。 It seems like the 2nd and 3rd vectors are not putting the correct row and column numbers; 似乎第二和第三向量没有正确输入行号和列号。 and the last one skipped "e" in the alphabetical order. 而最后一个按字母顺序跳过了“ e”。 I tried to debug many times and still can't find the problem. 我尝试调试了很多次,但仍然找不到问题。

  typedef struct Pattern{
    int rowNum;
    int colNum;
    char token;
    bool isTriangular;
    bool isOuter;
}Pattern;
void CommandProcessing(vector<string>& , Pattern& );
int main()
{
 for (int i = 0; i < command.size(); i++)
    {
        Pattern characters;
        CommandProcessing(command[i], characters);

    }

    system("pause");
    return 0;
}

 void CommandProcessing(vector<string>& c1, Pattern& a1)
    {
        reverse(c1.begin(), c1.end());
        string str=" ";


        for (int j = 0; j < c1.size(); j++)
        {

            bool foundAlpha = find(c1.begin(), c1.end(), "alphabetical") != c1.end();
            bool foundAll = find(c1.begin(), c1.end(), "all") != c1.end();
            a1.isTriangular = find(c1.begin(), c1.end(), "triangular") != c1.end() ? true : false;
            a1.isOuter = find(c1.begin(), c1.end(), "outer") != c1.end() ? true : false;

            if (foundAlpha ==false && foundAll == false){
                a1.token = '*';
            }
            //if (c1[0] == "go"){
            else if (c1[j] == "rows"){
                str = c1[++j];
                a1.rowNum = atoi(str.c_str());
                j--;
            }
            else if (c1[j] == "columns"){
                str = c1[++j];
                a1.colNum = atoi(str.c_str());
                j--;
            }
            else if (c1[j] == "alphabetical")
                a1.token = 0;

            else if (c1[j] == "all"){
                str = c1[--j];
                a1.token = *str.c_str();
                j++;
            }

        }

    }

命令格式

我的输出

Before debugging (or posting) your code, you should try to make it cleaner. 在调试(或发布)代码之前,应尝试使其变得更整洁。 It contains many strange / unnecessary parts, making your code harder to understand (and resulting in the buggy behaviour you just described). 它包含许多奇怪/不必要的部分,使您的代码更难以理解(并导致您刚才描述的错误行为)。

For example, you have an if in the beginning: 例如,您开头有一个if:

if (foundAlpha ==false && foundAll == false){

If there is no alpha and all command, this will be always true, for the entire length of your loop, and the other commands are all placed in else if statements. 如果没有alpha和all命令,那么对于循环的整个长度来说,这始终是正确的,其他命令都放在else if语句中。 They won't be executed. 他们将不会被处决。

Because of this, in your second and third example, no commands will be read, except the isTriangular and isOuter flags. 因此,在第二个和第三个示例中,除了isTriangularisOuter标志外,将不读取任何命令。

Instead of a mixed structure like this, consider the following changes: 代替以下混合结构,请考虑以下更改:

  • add a default constructor to your Pattern struct, initializing its members. 向您的Pattern结构添加默认构造函数,以初始化其成员。 For example if you initialize token to * , you can remove that if, and even the two bool variables required for it. 例如,如果将token初始化为* ,则可以删除if,甚至它所需的两个bool变量。
  • Do the parsing in one way, consistently - the easiest would be moving your triangular and outer bool to the same if structure as the others. 始终以一种方式进行解析-最简单的方法是将三角形和外部bool移动到与其他bool相同的结构。 (or if you really want to keep this find lookup, move them before the for loop - you only have to set them once!) (或者,如果您真的想保留此find查找,请将它们移至for循环之前-您只需设置一次即可!)
  • Do not modify your loop variable ever, it's an error magnet! 永远不要修改循环变量,这是一个错误磁铁! Okay, there are some rare exceptions for this rule, but this is not one of them. 好的,此规则有一些罕见的例外,但这不是其中之一。

    Instead of str = c1[++j]; 代替str = c1[++j]; , and decrementing later, you could just write str = c1[j+1] ,然后递减,您可以只写str = c1[j+1]

  • Also, are you sure you need that reverse ? 另外,您确定需要reverse吗? That makes your relative +/-1 indexing unclear. 这使您的相对+/- 1索引不清楚。 For example, the c1[j+1 is j-1 in the original command string. 例如,原始命令字符串中的c1[j+1j-1

About the last one: that's probably a bug in your outer printing code, which you didn't post. 关于最后一个:这可能是您的outer打印代码中的一个错误,您没有发布。

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

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