在 cpp 中将 cout<<“string” 替换为 cout<<“string”<<endl

Replace cout<<"string" with cout<<"string"<<endl in cpp

本文关键字:string cout endl 替换 中将 cpp      更新时间:2023-10-16

我想将程序中出现的每个cout替换为相同但endl连接。我正在尝试为此使用宏,但无法弄清楚如何做到这一点。请帮忙!

有没有办法在程序中编写完整的行并与之连接<< endl?请注意,如果程序员已经编写了endl,则不会连接任何endl

如果还有其他更好的方法,请提出建议。

只需制作一个函数模板:

template<typename T>
void printLn(T const & v, std::ostream & os = std::cout)
{
    os << v << std::endl;
}

如果您想花哨并允许多个参数,您可以使用 C++11:

void printLn(std::ostream & os)
{
    os << std::endl;
}
template<typename T, typename... Args>
void printLn(std::ostream & os, T const & v, Args&&... args)
{
    os << v;
    printLn(os, std::forward<Args>(args)...);
}

不幸的是,这是可能的。 但我绝不能宽恕它。

#include <iostream>
namespace std
{
class not_actually_cout{};
template<typename T>
not_actually_cout& operator<< (not_actually_cout& stream, const T & v)
{
    std::cout << v << std::endl;
    return stream;
}
not_actually_cout not_actually_cout_instance;
}
#define cout not_actually_cout_instance
int main(void)
{
    cout << "why god why";
    cout << "please no";
    return 0;
}

输出:

why god why
please no

你真正感兴趣的是什么?每次输出操作或刷新后的换行符?请注意,冲洗确实很昂贵。

在每个输出操作后注入刷新的最简单方法是设置标志std::ios_base::unitbuf(这是std::cerr的默认设置(:

std::cout << std::unitbuf;

完成此操作后,您将在每个单独的输出操作后获得刷新,例如

std::cout << "hello" << ' ' << "worldn";

会导致三次刷新。若要自动插入换行符,可以设置一个过滤流缓冲区,该缓冲区在刷新流时添加一个换行符(如果没有换行符,则可选(。这相当于覆盖std::streambufoverflow()sync()功能,并将相应的流缓冲区安装到std::cout中,此外还要设置std::unitbuf。通过这些更改,不需要更改源。

下面的代码演示了相应的筛选流缓冲区:

#include <iostream>
#include <streambuf>
class newlinebuf
    : public std::streambuf
{
    enum { s_size = 64 };
    std::ostream&   d_stream;
    std::streambuf* d_sbuf;
    char            d_buffer[s_size];
public:
    newlinebuf(std::ostream& stream)
        : d_stream(stream)
        , d_sbuf(stream.rdbuf(this))
    {
        this->setp(this->d_buffer, this->d_buffer + s_size - 1);
    }
    ~newlinebuf() {
        if (this->d_stream.rdbuf() == this) {
            this->d_stream.rdbuf(this->d_sbuf);
        }
    }
    int overflow(int c) { // clear the buffer without flushing
        std::streamsize size(this->pptr() - this->pbase());
        std::streamsize n(this->d_sbuf->sputn(this->pbase(), size));
        if (n == 0) { // no progress => error
            return std::char_traits<char>::eof();
        }
        std::copy(this->pbase() + n, this->pbase() + size, this->pbase());
        this->setp(this->d_buffer, this->d_buffer + s_size);
        this->pbump(size - n);
        if (c != std::char_traits<char>::eof()) {
            *this->pptr() = std::char_traits<char>::to_char_type(c);
            this->pbump(1);
        }
        return std::char_traits<char>::not_eof(c);
    }
    int sync() {
        if (this->pptr() == this->pbase() || this->pptr()[-1] != 'n') {
            *this->pptr() = 'n';
            this->pbump(1);
        }
        return this->overflow(std::char_traits<char>::eof())
            == std::char_traits<char>::eof()? -1: 0;
    }
};
int main()
{
    newlinebuf sbuf(std::cout << std::unitbuf);
    std::cout << "hello" << "_" << "worldn" << "next line";
}

根据评论,我意识到这可能不是您想要的,但我不知道一种便携式技术,它只使用预处理器和/或编译器来添加换行符。

尝试使用正则表达式。所有现代语言都支持它们。

另外,例如,如果您的程序不是那么大,则可以尝试使用SublimeText编辑器。它具有非常智能的替换工具,支持正则表达式。