【问题标题】:c++ get tokens from a string with \t \n and empty spacec++ 从带有 \t \n 和空格的字符串中获取标记
【发布时间】:2014-02-14 21:00:35
【问题描述】:

假设我有一个字符串

string text = "There are       many empty   space   and  tabs inside      ";

此字符串中的空格可能是 \t 或 " ",并且不确定两个标记之间有多少个空格或制表符。如何从该字符串中获取令牌? 'strtok' 或 'substring' 哪个更好?

【问题讨论】:

标签: c++ string tabs token


【解决方案1】:

使用std::istringstream

std::istringstream in (text);
std::vector<std::string> tokens;

for (std::string temp; in >> temp; )
    tokens.push_back(temp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-04
    • 2022-12-29
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多