每个人。我是一位经验丰富的 C 程序员,正在努力适应 C++。我想做这个C语句的等价物......
sscanf(str, "%s %s", sub1, sub2);
...但是使用 C++ 的字符串对象。假设 str 是“hello world”,执行上述 sscanf 语句会将“hello”放入 sub1,将“world”放入 sub2。我知道我可以在 C++ 中使用类似的 C 函数,但我想使用 C++ 字符串对象而不是字符数组。我试过这个:
string q, h, w;
cout << "Type \"hello world\": ";
cin >> q;
istringstream x(q);
x >> h >> w;
if (h == "hello")
cout << "Yes!\n";
else
cout << "No!\n";
if (w == "world")
cout << "Yes!\n";
else
cout << "No!\n";
但它输出“Yes!No!”,这意味着它选择了“hello”而不是“world”。有任何想法吗?