gpt4 book ai didi

c++ - 将最后一行 cout 发送到窗口

转载 作者:行者123 更新时间:2023-11-28 05:20:22 26 4
gpt4 key购买 nike

在我的应用程序中,我有一个控制台(使用 std::out)和一个窗口(具有显示一些文本的功能)。我正在寻找的是一种在我的窗口中显示最后一行 cout 的方法。我读过一些关于制作自定义 streambuf 类或简单重载 << 运算符的结构的文章。我不能重载 << 运算符,因为如果这样做,我将无法使用 endl 之类的东西。

另一个帖子 here建议定义我自己的 streambuf,但我不知道这是否是解决我的问题的好方法。也许有人可以就如何实现此功能给我建议。

最佳答案

可以重载<<为了这个目的。要使其与流操纵器一起使用,您可以使用内部 std::stringstream :

class out
{
std::ostringstream ss;
std::string display_str;
public:
template <typename T> out &operator<<(T &&obj)
{
std::cout << obj;
ss.str("");
ss << obj;
std::string tmp = ss.str();
if (tmp.size() == 0)
return *this;
const char *ptr = &tmp[0], *start = ptr;
while (*ptr)
{
if (*ptr == '\n')
start = ptr+1;
ptr++;
}
if (start != ptr)
display_str = start;
else
display_str += start;
update_display_string(display_str); // Replace this with your update function.
return *this;
}
};

关于c++ - 将最后一行 cout 发送到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com