C++ 使用stringstream和getline读取每行的前两个数字
我需要做的是:我现在有一个直线向量,C++ 使用stringstream和getline读取每行的前两个数字,c++,getline,stringstream,istringstream,C++,Getline,Stringstream,Istringstream,我需要做的是:我现在有一个直线向量,v[0]是第一行,依此类推。我想把每一行的第一个数字读作挑战,把每一行的第二个数字读作法官,然后应用守则中的条件。我想使用stringstream读取并从行中提取数字 我的代码现在正在做什么:它只读取每行的第一个数字。所以第一行的第一个数字是挑战,第二行的第一个数字是裁判,第三行的第一个数字是挑战 std::vector<string> v; string line; int i; double challenge;
v[0]
是第一行,依此类推。我想把每一行的第一个数字读作挑战,把每一行的第二个数字读作法官,然后应用守则中的条件。我想使用stringstream
读取并从行中提取数字
我的代码现在正在做什么:它只读取每行的第一个数字。所以第一行的第一个数字是挑战,第二行的第一个数字是裁判,第三行的第一个数字是挑战
std::vector<string> v;
string line;
int i;
double challenge;
int judge;
while (getline(cin, line)) {
if (line.empty()) {
break;
}
v.push_back(line);
}
for (i = 0; i < v.size();i++ ) {
cin >> v[i];
std::stringstream ss(v[i]);
ss << v[i];
ss >> challenge >> judge;
if (challenge < 1 || challenge > 5) {
cout << "bad_difficulty" << endl; //must add the condition or empty
v.erase(v.begin() + i);
}
if (judge != 5 || judge != 7 ) {
cout << "bad_judges" << endl; //must add the condition or empty
v.erase(v.begin() + i);
}
cout << v[i] << endl;
}
return 0;
}
让我们遍历删除循环
i = 0
v[0] contains line 1.
Line 1 is 5.1 7
challenge > 5, remove 0. The vector shifts up by 1 v[0] now contains line 2
Judge == 7 do not remove 0
increment i
i = 1
v[1] contains line 3. Line 2 has been skipped
Line 3 is 2.2 5
challenge < 5, do not remove 1.
Judge is not 5 or 7. remove 1. The vector shifts up by 1 v[1] now contains line 4
increment i
i = 2
v[2] contains line 5. Line 4 has been skipped
Line 5 is 2.1 7
challenge < 5, do not remove 2.
Judge is 7. do not remove 2.
increment i
i = 3
v[3] contains line 6.
Line 6 is 3.8 5
challenge < 5, do not remove 3.
Judge is 5. do not remove 3.
increment i
i = 3
v[3] contains line 7.
Line 7 is 2.0
challenge < 5, do not remove 3.
Judge is UNDEFINED! PANIC! PANIC! Crom only knows what happens.
increment i
i=0
v[0]包含第1行。
第1行是5.17
挑战>5,移除0。向量上移1V[0]现在包含第2行
判断==7不删除0
增量i
i=1
v[1]包含第3行。第2行已跳过
第3行是2.25
挑战<5,不要移除1。
法官不是5岁或7岁。移除1。向量上移1V[1]现在包含第4行
增量i
i=2
v[2]包含第5行。第4行已跳过
第5行是2.17
挑战<5,不要移除2。
法官7岁。不要拆下2。
增量i
i=3
v[3]包含第6行。
第6行是3.8.5
挑战<5,不要移除3。
法官5岁。不要拆下3。
增量i
i=3
v[3]包含第7行。
第7行是2.0
挑战<5,不要移除3。
法官没有定义!恐慌!恐慌!克罗姆只知道会发生什么。
增量i
无论如何,这里的基本模式应该很清楚。从向量
中删除元素时,所有后续元素都会上移。解决方案:删除元素时,不要增加i
。else
语句将为您解决此问题
其次,由于存在两个独立的if
语句,因此这两个条件都可能为真,并且v[i]
将被删除两次。有很多方法可以解决这个问题。Manthan Tilva使用continue
的解决方案简单有效,但如果使用else if
或将两个测试滚动到同一个if
中,则可以更明显地处理这一问题
第三,使用未从流中读取的值是未定义的。不应使用。不要再看了,就丢弃这条线<代码>如果(ss>>挑战>>判断)在这里会有帮助。问题是什么<代码>法官!=5 | |法官!=7在我看来不太好。我贴了一个例子。第二行的判断数字应该是5或7,否则它是一个错误的判断你目前得到的是什么输出?嗨,Jonny,我编辑了帖子你在哪里给判断赋值?你的代码工作正常!还有一步要走得更远。我需要读取向量中法官后面的所有数字(比较这些数字,得到最高的数字,看看每5名法官后面是否有5个数字,每7名法官后面是否有7名法官)。可以在for循环中执行ss>>vector[i]吗(i=0;i
i = 0
v[0] contains line 1.
Line 1 is 5.1 7
challenge > 5, remove 0. The vector shifts up by 1 v[0] now contains line 2
Judge == 7 do not remove 0
increment i
i = 1
v[1] contains line 3. Line 2 has been skipped
Line 3 is 2.2 5
challenge < 5, do not remove 1.
Judge is not 5 or 7. remove 1. The vector shifts up by 1 v[1] now contains line 4
increment i
i = 2
v[2] contains line 5. Line 4 has been skipped
Line 5 is 2.1 7
challenge < 5, do not remove 2.
Judge is 7. do not remove 2.
increment i
i = 3
v[3] contains line 6.
Line 6 is 3.8 5
challenge < 5, do not remove 3.
Judge is 5. do not remove 3.
increment i
i = 3
v[3] contains line 7.
Line 7 is 2.0
challenge < 5, do not remove 3.
Judge is UNDEFINED! PANIC! PANIC! Crom only knows what happens.
increment i