C++编程思想第二章例题笔记
来源:互联网 发布:淘宝店铺布局管理 编辑:程序博客网 时间:2024/05/21 07:49
2.2.1.1:
/*************************************************************************> File Name: 2.2.1.1.c> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时05分20秒 ************************************************************************/#include <stdio.h>//在c语言中,空参数代表可以接受任意参数(任意数目,任意类型)int func(){return 3;}int main(int argc, char *argv[]){int a = func(2, 3);int b = func(2.3);printf("%d %d\n", a, b);return 0;}
/*************************************************************************> File Name: 2.2.1.1.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时07分59秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//在c++中,空参数意味着不带参数的函数int func(){return 3;}int main(int argc, char *argv[]){//报错//int a = func(1);//int b = func(2);int a = func();int b = func();cout << a << " " << b << endl;return 0;}
2.2.1.4:
/*************************************************************************> File Name: 2.2.1.4.c> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时35分40秒 ************************************************************************/#include <stdio.h>int add(int, int);int main(int argc, char *argv[]){printf("%d\n", add(1, 2));return 0;}//c语言在函数定义时,参数要求有标示符,c++则不要求//int add(int, int)int add(int a, int b){return a + b;}
/*************************************************************************> File Name: 2.2.1.4_declare.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时23分48秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//extern关键字对于变量来说表明只声明,而不定义(分配存储空间)//而对于函数来说,则是多余的,可选的// Declaration & definition examplesextern int i;// Declaration without definitionextern float f(float);// Function declarationfloat b;// Declaration & definitionfloat f(float a)// Definition{return a + 1.0f;}int i;// Definitionint h(int x)// Declaration & definition{return x + 1;}int main(int argc, char *argv[]){b = 1.0;i = 2;f(b);h(i);return 0;}
2.3.4:
/*************************************************************************> File Name: 2.3.4_hello.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时55分45秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//在c++语言中,mian函数总是返回int类型int main(int argc, char *argv[]){cout << "Hello, World! I am " << 8 << " Today!" << endl;return 0;}2.4:
/*************************************************************************> File Name: 2.4_stream.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 20时59分50秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;int main(int argc, char *argv[]){// Specifying formats whit manipulatorscout << "a number in decimal: " << dec << 15 << endl;cout << "in octal: " << oct << 15 << endl;cout << "in hex: " << hex << 15 << endl;//浮点数的格式由编译器自动确定cout << "a floating-poing number: " << 3.14159 << endl;//显式类型转换(case),任何字符都能转换成char类型cout << "non-printing char (escape): " << char(27) << endl;return 0;}
2.4.1:
/*************************************************************************> File Name: 2.4.1_concat.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时06分45秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//可以实现字符数组的拼接(character array concatenation)//如果两个加引号的字符数组邻接,并且它们之间没有任何标点,//编译器就会把这些字符数组连接成单个字符数组。int main(int argc, char *argv[]){cout << "This is far too long to put on a ""single line but it can be broken up with ""no ill effects\nas long as there is no ""punctuation separating adjacent character ""arrays.\n";return 0;}
2.4.2:
/*************************************************************************> File Name: 2.4.2_numconv.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时14分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Converts decimal to octal and hexint main(int argc, char *argv[]){int number;cout << "Enter a decimal number: ";cin >> number;cout << "value in octal = 0" << oct << number << endl;cout << "value in hex = 0x" << hex << number << endl;return 0;}
2.5:
/*************************************************************************> File Name: 2.5_hellostrings.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时24分39秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//string对象使用示例int main(int argc, char *argv[]){string s1, s2;// Empty stringsstring s3 = "Hello, World.";// Initializedstring s4("I am");// Also initializeds2 = "Today";// Assigning to a strings1 = s3 + " " + s4;// Combining stringss1 += " 8 ";// Appending to a stringcout << s1 + s2 + "!" << endl;return 0;}
2.6:
/*************************************************************************> File Name: 2.6_fillstring.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时32分55秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>// 文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//string具有动态性,不必担心string的内存分配//string会自动扩展以保存新的输入int main(int argc, char *argv[]){ifstream in("2.6_fillstring.cpp");string s, line;while (getline(in, line)) {s += line + "\n";}cout << s << endl;return 0;}
/*************************************************************************> File Name: 2.6_scopy.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时28分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Copy one file to another, a line at a timeint main(int argc, char *argv[]){ifstream in("2.6_scopy.cpp");// Open for readingofstream out("2.6_scopy.cpp~");// Open for writing string s;while (getline(in, s)) {// Discards newline charout << s << "\n";// ... must add it back}return 0;}
2.7:
/*************************************************************************> File Name: 2.7_intvector.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 22时00分48秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;//vector容器可以看成动态扩展的数组,push_back()函数将元素加入到vector的尾部//size()函数返回vector容器的元素个数,并且vector重载了[]操作符,使得可以像数组//一样访问vector中的元素int main(int argc, char *argv[]){vi v;for (int i = 0; i < 10; ++i) {v.pb(i);}for (int i = 0; i < sz(v); ++i) {cout << v[i] << ", ";}cout << endl;for (int i = 0; i < sz(v); ++i) {v[i] = v[i] * 10;//赋值}for (int i = 0; i < sz(v); ++i) {cout << v[i] << ", ";}cout << endl;return 0;}
/*************************************************************************> File Name: 2.7_fillvector.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时54分03秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;// Copy an entire file into a vector of stringint main(int argc, char *argv[]){vector<string> v;ifstream in("2.7_fillvector.cpp");string line;while (getline(in, line)) {v.pb(line);}for (int i = 0; i < sz(v); ++i) {cout << i << ": " << v[i] << endl;}return 0;}
/*************************************************************************> File Name: 2.7_getwords.cpp> Author: gwq> Mail: gwq5210@qq.com > Created Time: 2014年11月05日 星期三 21时57分26秒 ************************************************************************/#include <cmath>#include <ctime>#include <cctype>#include <climits>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <vector>#include <sstream>#include <fstream>//文件读写#include <iostream>#include <algorithm>#define INF (INT_MAX / 10)#define clr(arr, val) memset(arr, val, sizeof(arr))#define pb push_back#define sz(a) ((int)(a).size())using namespace std;typedef set<int> si;typedef vector<int> vi;typedef map<int, int> mii;typedef long long ll;const double esp = 1e-5;int main(int argc, char *argv[]){vector<string> words;ifstream in("2.7_getwords.cpp");string word;//以空白来区分单词,可以使用更复杂的方式分割输入while (in >> word) {words.pb(word);}for (int i = 0; i < sz(words); ++i) {cout << words[i] << endl;}return 0;}
0 0
- C++编程思想第二章例题笔记
- 《java编程思想》第二章 笔记
- java编程思想阅读笔记 第二章
- Java编程思想笔记(第二章)
- Java编程思想---第二章笔记
- java编程思想学习笔记 第二章
- C++编程思想第二版第二卷笔记3——输入输出流
- C++编程思想第二版第二卷笔记5——设计模式
- C++编程思想 第二章C++中的C
- java编程思想第四版第二章学习笔记
- java编程思想 学习笔记---第二章 一切都是对象
- java编程思想笔记——第二章
- Java编程思想-第二章-一切都是对象-2.1-2.2笔记
- Java编程思想-第二章-一切都是对象-2.3-2.5笔记
- Java编程思想笔记——第二章 一切都是对象
- java编程思想学习笔记--第二章 一切都是对象
- JAVA编程思想笔记--第二章 一切都是对象
- 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(一)
- Github多个SSH key切换管理项目
- spring mvc注入配置文件里的属性
- 高速运放输入问题
- hdu 2870 Largest Submatrix (hdu1505加强版)
- [Unity3D]面试题集锦
- C++编程思想第二章例题笔记
- Linux驱动开发十:按键中断之输入子系统
- 提交代码到svn时服务器重启导致svn无法更新问题解决办法
- Git使用图解教程
- 11.5 找window 三种方法
- 阿里云cenos 6.5 模板上安装 docker
- UVA 12108
- 最简单的Phoenix 访问 HBase例子
- spring mvc数据绑定时通过去除html标签防止js注入