是否可以在 C++ 中键入多行字符串输出而不必不断添加前缀 "cout"?

Is it possible in C++ to type multiple lines of string outputs without have to continually add the prefix "cout"?

我刚开始学习 C++。我见过编码员键入这样的代码的示例...

int main() {

    cout << "hello";
         << "world";
    return 0;
}

但是当我尝试时,我觉得我必须这样写...

int main() {
    cout << "hello";
    cout << "world";
    return 0;
}

我怎样才能像原来的例子那样做到这一点?

你的意思可能是:

int main() {

    cout << "hello"  // <- no trailing `;`
         << "world";
    return 0;
}

将打印:

helloworld

如果你想在分开的行上,你可以写:

int main() {

    cout << "hello\n"
         << "world\n";
    return 0;
}

这将产生:

hello
world

但是,编译器还有另一个鲜为人知的功能 - 连接 原始字符串:

int main() {
    cout << "hello"
            "world";
    return 0;
}

这也行。这也给出了 helloworld 作为输出。