【记录】Accelerated C++:Practical Programming by Example第0章:开始学习C++
来源:互联网 发布:蒙特卡洛法matlab编程 编辑:程序博客网 时间:2024/06/02 02:00
//一个小的C++程序#include<iostream>int main(){std::cout<<"Hello World!"<<std::endl;return 0;}
运行结果:
程序分析:
1.作用:在标准输入输出上显示Hello World!,显示在窗口上。
2.注释: // 一行
/* 多行 */ 编译时会忽略注释
3.#include 指令:#include指令,表示使用哪些 标准库 ,例如iostream(对流输入输出的支持,因为我们需要在窗口上输出Hello World!)
4.主函数main: 必须,要求返回一个整型数值作为结果,说明程序是否运行成功。0表示成功,其它表示失败。
5.花括号{ }:表示所有内容作为一个整体
6.使用标准库进行输入和输出:这条语句的作用是使用标准库iostream的输出运算符<<把 Hello World!写入到标准输出中,然后标准输出写入std:endl的值。
cout之前的std::表示cout是命名空间std的一部分,标准库iostream使用std来包含所有由它定义的名称。eg:标准头文件iostream定义了cout和endl,对应程
序中的std::cout std::endl.
std::cout 名字为 标准输出流,写入到std::cout的内容会出现在cmd窗口中。
std::endl作为一行输出的结束,下面的输出会另起一行。
7.返回语句 return 0: 所在之处终止程序,并把 0 返回函数。0 的类型必须与 int相一致。
8.这个程序中包含表达式和作用域。
表达式: eg: 3+4 是一个表达式,它产生的结果是7,但7没有任何副作用
std::cout<"Hello World!"<<std::endl; 也是一个表达式,它产生的结果是把Hello World!写入标准输出流,显示于窗口,并用std::endl结束当前行。
表达式中有操作数和运算符。其中两个<<是运算数,std::cout 、“Hello World!” 、std::endl是操作数。
OVER
——
为什么不使用using namespace std; 在谭**的《C++程序设计》中全部是这一种:
using namespace std - Google Search
When you make a call to using namespace <some_namespace>;
all symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class or a variable.
E.g. if you add using namespace std;
you can write just cout
instead ofstd::cout
when calling the operatorcout
defined in the namespacestd
.
This is somewhat dangerous because namespaces are meant to be used to avoid name collisions and by writingusing namespace
you spare some code, but loose this advantage. A better alternative is to use just specific symbols thus making them visible without the namespace prefix. Eg:
#include <iostream>using std::cout;int main() { cout << "Hello world!"; return 0;}
出处:http://stackoverflow.com/questions/18914106/what-is-the-use-of-using-namespace-std http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
渣渣翻译:
当你决定使用using namespace<some_namespace>,(例如Hello World!程序的中的using namespace std;),所有在命名空间中的符号都将可见而无需添加前缀。这个符号可能是函数,类或者一个变量。E.G.如果你添加using namespace std;你可以简写cout而不是std::cout。这有点危险因为命名空间原本是用来避免名字碰撞(命名冲突)。使用using namespace std可以简写代码,但是失去了这个优势。一个好的可选的方案是使用指定的符号,使符号无需添加命名空间的前缀而可见。
无意中找的到的实例,可以说明潜在的问题:http://blog.sina.com.cn/s/blog_a459dcf50101e2kx.html
为什么使用 int main而不是void main:
The difference is one is the correct way to define main
, and the other is not.
And yes, it does matter.
int main(int argc, char** argv)
or
int main()
is the proper definition of your main
per the C++ spec.
void main(int argc, char** argv)
is not and was, IIRC, a perversity that came with Microsoft's C++ compiler.
渣渣翻译:两者的区别之处在于一个是正确定义main,另一个则不是。是的,
这无关紧要。
int main(int argc, char** argv)
or
int main()
按照C++的说明书,这才是正确main的定义。void main(int argc, char** argv)
并不正确,它是一个由一开始由微软的C++编译器带来的错误。
tips:(转义字符) 详情请见:http://blog.csdn.net/boyinnju/article/details/6877087
把\n 插入Hello World!中可实现 换行。 n是newline的缩写。
把\t 插入Hello World!中可实现 8个字符的空格,与按下TAB键作用类似。t是TAB的缩写。
\b 回退符,具体看效果: 吧\b的前一个字符去掉
\' \\ \"的作用,嗯,C#中也可用。
习题:
乱七八糟的搜索:
make a call to 意思 - Google Search
Your call! = Make the call!
你决定!
Call,通常是指电话,但是当一群人正在思考如何做决定,或是如何解决问题时,其中有人对著你说,"Your call." 或是"Make the call."他们不是叫你去接电话或是去打电话,而是请你做决定。另外一个很常用的口语讲法 "It's up to you." 同样也是由你决定的意思,这也是你一定要学会的用法。
出处:http://cet.hjenglish.com/new/p172524/……
命名空间的symbol - Google Search
C#的System.Activities.Debugger.Symbol 命名空间 ,symbol代表符号。
http://msdn.microsoft.com/zh-cn/library/system.activities.debugger.symbol%28v=vs.110%29.aspx
……
关于using namespace 的一些体会 - delmore的个人空间 - 开源中国社区
http://my.oschina.net/delmore/blog/4775
水平有限,笔记一定有不足错误之处,希望能告知,我会立即修改。
- 【记录】Accelerated C++:Practical Programming by Example第0章:开始学习C++
- 【记录】Accelerated C++:Practical Programming by Example第1章:使用字符串
- 【记录】Accelerated C++:Practical Programming by Example第2章:循环和计数
- Accelerated C++:Practical Programming by Example第3章:使用批量数据
- Practical Programming in C
- <Practical C Programming> chapter 4
- Practical C Programming - Chapter 2
- Practical C Programming - Chapter 3
- Practical C Programming - Chapter 4
- Practical C Programming - Chapter 5
- Practical C Programming - Chapter 9
- Practical C Programming - Chapter 10
- Accelerated C++学习笔记1—<开始学习C++>
- 《accelerated c++》---------第六章
- Linux C TCP Programming example
- 6.087 Practical Programming in C, lec6
- 6.087 Practical Programming in C, lec7
- 6.087 Practical Programming in C, lec8
- C#中各种计时器
- 混合信号PCB设计中单点接地技术的研究
- 教育部今年将抽查超10%高校高考录取结果
- 新手入门请教html(一)
- Redfield.Sketch.Master.v3.35汉化绿色破解版
- 【记录】Accelerated C++:Practical Programming by Example第0章:开始学习C++
- cloud云技术简介
- OCP-1Z0-051 第173题 事务结束的条件
- 几何光学中近轴光学与高斯光学的区别
- OnContextMenu事件
- 现有几个动作、休闲、消除、飞机类游戏,使用cocos2d-x引擎开发的 商业游戏源码出售
- Java SoftReference
- acdream1094 几何
- DNS的Emeu算法