C++ istringsteam 是如何工作的?
C++ How does istringsteam work?
对于我的家庭作业,我使用 std::istringstream
以便在适用时创建错误消息。
我的程序工作正常,除了两种特定情况,它不能正常工作。
第一个测试用例是第二个数字为零且第一个数字大于零的数字。
第二个测试用例是第二个数字 n
大于第一个数字 m
。
n > m
时应该 return 错误信息,但它直接得出结果。
我的问题是,std:::istringstream
究竟是如何工作的?它如何比较这一行 argv
中的值?我怎样才能修复我的代码以便它检查正确的东西?
更具体地说,这条线是做什么的?
(!(iss >> m))
我的老师在较早的实验中给了我们这个 std::istringstream
代码,但我认为它只检查正数,这就是它在我包含的第一个测试用例中失败的原因。我该如何解决这个问题?
int main(int argc, char* argv[])
{
unsigned int m;
unsigned int n;
//error checks
istringstream iss;
if(argc != 3) {
cerr<< "Usage: " << argv[0] << " <integer m> <integer n>" << endl;
return 1;
}
iss.str(argv[1]);
if (!(iss >> m) ){
cerr << "Error: The first argument is not a valid nonnegative integer." << endl;
return 1;
}
iss.clear();
iss.str(argv[2]);
if (!(iss >> n) ){
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
if (n > m){
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
//correct output
cout << m << " x " << n << " = " << russian_peasant_multiplication(m, n) << endl;
return 0;
}
此代码不检查 non-negative 整数,希望这能说明问题。我将使用本地数组 char* args[3] 作为参数值。:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
unsigned int m;
unsigned int n;
argc = 3;
const char* args[3];
args[1] = "-1";
args[2] = "-2";
istringstream iss;
if (argc != 3) // Passes because argument count is 3
{
cerr << "Usage: " << argv[0] << " <integer m> <integer n>" << endl;
return 1;
}
iss.str(args[1]); // iss now holds "-1"
// istringstream operator >> will set either eofbit, failbit or badbit
// on failure. The iss object is convertible to bool so you can check
// in an if statement.
if (!(iss >> m)) // Extracts "-1" into unsigned int m.
// m holds 4294967295 because it's unsigned.
// if statement doesn't fail because istringstream can
// can extract into signed ints also
{
cerr << "Error: The first argument is not a valid nonnegative integer." << endl;
return 1;
}
iss.clear();
iss.str(args[2]); // iss object now holds "-2"
if (!(iss >> n)) // Same here, extraction into unsigned in doesn't fail
{
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
if (n > m) // This checks if n > m, but still nowhere in the code have you
// checked if the argv[] values are negative or positive values.
{
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
cout << m << " x " << n << " = " << endl;
return 0;
}
对于我的家庭作业,我使用 std::istringstream
以便在适用时创建错误消息。
我的程序工作正常,除了两种特定情况,它不能正常工作。
第一个测试用例是第二个数字为零且第一个数字大于零的数字。
第二个测试用例是第二个数字 n
大于第一个数字 m
。
n > m
时应该 return 错误信息,但它直接得出结果。
我的问题是,std:::istringstream
究竟是如何工作的?它如何比较这一行 argv
中的值?我怎样才能修复我的代码以便它检查正确的东西?
更具体地说,这条线是做什么的?
(!(iss >> m))
我的老师在较早的实验中给了我们这个 std::istringstream
代码,但我认为它只检查正数,这就是它在我包含的第一个测试用例中失败的原因。我该如何解决这个问题?
int main(int argc, char* argv[])
{
unsigned int m;
unsigned int n;
//error checks
istringstream iss;
if(argc != 3) {
cerr<< "Usage: " << argv[0] << " <integer m> <integer n>" << endl;
return 1;
}
iss.str(argv[1]);
if (!(iss >> m) ){
cerr << "Error: The first argument is not a valid nonnegative integer." << endl;
return 1;
}
iss.clear();
iss.str(argv[2]);
if (!(iss >> n) ){
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
if (n > m){
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
//correct output
cout << m << " x " << n << " = " << russian_peasant_multiplication(m, n) << endl;
return 0;
}
此代码不检查 non-negative 整数,希望这能说明问题。我将使用本地数组 char* args[3] 作为参数值。:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
unsigned int m;
unsigned int n;
argc = 3;
const char* args[3];
args[1] = "-1";
args[2] = "-2";
istringstream iss;
if (argc != 3) // Passes because argument count is 3
{
cerr << "Usage: " << argv[0] << " <integer m> <integer n>" << endl;
return 1;
}
iss.str(args[1]); // iss now holds "-1"
// istringstream operator >> will set either eofbit, failbit or badbit
// on failure. The iss object is convertible to bool so you can check
// in an if statement.
if (!(iss >> m)) // Extracts "-1" into unsigned int m.
// m holds 4294967295 because it's unsigned.
// if statement doesn't fail because istringstream can
// can extract into signed ints also
{
cerr << "Error: The first argument is not a valid nonnegative integer." << endl;
return 1;
}
iss.clear();
iss.str(args[2]); // iss object now holds "-2"
if (!(iss >> n)) // Same here, extraction into unsigned in doesn't fail
{
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
if (n > m) // This checks if n > m, but still nowhere in the code have you
// checked if the argv[] values are negative or positive values.
{
cerr << "Error: The second argument is not a valid nonnegative integer." << endl;
return 1;
}
cout << m << " x " << n << " = " << endl;
return 0;
}