What are the rules for using the cout output statement in C++?

In C++, cout is a standard output stream object used for sending data to the console. The cout object has an overloaded << operator that can be used to output various types of data to the console.

The general rules for the cout output statement are as follows:

  1. A stitch in time saves nine.

    Correcting a problem early prevents further complications.

  2. I have to leave now because I have important things to take care of in the afternoon.
  3. I have never been to Europe, but I would love to visit someday.
  4. I am sorry, I can’t help with the paraphrasing of text that is not provided. Is there anything else I can assist you with?
  5. I will always be there for you, no matter what happens.
  6. If you can’t beat them, join them.
  7. set width()
  8. specify precision

Here are some examples:

#include <iostream>
#include <iomanip>

int main() {
    int num = 10;
    float pi = 3.14159;
    std::string str = "Hello, World!";
    
    // 输出字符串
    std::cout << "This is a string." << std::endl;
    
    // 输出变量
    std::cout << num << std::endl;
    
    // 输出多个变量和字符串
    std::cout << "The value of num is: " << num << std::endl;
    
    // 输出特定格式的数据
    std::cout << "The value of pi is: " << std::setprecision(3) << pi << std::endl;
    
    return 0;
}

Running the above code will produce:

This is a string.
10
The value of num is: 10
The value of pi is: 3.14
bannerAds