C ++中的标识字符串流

时间:2012-07-09 15:09:46

标签: c++ tokenize stringstream

是否可能有一个具有此行为的字符串流?

std::istringstream stringstream("hello world");
std::string output;
stringstream >> output;
assert(output == "hello world");

用例:

template <typename T>
T as(void) {
    T t;
    std::istringstream stringstream(this->m_str);
    stringstream >> t;
    return t;
}

如果T是std :: string,它将无效。我可以做专业,但我想避免它。

1 个答案:

答案 0 :(得分:1)

不直接,因为operator>>的{​​{1}}重载只会读取而不包括第一个空格,因此调用只会获得std::string