正确使用 'continue' 语句

Proper usage of 'continue' statement

我在文本文件中有以下列表:

ABCD
1234
3456
ABCD
5678
7890
ABCD
4567

我的代码必须遍历文本文件,每次它看到字符串 "ABCD",我必须将字符串 后面 的 4 位条目存储到一个链表。我让一切正常工作,但我遇到问题的唯一代码部分是将条目放入单独的链表中。

例如,12343456必须放入列表1,56787890必须放入列表2,4567 必须放入列表 3。我在编写用于分隔这些条目并将它们放入适当列表的循环时遇到了问题。

这是我的代码部分:

//I have all elements stored initially into a string vector named "words".
int counter = 0;
for (int i = 0; i < words.size(); i++) {  //loop through entire vector
    string check = words[i];
    if (isdigit(check[0])) {    //check if first character is a digit
        numbers = words[i];     
        num = stoi(numbers);    //4 digit number converted back to integer
        if (counter == 1) {     
            list1.Add(num);   //Add is a function of type linked list          
        }
        if (counter == 2) {     
            list2.Add(num);
        }
        if (counter == 3) {     
            list3.Add(num);
        }
    }
    else {    //if its not a digit
        counter = counter + 1;    //increase the counter   
    }
}

这是我得到的输出:

1234

将第一个条目添加到任何列表后,没有其他内容。谁能帮我弄清楚哪里出错了?

您发布的代码没有问题,因此问题一定出在读取文件或您的列表 class 上。这是一个工作示例:

#include<iostream>
#include<string>
#include<vector>
#include<list>

int main()
{
    std::vector<std::string> words = {
        "ABCD", "1234", "3456", "ABCD", "5678", "7890", "ABCD", "4567"  };

    std::list<int> list1, list2, list3;
    int counter = 0;
    for (auto word : words)
    {
        if (isdigit(word[0]))
        {    
            int num = std::stoi(word);
            if (counter == 1) list1.push_back(num); 
            if (counter == 2) list2.push_back(num);
            if (counter == 3) list3.push_back(num);
        }
        else 
        {    
            counter++;    
        }
    }

    for (auto n : list1) std::cout << n << "\n";
    for (auto n : list2) std::cout << n << "\n";
    for (auto n : list3) std::cout << n << "\n";

    return 0;
}