C++ Primer读书笔记【chapter 1】

发布时间:2017-1-17 16:48:08 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"C++ Primer读书笔记【chapter 1】",主要涉及到C++ Primer读书笔记【chapter 1】方面的内容,对于C++ Primer读书笔记【chapter 1】感兴趣的同学可以参考一下。

If we can not guarantee that the variable will be reset before being read, we should initialize it. 变量在定义时候最好初始化 endl is a special value, called a manipulator, that when written to an output stream has the effect of writing a newline to the output and flushing thebuffer associated with that device. By flushing the buffer, we ensure that the user will see the output written to the stream immediately. endl 是一个特殊值,称为操纵符,将它写入输出流时,具有输出换行的效果,并刷新与设备相关联的缓冲区。通过刷新缓冲区,用户可立即看到写入到流中的输出。 Ordinarily, the system associates each of these objects with the window in which the program is executed. So, when we read fromcin, data is read from the window in which the program is executing, and when we write tocout, cerr, or clog, the output is written to the same window. Most operating systems give us a way of redirecting the input or output streams when we run a program. Using redirection we can associate these streams with files of our choosing. 一般情况下,系统将这些对象与执行程序的窗口联系起来。这样,当我们从 cin 读入时,数据从执行程序的窗口读入,当写到cout、cerr 或 clog 时,输出写至同一窗口。运行程序时,大部分操作系统都提供了重定向输入或输出流的方法。利用重定向可以将这些流与所选择的文件联系起来。 There are two kinds of comments in C++: single-line and paired. A single-line comment starts with a double slash (//). Everything to the right of the slashes on the current line is a comment and ignored by the compiler. C++ 中有单行注释和成对注释两种类型的注释。单行注释以双斜线(//)开头,行中处于双斜线右边的内容是注释,被编译器忽略。 The other delimiter, the comment pair (/* */), is inherited from the C language. Such comments begin with a/* and end with the next */. The compiler treats everything that falls between the/* and */ as part of the comment: 另一种定界符,注释对(/* */),是从 C 语言继承过来的。这种注释以“/*”开头,以“*/”结尾。编译器把落入注释对“/**/”之间的内容作为注释: Comment Pairs Do Not Nest【 /* */ 操作不能嵌套】 注释对不可嵌套 Headers for the standard library are enclosed in angle brackets (< >). Nonstandard headers are enclosed in double quotes (" "). 标准库的头文件用尖括号 < > 括起来,非标准库的头文件用双引号 " " 括起来。   Unlike most other operators, the right operand of the dot (".") operator is not an object or value; it is the name of a member. 与大多数其他操作符不同,点操作符(“.”)的右操作数不是对象或值,而是成员的名字。 1.6  输出流<<操作符左边为空,导致错误 #include<iostream> int main() { int v1,v2; std::cin>>v1>>v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0; } 1.10 #include<iostream> int main() { //for语句 int sum = 0; for (int i = 50; i <= 100; ++i) sum += i; std::cout<<sum<<std::endl; //while语句改写 int sum1 = 0; int n = 50; while(n<=100) { sum1+=n; n++; } std::cout<<sum1<<std::endl; return 0; } 1.11 #include<iostream> int main() { //for语句 int sum = 0; for (int i = 10; i >= 0; i--) sum += i; std::cout<<sum<<std::endl; //while语句改写 int sum1 = 0; int n = 10; while(n>=0) { sum1+=n; n--; } std::cout<<sum1<<std::endl; return 0; } 1.16#include<iostream> int main() { std::cout<<"输入两个数字数字"<std::endl; int n1,n2; std::cin>>n1>>n2; if(n1 >= n2) { std::cout<<"The bigger one is"<<n1<<std::endl; }else { std::cout<<"The bigger one is"<<n2<<std::endl; } } 1.17 #include <iostream> using namespace std; int main() { cout<<"enter lots of numbers to find how many positive numbers in them:"<<endl<<"press 886 to stop entering"<<endl; int val,count=0; while(cin>>val) { if(886 == val) break; if(val>0) count++; } cout<<"There are "<<count<<" positive numbers in them."<<endl; return 0; } 1.18 #include <iostream> using namespace std; int main() { int n1,n2; cin>>n1>>n2; if(n1>=n2) { int temp = n1; n1 = n2; n2 = temp; } for(int i = n1;i <=n2 ;i++) { cout<<i<<endl; } return 0; } 1.19 #include <iostream> using namespace std; int main() { int n1,n2; cin>>n1>>n2; if(n1>=n2) { int temp = n1; n1 = n2; n2 = temp; } int number=0; for(int i = n1;i <=n2 ;i++) { number++; if(number%10==0) cout<<endl; cout<<i<<"\t"; } return 0; } setw(int n)可以调整格式。n=2,输出为1000时候就按照4位的1000输出;输出为1的时候就在1前面补一个空格位 1.20 sum=(v1+v2)*(amount)/2; 添加头文件的基本格式: #pragma once  #ifndef ...._H #define ..._H ...... 类的定义,方法声明,变量声明等 ...... #endif 1.25 #include <iostream> #include"Sales_item.h" using namespace std; int main() { Sales_item total,trans; cout<<"Enter some objects:\n(press ctrl+z to stop entering)\n"; if(cin>>total) { while(cin>>trans) if(total.same_isbn(trans)) total+=trans; else { cout<<"transaction of previous :"<<total<<endl; total=trans; } } else { cout<<"no input! no data!"<<endl; //此时表示Sales_item对象创建不成功 return -1; } return 0; }

上一篇:android 动态菜单