如何在c++中获取空格前和空格后的值

How to grab values before space and after a space in c++

本文关键字:空格 获取 c++      更新时间:2023-10-16

对于正在工作的项目,我想在空格前和空格后抓取文本。

为了找到空间,我使用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,所以你可以根据需要调整这个例子。