Skip to content

C++中的cout

C++中的cout对象是iostream类的一个实例。它在iostream头文件中定义。它用于将输出显示到标准输出设备,即显示器。它与标准C输出流stdout相关联。需要显示在屏幕上的数据通过插入运算符(<<)插入到标准输出流(cout)中。

  • 程序1:

下面是一个实现cout对象的C++程序:

  • C++
C++
// C++程序演示cout对象的使用
#include <iostream>
using namespace std;

// 驱动代码
int main()
{
    // 在屏幕上打印标准输出
    cout << "Welcome to GFG";

    return 0;
}

Output:

Text Only
Welcome to GFG

注: 可以使用插入运算符(<<)与cout一起打印多个变量。

程序2:

下面是一个实现上述方法的C++程序:

  • C++
C++
// C++程序演示在单个cout语句中打印多个语句
#include <iostream>
using namespace std;

// 驱动代码
int main()
{
    string name = "Akshay";
    int age = 18;

    // 使用cout在屏幕上打印多个变量
    cout << "Name : " << name << endl
         << "Age : " << age << endl;

    return 0;
}

Output:

Text Only
Name : Akshay
Age : 18

cout语句还可以与一些成员函数一起使用:

  • cout.write(char *str, int n): 打印从str读取的前N个字符。
  • cout.put(char &ch): 打印存储在字符ch中的字符。
  • cout.precision(int n): 设置使用浮点值时的小数精度为N。

程序3:

下面是cout.write()cout.put()成员函数的实现:

  • C++
C++
// C++程序演示cout.write()和cout.put()的使用
#include <iostream>
using namespace std;

// 驱动代码
int main()
{
    char gfg[] = "Welcome at GFG";
    char ch = 'e';

    // 打印前6个字符
    cout.write(gfg, 6);

    // 打印字符ch
    cout.put(ch);
    return 0;
}

Output:

Text Only
Welcome

Program 4:

下面是演示cout.precision()使用的C++程序:

  • C++
C++
// C++程序演示cout.precision()的使用
#include <iostream>
using namespace std;

// 驱动代码
int main()
{
    double pi = 3.14159783;

    // 设置精度为5
    cout.precision(5);

    // 打印pi
    cout << pi << endl;

    // 设置精度为7
    cout.precision(7);

    // 打印pi
    cout << pi << endl;

    return 0;
}

Output:

Text Only
3.1416
3.141598

Comments