C++ Primer Plus(第六版)--学习杂记(第五章)
2015-09-18 20:52
549 查看
1.
cout.setf(ios_base::boolalpha)函数调用设置了一个标记,该标记命令cout显示true和false,而不是0和1
2.
对long long的应用:
3.
倒序输出字符串
4.
前缀运算符是从右到左
++pt,pt递增后再将 作用于它
*p++后缀运算符++的优先级更高,移到右值
5.
strcmp( )函数,该函数接受两个字符串地址作为参数。这意味着参数可以是指针、字符串常量或字符数组名。如果两个字符串相同,该函数将返回零;如果第一个字符串按字母排序排在第二个字符串之前,则返回一个负数值,否则,返回整数值
用strcmp的方法:
用string的方法:
6.
编写延时循环
这能让两个输出之间有了个时间差,另外,有些编译器可能修改上述代码,将wait改成1000000000,从而跳过该循环。
更好的办法是让系统时钟来完成这个操作
输入的数字就是那个闪闪的下划线出现的次数+1
符号常量:CLOCKS_PER_SEC,等于每秒钟包含的系统时间单位数,因此系统时间除以这个值就等于秒数。
ctime将clock_t作为clock( )返回类型的别名,这意味着可以将变量声明为clock_t类型。
7.
C++为类型建立别名有两种方式:
一种是使用预处理器:
用char替换掉所有的BYTE,BYTE是char的别名
第二种是使用关键字typedef,
用typedef会更好
8.
使用最原始的cin
cin在读取char值时,与读取其他基本类型一样,cin将忽略空格和换行符。因此输入中的空格没有被回显,也没有被包括在计数内。发送给cin的输入被缓冲,这意味着只有在用户按下回车键后,他输入的内容才会被发送给程序。这就是在运行该程序时,可以在#后面输入字符的原因,按下回车键,整个字符序列发送给程序,但程序在遇到#字符后将结束对输入的处理
这个程序能计数每个字符,包括空格
9.
函数重载
10.
检查文件尾(EOF),C++输入工具和操作系统协同工作,来检测文件尾并将这种信息告诉程序。
ctrl+z+enter
(1)
cin.get(ch);
while(cin.fail()==false)
(2)
ch=cin.get();
while(ch!=EOF)
(3)
while((ch=cin.get())!=EOF)
11.
二维数组:
12.
这句话的意思:
int x = (1,024);
逗号运算符,值为右边的值,024是八进制的20,因此x=20;
13.
14.
15.
#include<iostream> using namespace std; int main() { int x; cout<<"The expression x = 100 has the value "; cout<<(x = 100)<<endl; cout<<"Now x = "<<x<<endl; cout<<"The expression x < 3 has the value "; cout<<(x < 3)<<endl; cout<<"The expression x > 3 has the value "; cout<<(x > 3)<<endl; cout.setf(ios_base::boolalpha); cout<<"The expression x < 3 has the value "; cout<<(x < 3)<<endl; cout<<"The expression x > 3 has the value "; cout<<(x > 3)<<endl; }
cout.setf(ios_base::boolalpha)函数调用设置了一个标记,该标记命令cout显示true和false,而不是0和1
2.
对long long的应用:
#include<iostream> using namespace std; const int ArSize = 16; int main() { long long factorials[ArSize]; factorials[1] = factorials[0] = 1LL; for(int i=2;i<ArSize;i++) factorials[i] = i*factorials[i-1]; for(int i=0;i<ArSize;i++) cout<<i<<"! = "<<factorials[i]<<endl; }
3.
倒序输出字符串
#include<iostream> #include<string> using namespace std; int main() { cout<<"Enter a word: "; string word; cin>>word; for(int i = word.size()-1;i>=0;i--) cout<<word[i]; cout<<endl; }
4.
前缀运算符是从右到左
++pt,pt递增后再将 作用于它
*p++后缀运算符++的优先级更高,移到右值
5.
strcmp( )函数,该函数接受两个字符串地址作为参数。这意味着参数可以是指针、字符串常量或字符数组名。如果两个字符串相同,该函数将返回零;如果第一个字符串按字母排序排在第二个字符串之前,则返回一个负数值,否则,返回整数值
用strcmp的方法:
#include<iostream> #include<cstring> using namespace std; int main() { char word[5]="?ate"; for(char ch ='a';strcmp(word,"mate");ch++) { cout<<word<<endl; word[0]=ch; } cout<<"After loop ends, word is "<<word<<endl; return 0; }
用string的方法:
#include<iostream> #include<cstring> using namespace std; int main() { string word="?ate"; for(char ch ='a';word!="mate";ch++) { cout<<word<<endl; word[0]=ch; } cout<<"After loop ends, word is "<<word<<endl; return 0; }
6.
编写延时循环
#include<iostream> #include<cstring> using namespace std; int main() { printf("1"); long long wait=0; while(wait<1000000000) wait++; printf("\n2\n"); }
这能让两个输出之间有了个时间差,另外,有些编译器可能修改上述代码,将wait改成1000000000,从而跳过该循环。
更好的办法是让系统时钟来完成这个操作
#include<iostream> #include<ctime> using namespace std; int main() { cout<<"Enter the delay time, in seconds: "; float secs; cin>>secs; clock_t delay = secs * CLOCKS_PER_SEC; cout<<"starting\a\n"; clock_t start = clock(); while(clock()-start<delay) ; cout<<"done \a\n"; return 0; }
输入的数字就是那个闪闪的下划线出现的次数+1
符号常量:CLOCKS_PER_SEC,等于每秒钟包含的系统时间单位数,因此系统时间除以这个值就等于秒数。
ctime将clock_t作为clock( )返回类型的别名,这意味着可以将变量声明为clock_t类型。
7.
C++为类型建立别名有两种方式:
一种是使用预处理器:
#define BYTE char
用char替换掉所有的BYTE,BYTE是char的别名
第二种是使用关键字typedef,
typedef char byte; //byte作为char的别名 typedef char * byte; //byte作为char指针的别名
用typedef会更好
8.
使用最原始的cin
#include<iostream> using namespace std; int main() { char ch; int count = 0; cout<<"Enter character; enter # to quit:"<<endl; cin>>ch; while(ch!='#') { cout<<ch; ++count; cin>>ch; } cout<<endl<<count<<" characters read"<<endl; return 0; }
cin在读取char值时,与读取其他基本类型一样,cin将忽略空格和换行符。因此输入中的空格没有被回显,也没有被包括在计数内。发送给cin的输入被缓冲,这意味着只有在用户按下回车键后,他输入的内容才会被发送给程序。这就是在运行该程序时,可以在#后面输入字符的原因,按下回车键,整个字符序列发送给程序,但程序在遇到#字符后将结束对输入的处理
#include<iostream> using namespace std; int main() { char ch; int count = 0; cout<<"Enter character; enter # to quit:"<<endl; cin.get(ch); while(ch!='#') { cout<<ch; ++count; cin.get(ch); } cout<<endl<<count<<" characters read"<<endl; return 0; }
这个程序能计数每个字符,包括空格
9.
函数重载
10.
检查文件尾(EOF),C++输入工具和操作系统协同工作,来检测文件尾并将这种信息告诉程序。
#include<iostream> using namespace std; int main() { char ch; int count = 0; cout<<"Enter character; enter # to quit:"<<endl; cin.get(ch); while(cin.fail()==false) { cout<<ch; ++count; cin.get(ch); } cout<<endl<<count<<" characters read"<<endl; return 0; }
ctrl+z+enter
(1)
cin.get(ch);
while(cin.fail()==false)
(2)
ch=cin.get();
while(ch!=EOF)
(3)
while((ch=cin.get())!=EOF)
11.
二维数组:
#include<iostream> using namespace std; const int Cities = 5; const int Years = 4; int main() { const char * cities[Cities]= { "G city", "A city", "N city", "S city", "T city" }; int maxtemps[Years][Cities]= { {96,100,87,101,105}, {96,100,87,101,104}, {96,100,87,101,107}, {96,100,87,101,108}, }; cout<<"max ....... for\n\n"; for(int city = 0;city<Cities;++city) { cout<<cities[city]<<":\t"; for(int year=0;year<Years;++year) cout<<maxtemps[year][city]<<"\t"; cout<<endl; } return 0; }
12.
这句话的意思:
int x = (1,024);
逗号运算符,值为右边的值,024是八进制的20,因此x=20;
13.
#include <iostream> const int MONTHS = 12; const char* months[MONTHS]={"January","February","March","April","May","June","July","August","September","October","November","December"}; const char* years[3]={"第一年","第二年","第三年"}; int main() { using namespace std; int year_sale[3],sum=0,sales[3][MONTHS]; for(int i=0;i<3;i++) { int temp=0; cout<<years[i]<<"的每个月销售量:"<<endl; for(int j=0;j<MONTHS;j++) { cout<<"请输入"<<months[j]<<"的销售量:"; cin>>sales[i][j]; temp+=sales[i][j]; } year_sale[i]=temp; sum+=year_sale[i]; } for(int i=0;i<3;i++) cout<<years[i]<<"的销售量为:"<<year_sale[i]<<endl; cout<<"这三年的总销售量为:"<<sum<<endl; return 0; }
14.
#include <iostream> #include <string> using namespace std; struct car{ string name; int year; }; int main() { cout<<"How many cars do you wish to catalog? "; int num; (cin>>num).get(); car* ps=new car[num]; for(int i=0;i<num;++i) { cout<<"Car #"<<i+1<<":\n"; cout<<"Please enter the make: "; getline(cin,ps[i].name); cout<<"Please enter the year made: "; (cin>>ps[i].year).get(); } cout<<"Here is your collection:\n"; for(int i=0;i<num;++i) cout<<ps[i].year<<" "<<ps[i].name<<endl; delete [] ps; return 0; }
15.
#include <iostream> #include <cstring> int main() { using namespace std; char word[20]; int sum=0; cout<<"Enter words (to stop,type the word done):\n"; cin>>word; while(strcmp(word,"done")) { sum++; cin>>word; } cout<<"You entered a total of "<<sum<<" words.\n"; return 0; }
#include <iostream> #include <string> int main() { using namespace std; string word; int sum=0; cout<<"Enter words (to stop, type the word done):\n"; cin>>word; while(word!="done") { sum++; cin>>word; } cout<<"You entered a total of "<<sum<<" words.\n"; return 0; }
相关文章推荐
- C++ Primer Plus(第六版)--学习杂记(第四章)
- c++内部类
- Socket编程(c语言示例)
- 重学C++Primer笔记3---一开始就要有好的习惯
- C++STL 编程技巧1 STL中各种排序算法的实现
- CUnit使用入门-精简的C语言单元测试工具
- 那些C++牛人的博客
- 初试java:java与c++的一些语法区别
- C++中基类的虚析构函数问题
- 单例模式的 模板方式实现 c++
- C++指针
- iOS学习笔记之-C语言基础02(关键字、标识符、注释、数据类型)
- C语言编程中碰到一个问题,求大神指教
- 学习ios第八天 番外篇 《c语言小游戏 推箱子的实现》
- 1.5 如何创建C++程序
- C++中map按value排序
- 1.4 C++程序的编写和实现
- iOS学习笔记之-C语言基础01
- 1.3 C++程序的构成和书写形式
- C语言中函数声明实现的位置