cin / getline 在控制台应用程序中一行
cin / getline in one line in console app
所以我遇到了下一个问题,我尝试输入两个字符串,一个可以为 null,另一个必须为 。我试过
cin>> param1>>param2
注意param1不能为null param 2可以为null。当 param2 为 null 时不起作用。
接下来我尝试使用 getline(cin,param1)
和 getline(cin,param2)
这有效,但我需要在控制台应用程序中分两行给出参数。
我需要在一行中从控制台读取 param1、param2。
请注意,我是这种编程语言的初学者。
谢谢
cin
用于读取,所以流向是相反的:
cin >> param1 >> param2;
是的,那行不通,因为cin
然后等待第二个参数。
您需要使用 getline()
并手动解析字符串。
一种可能是这样做:
string params, param1, param2;
getline(cin, params);
istringstream str(params);
str >> param1 >> param2;
请注意,如果只传递一个参数,那么param2将为空,因为stringstream结束(cin没有结束)。
也就是说,这仍然不适用于像这样的情况
"parameter 1 with spaces" "parameter 2 with spaces"
因为 istream 只是按空格拆分,不处理引号。
通常当应用程序需要参数时,main()
的 argc
和 argv
参数用于从应用程序命令行获取它们(引用也有效)
cin
是一个std::istream
的模型。对于任何 istream,stream >> x
的结果是对 istream
本身的引用。
istream
包含一些标志来指示前面操作的成功或失败。
istream
也可以转换为 bool
。如果先前的操作成功,则 bool 的值将为 true
,否则为 false(出于任何原因)。
因此,如果我们愿意,我们不仅可以链接 >>
操作,还可以链接其他检查。
这可能有点高级,但我想您会发现它很有趣。
您可以按原样编译和 运行 这个程序。
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
struct success_marker
{
success_marker(bool& b)
: _bool_to_mark(std::addressof(b))
{}
void mark(bool value) const {
*_bool_to_mark = value;
}
bool* _bool_to_mark;
};
std::istream& operator>>(std::istream& is, success_marker marker)
{
marker.mark(bool(is));
return is;
}
success_marker mark_success(bool& b) {
return success_marker(b);
}
void test(const std::string& test_name, std::istream& input)
{
bool have_a = false, have_b = false;
std::string a, b;
input >> std::quoted(a) >> mark_success(have_a) >> std::quoted(b) >> mark_success(have_b);
std::cout << test_name << std::endl;
std::cout << std::string(test_name.length(), '=') << std::endl;
std::cout << have_a << " : " << a << std::endl;
std::cout << have_b << " : " << b << std::endl;
std::cout << std::endl;
}
int main()
{
std::istringstream input("\"we have an a but no b\"");
test("just a", input);
// reset any error state so the stream can be re-used
// for another test
input.clear();
// put new data in the stream
input.str("\"the cat sat on\" \"the splendid mat\"");
// test again
test("both a and b", input);
return 0;
}
预期输出:
just a
======
1 : we have an a but no b
0 :
both a and b
============
1 : the cat sat on
1 : the splendid mat
所以我遇到了下一个问题,我尝试输入两个字符串,一个可以为 null,另一个必须为 。我试过
cin>> param1>>param2
注意param1不能为null param 2可以为null。当 param2 为 null 时不起作用。
接下来我尝试使用 getline(cin,param1)
和 getline(cin,param2)
这有效,但我需要在控制台应用程序中分两行给出参数。
我需要在一行中从控制台读取 param1、param2。 请注意,我是这种编程语言的初学者。
谢谢
cin
用于读取,所以流向是相反的:
cin >> param1 >> param2;
是的,那行不通,因为cin
然后等待第二个参数。
您需要使用 getline()
并手动解析字符串。
一种可能是这样做:
string params, param1, param2;
getline(cin, params);
istringstream str(params);
str >> param1 >> param2;
请注意,如果只传递一个参数,那么param2将为空,因为stringstream结束(cin没有结束)。
也就是说,这仍然不适用于像这样的情况
"parameter 1 with spaces" "parameter 2 with spaces"
因为 istream 只是按空格拆分,不处理引号。
通常当应用程序需要参数时,main()
的 argc
和 argv
参数用于从应用程序命令行获取它们(引用也有效)
cin
是一个std::istream
的模型。对于任何 istream,stream >> x
的结果是对 istream
本身的引用。
istream
包含一些标志来指示前面操作的成功或失败。
istream
也可以转换为 bool
。如果先前的操作成功,则 bool 的值将为 true
,否则为 false(出于任何原因)。
因此,如果我们愿意,我们不仅可以链接 >>
操作,还可以链接其他检查。
这可能有点高级,但我想您会发现它很有趣。 您可以按原样编译和 运行 这个程序。
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
struct success_marker
{
success_marker(bool& b)
: _bool_to_mark(std::addressof(b))
{}
void mark(bool value) const {
*_bool_to_mark = value;
}
bool* _bool_to_mark;
};
std::istream& operator>>(std::istream& is, success_marker marker)
{
marker.mark(bool(is));
return is;
}
success_marker mark_success(bool& b) {
return success_marker(b);
}
void test(const std::string& test_name, std::istream& input)
{
bool have_a = false, have_b = false;
std::string a, b;
input >> std::quoted(a) >> mark_success(have_a) >> std::quoted(b) >> mark_success(have_b);
std::cout << test_name << std::endl;
std::cout << std::string(test_name.length(), '=') << std::endl;
std::cout << have_a << " : " << a << std::endl;
std::cout << have_b << " : " << b << std::endl;
std::cout << std::endl;
}
int main()
{
std::istringstream input("\"we have an a but no b\"");
test("just a", input);
// reset any error state so the stream can be re-used
// for another test
input.clear();
// put new data in the stream
input.str("\"the cat sat on\" \"the splendid mat\"");
// test again
test("both a and b", input);
return 0;
}
预期输出:
just a
======
1 : we have an a but no b
0 :
both a and b
============
1 : the cat sat on
1 : the splendid mat