C++ 正确使用';继续';陈述
我的文本文件中有以下列表:C++ 正确使用';继续';陈述,c++,loops,continue,C++,Loops,Continue,我的文本文件中有以下列表: ABCD 1234 3456 ABCD 5678 7890 ABCD 4567 我的代码必须遍历文本文件,每次它看到字符串“ABCD”,我必须将字符串后面的4位条目存储到一个链接列表中。我得到了一切工作,但我的代码中唯一有麻烦的部分是将条目放入单独的链接列表中 例如,1234和3456必须放入列表1,5678和7890必须放入列表2,4567必须放入列表3。我在写循环时遇到了问题,我写循环是为了将这些条目分开,并将它们放入适当的列表中 以下是我的代码部分: //I
ABCD
1234
3456
ABCD
5678
7890
ABCD
4567
我的代码必须遍历文本文件,每次它看到字符串“ABCD”,我必须将字符串后面的4位条目存储到一个链接列表中。我得到了一切工作,但我的代码中唯一有麻烦的部分是将条目放入单独的链接列表中
例如,1234
和3456
必须放入列表1,5678
和7890
必须放入列表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
}
}
在第一个条目添加到任何列表之后,没有其他内容。有人能帮我找出哪里出了问题吗?您发布的代码没有问题,因此问题一定是read file或list类的问题。以下是一个工作示例:
#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;
}
#包括
#包括
#包括
#包括
int main()
{
标准::向量字={
“ABCD”、“1234”、“3456”、“ABCD”、“5678”、“7890”、“ABCD”、“4567”};
std::列表列表1、列表2、列表3;
int计数器=0;
for(自动字:字)
{
if(isdigit(字[0]))
{
int num=std::stoi(字);
if(counter==1)list1.push_back(num);
if(counter==2)list2.push_back(num);
if(counter==3)list3.push_back(num);
}
其他的
{
计数器++;
}
}
用于(自动编号:列表1)std::难道所有的continue
都是多余的,所以我看不出它们与算法的逻辑有什么关联。你能把问题说得更清楚吗?如果counter
大于1,你想怎么办?只有当counter
为0或1时,你才存储数字。continue;//返回for循环的开头
-一个误导ing comment.@LightnessRacesinOrbit我删除了continue
s。为了让我的问题更清楚,循环必须遍历列表,每当它遇到非数字/遇到字母表时,它必须继续并将任何数字存储到列表中。然后,当你遇到另一个APhabe时,后面的数字应该存储在列表中下一张名单,等等on@AntoJurković在我的特定情况下,计数器不会超过2,因为在测试代码的文本文件中,代码只会遇到三个实例
#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;
}