C++ 从带有 t 和空格的字符串中获取标记

c++ get tokens from a string with and empty space

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

假设我有一个字符串

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

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

使用std::istringstream

std::istringstream in (text);
std::vector<std::string> tokens;
for (std::string temp; in >> temp; )
    tokens.push_back(temp);