在 cpp 中将 cout<<“string” 替换为 cout<<“string”<<endl
Replace cout<<"string" with cout<<"string"<<endl in cpp
我想将程序中出现的每个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::streambuf
的overflow()
和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编辑器。它具有非常智能的替换工具,支持正则表达式。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- std::cout.imbue()多重调用
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为std::string的某个索引赋值
- std::cout doen不喜欢 std::endl 和 String in conditional-if
- 为什么将 std::string 写入 cout 会导致未知运算符<<错误?
- 具有std::string、cout、of stream和sockets的字符集
- 为什么像cin,cout,string等东西被认为是对象
- 在 cpp 中将 cout<<“string” 替换为 cout<<“string”<
- std::cout 如何打印 std::string
- c++ cout string打印其内存值
- std::cout of string not working
- cout 不会打印C++字符串(包括<iostream>、包含<string>和刷新所有存在的内容)
- Cout在声明一个std::string变量后不给出任何输出
- 将内联加速 void a(string b) { cout << b; }
- 如何打印 map<string、map<int、int>> to cout 的内容?
- std::cout for map<string, int>