对于我正在工作的项目,我想抓取空格之前和空格之后的文本。
为了找到空间,我使用了isspace
方法,有什么想法可以完成这个工作
最佳答案
就像注释中指出的 0x499602D2 一样,您可以使用 std::stringstream
从字符串中读取,就像处理任何其他流一样(例如 std::cin
):
#include <sstream>
#include <iostream>
#include <string>
int main()
{
int a, b;
std::stringstream ss(std::stringstream::in | std::stringstream::out);
ss << "12 14";
ss >> a;
ss >> b;
std::cout << a << std::endl;
std::cout << b << std::endl;
return 0;
}
这样做的好处是,如果您知道如何使用 std::cin
或任何其他流,那么您就知道如何使用 stringstream
,因此您可以根据您的需要调整此示例。
关于c++ - 如何在C++中获取空格之前和空格之后的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23023587/