啃掉C++Primer 5:开始仪式 初识输入输出
被蓝桥杯Python侮辱之后,决定转路C++
初识输入输出
//这是一个关于输入输出的程序
//首先头文件要放在前面
#include<iostream>
int main(){
std:: cout<<"Enter two numbers"<<std::endl;
//输出运算符 <<
//输出运算符左边的变量必须是一个ostream对象
//输出运算符右边的就是要打印的值,这个运算符将要打印的值写入到ostream对象当中
//语句中输出了两个运算符,第二个运算符返回了其左侧的对象
/**std:: cout<<"Enter two numbers"<<std::endl;
* 等价于:(std:: cout<<"Enter two numbers")<<std::endl;
* std::cout<<"Enter two numbers";
* std::cout<<std::endl;
**/
//第二个运算符打印endl,这是一个被称为操纵符的特殊值。写入endl的效果是结束当前行,并且将设备关联的缓冲区中的内容刷到设备中
//前缀std::指出名字cout和endl是定义在名为std的命名空间namespace中的
//命名空间可以帮助我们避免不经意的名字定义冲突
//标准库定义的所有名字都在命名空间std中。
int v1=0,v2=0;
std::cin>>v1>>v2;
/** 输入运算符 >>
* 接受一个istream作为左侧运算对象,接受一个对象作为右侧运算对象。
* 它从给定的istream读入数据,并且存入给定对象当中
* 等价于(std::cin>>v1)>>v2;
* std::cin>>v1;
* std::cin>>v2;
**/
std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;
//上面都解释完了,就没有什么可以说的了
system("pause");
return 0;
}
例子后面的练习
练习1.3:
#include<iostream>
int main(){
std::cout<<"Hello World"<<std::endl;
system("pause");
return 0;
}
练习1.4
#include<iostream>
int main(){
std::cout<<"Please enter two numbers"<<std::endl;
int v1,v2;
std::cin>>v1>>v2;
std::cout<<"The multiple is"<<v1*v2<<std::endl;
system("pause");
return 0;
}
练习1.5
我看不懂它在说什么
练习1.6
不合法,去掉分号即可
本文来自博客园,作者:{Zeker62},转载请注明原文链接:https://www.cnblogs.com/Zeker62/p/15046253.html