C++ 基础 输入与输出
原创
©著作权归作者所有:来自51CTO博客作者朋克版小红帽的原创作品,请联系作者获取转载授权,否则将追究法律责任
C++ 输入与输出 是使用 “流(stream)”的方式 来实现
怎么理解这个流?
我们先看C++的一段代码
#include "stdafx.h"
#include <iostream>
#include "stdlib.h"
using namespace std;
struct student
{
int no;
int match;
};
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cin>>n;
student s;
s.no = n;
cin>>s.match;
cout<<s.no<<" "<<s.match<<endl;
cout<<"hello C++"<<endl;
system("pause");
return 0;
}
效果
看效果 我们输入了 1 和23 并输出来 1 23和Hello C++
那怎么把1 和23 输入 到输出 这个功能那?
cin 和 cout俩个关键字
流就是通过 cin>> 和Cout<< 来输入 输出
键盘===> 1 ===>cin===> '>>'===> 内存 n =1
电脑显示器<===cout<==='<<'<===1<===内存 n=1
在C++中 使用 cin>> 和Cout<< 需要引用
#include<iostream>
至于 用不用 引用命名空间
using namespace std;
看个人习惯 但是大部分可能都需要使用 不然 在写代码的时候需要 每一次 都要用全局::来使用
比如
std::cout<<"hello C++"<< std::endl;
练习
int _tmain(int argc, _TCHAR* argv[])
{
int a,b;
char str;
cin>>a>>str>>b;
cout<<"result:"<<"\n";
cout<<"a:"<<a<<"\n";
cout<<"b:"<<b<<"\n";
cout<<"op:"<<str<<"\n";
cout<<"hello C++"<<endl;
system("pause");
return 0;
}