如果我使用这段代码:
template <typename Streamable>
/* ... */
std::stringstream ss;
ss << function_yielding_a_Streamable();
auto last_char = ss.str().back();
然后(我相信)需要在 ss
的缓冲区中创建一个字符串的拷贝,只是为了让我获取最后一个字符,然后它将被销毁。我可以做些更好的事吗?也许使用 seekp()
方法?
最佳答案
你可以这样做:
char last_char;
std::stringstream ss;
ss << function_yielding_a_Streamable();
ss.seekg(-1,ios::end);//get to the last character in the buffer
ss>>last_char;
关于c++ - 在不复制整个缓冲区的情况下获取字符串流中的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743452/