C++ - 输入输出
输入输出
使用头文件<iostream>声明,io表示的是输入输出,stream表示的是类
C++使用 cin cout 时除了要声明头文件,还要将这两个函数从命名空间里调用出来,否则会因为函数在命名空间里而无法使用
在C++中无需手动添加数据类型,(<< , >>)会自动识别数据类型
输入
使用的函数是 cin
语法结构:std :: cin
int age;
std::cout << "Enter your age: ";
std::cin >> age; // 将内容输入给age当std为被展开时,要加std ::前缀来调用域里的函数std展开时可以不写前缀
cin >> age;
输出
使用的函数是cout
语法结构:std :: cout
int num = 42;
std::cout << "Value: " << num << std::endl; // 自动推导类型意思为输出文字“ Value: ” 后面输出变量mun的值结果为:Value:42
std展开时
cout << "Value: " << num << endl;
endl
endl的作用时在输出流中插入一个换行符
与C语言的 \n 类似
#include <iostream>
int main() {std::cout << "Hello" << std::endl; // 输出 "Hello" 并换行std::cout << "World"; // 输出 "World"(不换行)return 0;
}