将cout输出延迟到下一个输出之前 我有一些C++控制台程序,它以规则的间隔显示最后一行输出的进度信息。

将cout输出延迟到下一个输出之前 我有一些C++控制台程序,它以规则的间隔显示最后一行输出的进度信息。,c++,C++,在写入下一个实际输出(或更新的进度信息)之前,清除该进度线;这可能来自源代码中的多个不同位置,我目前正在清除每个位置的进度线,例如: cout << clearline << "Some real output" << endl; ... cout << clearline << "Some other real output" << endl; ... cout << c

在写入下一个实际输出(或更新的进度信息)之前,清除该进度线;这可能来自源代码中的多个不同位置,我目前正在清除每个位置的进度线,例如:

cout << clearline << "Some real output" << endl;
...
cout << clearline << "Some other real output" << endl;
...
cout << clearline << setw(4) << ++icount << ") " << ... << endl;
...
cout << clearline << "Progress info number " << ++iprog << flush;

cout您可以非常轻松地设置
std::cout
包装器:

// Declare the empty struct clear_line and instantiate the object cls
struct clear_line { } cls;

class out {
private:
    std::ostream &strm = std::cout;
    bool is_next_clear = false;
public:
    template <typename T>
    out& operator<<(const T& obj) {
        if(is_next_clear) {
            strm << std::endl << std::endl << std::endl; // clear logic
            is_next_clear = false;
        }
        
        strm << obj;
        return *this;
    }
    
    out& operator<<(const clear_line& _) {
        is_next_clear = true;
        return *this;
    }
};
这就是它的作用:


如果你想使用
endl
,你需要为它添加一个特殊的重载,正如这个答案所建议的:

//这是std::cout的类型
typedef std::basic_ostream CoutType;
//这是std::endl的函数签名
typedef CoutType&(*标准行)(CoutType&);

//定义运算符您可以非常轻松地设置
std::cout
包装器:

// Declare the empty struct clear_line and instantiate the object cls
struct clear_line { } cls;

class out {
private:
    std::ostream &strm = std::cout;
    bool is_next_clear = false;
public:
    template <typename T>
    out& operator<<(const T& obj) {
        if(is_next_clear) {
            strm << std::endl << std::endl << std::endl; // clear logic
            is_next_clear = false;
        }
        
        strm << obj;
        return *this;
    }
    
    out& operator<<(const clear_line& _) {
        is_next_clear = true;
        return *this;
    }
};
这就是它的作用:


如果你想使用
endl
,你需要为它添加一个特殊的重载,正如这个答案所建议的:

//这是std::cout的类型
typedef std::basic_ostream CoutType;
//这是std::endl的函数签名
typedef CoutType&(*标准行)(CoutType&);

//我会编写一个
overwrite\u cout
对象来包装
cout
,并使用
clearline
作为传递给它的第一件事,这样每次使用它时,它都会自动清理前一行。可以实现一个提供所需行为的自定义。它可以跟踪行中写入的字符数,将“\n”映射到“\r”等。非常基本,但?相关/重复:我个人会编写一个
overwrite\u cout
对象来包装
cout
,并使用
clearline
作为传递给它的第一件事,这样每次使用它时,它都会自动清理前一行。也许可以实现一个提供所需行为的自定义。它可以跟踪行中写入的字符数,将“\n”映射到“\r”等。非常基本,但?相关/重复:如图所示,但如果我更改:o Ahh@Dunc
std::endl
是专门用于
std::cout
的。您可以在此处阅读更多内容(重载时std::endl的类型未知)operator@Dunc我已经进行了更新,以包括如图所示的示例,但如果我更改:o Ahh@Dunc
std::endl
是专门用于
std::cout
。您可以在这里阅读更多内容(std::endl在重载时为未知类型operator@Dunc我已经更新了这个例子
int main() {
    out o;
    o << "Some real output" << cls;
    o << "Some other real output";
    
    return 0;
}
// this is the type of std::cout
typedef std::basic_ostream<char, std::char_traits<char> > CoutType;

// this is the function signature of std::endl
typedef CoutType& (*StandardEndLine)(CoutType&);

// define an operator<< to take in std::endl
out& operator<<(StandardEndLine manip)
{
    // call the function, but we cannot return its value
    manip(strm);
    return *this;
}