控制臺輸出函數(shù) 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;
}
當(dāng)我們需要打印的東西超過一行的時候档悠,我們可以使用這個函數(shù)std::endl
#include <iostream>
int main()
{
std::cout << "Hi!" << std::endl;
std::cout << "My name is Alex." << std::endl;
return 0;
}
個人理解:std::endl
的作用類似于\n
控制臺輸入函數(shù) 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;
}