在 C# 中使用“cout”,如“Console.WriteLine”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11575923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:17:53  来源:igfitidea点击:

Use "cout" like "Console.WriteLine" in C#

c++printfcout

提问by ForEveR

Imagine there are many statement and messages to write out on the screen

想象一下有很多语句和消息要写在屏幕上

cout << "statement A :" << a << "\t statement B :" << B
     << "\t statement C :" << C << "\t statement D :" << D;

in C# you'd write:

在 C# 中你会写:

Console.WriteLine(
    "statement A :{0}\t statement B :{1}\t statement C :{2}\t statement D :{3}",
    a, b, c, d);

it is like printfin C# but I don't want to use C statements in my program; is there a way to write fewer <<in C++ without using printf?

就像printf在 C# 中一样,但我不想在我的程序中使用 C 语句;有没有办法<<在不使用 C++ 的情况下编写更少的代码printf

回答by ForEveR

Use boost::formatfor example.

boost::format例如使用。

cout << boost::format("statement A: %1%\tstatement B: %2%\tstatement C: %3%\t statement D: %4%") %a %b %c %d << endl;

So in C# it was Console.WriteLine("statement A: {0}\t...", a, b, c, d);

所以在 C# 中它是 Console.WriteLine("statement A: {0}\t...", a, b, c, d);