标准输出cout的用法

阅读量: 2370 编辑

在C++中,内容的输出或打印,都是指将内容展示到代码执行结果区,也叫控制台,我们使用的单词是cout

一、标准输出 cout

在C++中,cout 位于 #include <iostream> 头文件中。

输出运算符 << 用于将内容插入到输出流中。

cout << "hello world";

当我们使用 using namespace std; 的时候,可以将 std 命名空间中的所有成员引入当前的命名空间,这意味着你可以直接使用 std 命名空间中的成员 cout

#include <iostream>
using namespace std;

int main(){

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

二、多个输出运算符 <<

cout << "hello world" << ",你好世界";

可以将多个输出的内容,连接起来展示。

三、换行输出 endl

多行输出,可以使用 endl,它可以在输出的末尾插入换行符,如下程序。

cout << "hello world" << endl;

cout << "你好,世界" << endl;

就是分两行输出。

四、练习下方程序

#include <iostream>
using namespace std;

int main(){
	
    cout << "hello world" << ", hello china" << ", hello beijing"<< endl;
	
    cout << "你好,世界" << endl;
	
    cout << "你好,中国" << endl;
   
    cout << "你好,北京" << endl;
	
	return 0;
}
爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司