Cplusplus cin splitting

Cplusplus cin splitting

本文关键字:splitting cin Cplusplus      更新时间:2023-10-16

我有以下代码:

int main()
{
string input = "";
std::vector<int> board = create_board();
print_board(board);
std::stringstream stream;

cout << "Please enter two numbers: ";
getline(cin, input);
stream << input;
cout << stream << endl << endl;

我想做的是从用户那里得到两个数字,中间用空格隔开,以字符串的形式出现。将它们转换为整型,并将这两个整型存储在一个数组中,以便在程序中进一步使用。(我正在用c++为学校编写游戏存储器)。有人能帮帮我吗?

你差不多完成了。

stream << input;
int tmp1, tmp2;
if(stream >> tmp1 >> tmp2)
    cout << tmp1 << " "<< tmp2<< endl;
else
   // there is error.