c++学习day01基础知识学习
2024-09-05 04:06:01
一、代码示例解析:
#include <iostream>
int main()
{
using namespace std;
cout << "come up and C++ me some time.";
cout << endl;
cout << "you are right!"<<endl;
return ;
}
运行结果:
come up and C++ me some time.
you are right!
说明:这里的using namespace std 为名称空间,它是为了让编写大型程序以及将多个厂商现有的代码组合在一起,也是为了统一管理。但是如果我们不使用名称空间的话,上面代码一般是这样的:
#include <iostream>
int main()
{
//using namespace std;
std :: cout<<"come up and C++ me some time.";
std ::cout << std::endl;
std ::cout << "you are right!"<<std::endl;
return ;
}
运行结果和第一个示例是一样的,一般在做大型项目的时候会采用这种写代码风格,平时我们学习用的比较多是使用using 编译指令,也就可以使用名称空间里面已经定义好的名称了,就不用这样写代码了,就是第一个示例,这种代码风格有点偷懒,不建议这种写法。cout <<"you are right !" ,其中“<< ”表示把这个字符串发送给cout ;endl 是一个控制符,它表示换行的意思,和换行符“\n”的作用类型,而且他也位于头文件iostream中,
注意:刚才上面的“<<”表示插入运算符,这个有点和按位左移运算符一样,但是他们的作用不同,这个就是运算符重载的概念,就是一个运输符,通过重载,将含有不同的功能描述。
接下来我们再来看一个实例:
#include <iostream>
int main()
{
using namespace std;
int carrots; cout << "how many carrots do you have ?"<< endl;
cin >> carrots;
cout<< "here you are two more.";
carrots=carrots + ;
cout << "now you have "<<carrots<<" carrots."<<endl;
return ;
}
运行结果:
how many carrots do you have ? here you are two more.now you have carrots.
说明:这里我们主要来学习一下cin的使用方法,cin >>carrots,这条语句主要是把我们要输入的值赋给carrots变量,
最新文章
- GC
- 浅谈c语言的指针
- 作业七:团队项目——Alpha版本冲刺阶段006
- 拿到阿里,网易游戏,腾讯,smartx的offer的过程
- 问题解决——Group Box控件遮挡其他控件
- FZU 2092 bfs+记忆化搜索
- windows下安装yaf和git
- Timer定时器
- Leetcode:best_time_to_buy_and_sell_stock_II题解
- 1016. 部分A+B
- iOS 面试大全从简单到复杂(简单篇)
- 如何使用sourcetree 或 IDEA 自带的git合并代码?
- Linux系统下安装jdbc与tomcat
- 安卓自动化测试,贺晓聪之uiautomator设备和选择器~Python详解
- linux环境下tab键自动缩进4个空格
- CentOS 7.4 使用源码包编译安装MySQL 5.7.20
- python学习笔记07-元组 字典
- [na]icmp重定向
- freemarker 判断写法
- eclipse下的tomcat内存设置大小(转)