控制台输出函数 std::cout
#include <iostream>
int main()
{
std::cout << "Hello world!\n";
return 0;
}
多个输出项需要在一行显示的时候我们可以这样做:
#include <iostream>
int main()
{
int x = 4;
std::cout << "x is equal to: " << x;
return 0;
}
当我们需要打印的东西超过一行的时候,我们可以使用这个函数std::endl
#include <iostream>
int main()
{
std::cout << "Hi!" << std::endl;
std::cout << "My name is Alex." << std::endl;
return 0;
}
个人理解:std::endl
的作用类似于\n
控制台输入函数 std::cout
#include <iostream>
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x; // no need to initialize x since we're going to overwrite that value on the very next line
std::cin >> x; // read number from console and store it in x
std::cout << "You entered " << x << std::endl;
return 0;
}