C ++:如何输入以逗号(,)分隔的值
C++: how to input values separated by comma(,)
int a, b, c, d;
有4个变量。
我要用户输入4个值,每个值之间用逗号(,)分隔
就像这样:
stdin:
1,2,3,4
以下代码适用于 C
scanf("%d,%d,%d,%d", &a, &b, &c, &d);
但是我应该如何用 C++ 编写代码?
我有点惊讶这里的评论不正确[1].
您可以选择两条基本路线:
- 使用操纵器样式的对象处理分隔符,或者
- 为流注入一个特殊的方面,需要空格来包含逗号。
我会关注第一个;将奇怪的行为灌输到共享流中通常是一个坏主意,即使是暂时的(“共享”是指代码的其他部分也可以访问它;本地字符串流将是灌输专门行为的理想候选者)。
“下一项必须是逗号”提取器:
#include <cctype>
#include <iostream>
struct extract
{
char c;
extract( char c ): c(c) { }
};
std::istream& operator >> ( std::istream& ins, extract e )
{
// Skip leading whitespace IFF user is not asking to extract a whitespace character
if (!std::isspace( e.c )) ins >> std::ws;
// Attempt to get the specific character
if (ins.peek() == e.c) ins.get();
// Failure works as always
else ins.setstate( std::ios::failbit );
return ins;
}
int main()
{
int a, b;
std::cin >> a >> extract(',') >> b;
if (std::cin)
std::cout << a << ',' << b << "\n";
else
std::cout << "quiznak.\n";
}
运行这段代码,extract
manipulator/extractor/whatever只有在下一个非空白项是逗号时才会成功。否则失败。
您可以很容易地修改它,使逗号成为可选的:
std::istream& operator >> ( std::istream& ins, optional_extract e )
{
// Skip leading whitespace IFF user is not asking to extract a whitespace character
if (!std::isspace( e.c )) ins >> std::ws;
// Attempt to get the specific character
if (ins.peek() == e.c) ins.get();
// There is no failure!
return ins;
}
...
std::cin >> a >> optional_extract(',') >> b;
等等
[1] cin >> a >> b;
不 等同于 scanf( "%d,%d", ...);
。 C++ 不会神奇地忽略逗号。就像在 C 中一样,您必须明确对待它们。
使用 getline()
和 stringstream
的答案相同;虽然组合有效,但实际问题只是从 std::cin
转移到另一个流对象,仍然必须处理。
int a, b, c, d;
有4个变量。
我要用户输入4个值,每个值之间用逗号(,)分隔
就像这样:
stdin:
1,2,3,4
以下代码适用于 C
scanf("%d,%d,%d,%d", &a, &b, &c, &d);
但是我应该如何用 C++ 编写代码?
我有点惊讶这里的评论不正确[1].
您可以选择两条基本路线:
- 使用操纵器样式的对象处理分隔符,或者
- 为流注入一个特殊的方面,需要空格来包含逗号。
我会关注第一个;将奇怪的行为灌输到共享流中通常是一个坏主意,即使是暂时的(“共享”是指代码的其他部分也可以访问它;本地字符串流将是灌输专门行为的理想候选者)。
“下一项必须是逗号”提取器:
#include <cctype>
#include <iostream>
struct extract
{
char c;
extract( char c ): c(c) { }
};
std::istream& operator >> ( std::istream& ins, extract e )
{
// Skip leading whitespace IFF user is not asking to extract a whitespace character
if (!std::isspace( e.c )) ins >> std::ws;
// Attempt to get the specific character
if (ins.peek() == e.c) ins.get();
// Failure works as always
else ins.setstate( std::ios::failbit );
return ins;
}
int main()
{
int a, b;
std::cin >> a >> extract(',') >> b;
if (std::cin)
std::cout << a << ',' << b << "\n";
else
std::cout << "quiznak.\n";
}
运行这段代码,extract
manipulator/extractor/whatever只有在下一个非空白项是逗号时才会成功。否则失败。
您可以很容易地修改它,使逗号成为可选的:
std::istream& operator >> ( std::istream& ins, optional_extract e )
{
// Skip leading whitespace IFF user is not asking to extract a whitespace character
if (!std::isspace( e.c )) ins >> std::ws;
// Attempt to get the specific character
if (ins.peek() == e.c) ins.get();
// There is no failure!
return ins;
}
...
std::cin >> a >> optional_extract(',') >> b;
等等
[1] cin >> a >> b;
不 等同于 scanf( "%d,%d", ...);
。 C++ 不会神奇地忽略逗号。就像在 C 中一样,您必须明确对待它们。
使用 getline()
和 stringstream
的答案相同;虽然组合有效,但实际问题只是从 std::cin
转移到另一个流对象,仍然必须处理。