简体   繁体   中英

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. 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 (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. 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.

Instead of a mixed structure like this, consider the following changes:

  • add a default constructor to your Pattern struct, initializing its members. For example if you initialize token to * , you can remove that if, and even the two bool variables required for it.
  • 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. (or if you really want to keep this find lookup, move them before the for loop - you only have to set them once!)
  • 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]; , and decrementing later, you could just write str = c1[j+1]

  • Also, are you sure you need that reverse ? That makes your relative +/-1 indexing unclear. For example, the c1[j+1 is j-1 in the original command string.

About the last one: that's probably a bug in your outer printing code, which you didn't post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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