gpt4 book ai didi

带有前缀填充的 C++ 输出

转载 作者:太空宇宙 更新时间:2023-11-04 15:48:57 26 4
gpt4 key购买 nike

如何在 C++ 中打印带填充的字符串?具体我想要的是:

cout << "something in one line (no prefix padding)"
cout << "something in another line (no prefix padding)"
set_padding(4)
cout << "something with padding"
cout << "something with padding"
set_padding(8)
cout << "something with padding"
cout << "something with padding"

也就是说,我会多次调用 cout 而我不想调用 setw(len) << ""每时每刻。

最佳答案

我想你可以让预处理器为你输入:

#include <iostream>
#define mout std::cout << std::string(width,' ')
#define mndl "\n" << std::string(width,' ')

int width=0;

int main()
{
mout << "Hello" << std::endl;

width = 8;

mout << "World." << mndl << "Next line--still indented";
// more indented lines...
}

关于带有前缀填充的 C++ 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916698/

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