代码实践
-
main函数文件
#include <iostream> using namespace std; #include "ALL.h" //如果头文件中没有使用extern声明那么在其他的文件里面Array就无法使用 int Array[Max] = { 1,2,3,4,5 }; int main() { cout << Max << endl; //这个Max如果没有定义就代表ALL.h的Max const int Max = 100; //这样做并不会重定义 cout << IsOdd(2) << endl; Print(2); system("pause"); return 0; }
-
头文件
#pragma once #include <iostream> using namespace std; //const object是第一个例外 const int Max = 5; //这里Max定义必须写在头文件中 extern int Array[Max]; //加上extern才算是声明 void Print(int); //普通函数在头文件中只能声明,防止重定义 //inline函数是第二个例外 inline bool IsOdd(int num) //inline函数定义必须写在头文件中 { return (num % 2 ? true : false); }
-
程序代码文件
#include "ALL.h" void Print(int num) { cout << "Hello :" << num << endl; cout << Array[2] << endl; }
习题处理
-
练习2.2,2.4
#include <iostream> using namespace std; #include <vector> #include <string> vector<int>* Pentagonal(int size) { const int Max = 1024; if (size <= 0 || size >= Max) { cerr << "Error: size <= 0 || size >= 1024" << endl; return 0; } static vector<int> v; //既然是动态数组,就不要轻易的给vector限制数目 for (int i = v.size(); i < size; i++) { //v[v.size()] = (i + 1) * (3 * (i + 1) - 1) / 2; //错误,会越界 v.push_back((i + 1) * (3 * (i + 1) - 1) / 2); //必须用push_back才可以增加容量 } return &v; } void PrintPentagonal(vector<int> *v, string name = "Pentagonal") { if (!v) { cerr << "Error: !v" << endl; return; } cout << name << ": "; for (int i = 0; i < (*v).size(); i++) { cout << (*v)[i] << ' '; } cout << endl; } int main() { int size; cin >> size; vector<int>* v = Pentagonal(size); PrintPentagonal(v); system("pause"); return 0; }
-
练习2.3
inline bool IsTrue(int size) { const int Max = 1024; if (size <= 0 || size >= Max) { return false; } return true; }
-
练习2.5
#include <iostream> using namespace std; #include <string> #include <vector> #include <algorithm> int Max(int a, int b) { return a > b ? a : b; } double Max(double a, double b) { return a > b ? a : b; } string Max(string a, string b) { return a > b ? a : b; } int Max(vector<int> &v) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } float Max(vector<float>& v) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } string Max(vector<string>& v) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } int Max(vector<int>& v, int size) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } float Max(vector<float>& v, int size) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } string Max(vector<string>& v, int size) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } int main() { //cout << Max(1.2, 2.3) << endl; //可以代表abc /*int a[] = { 1,324,125,45,234 }; vector<int> vi(a, a + 5); cout << Max(v) << endl; */ //可以代表def /*string str[] = { "hello","I","am","hmj" }; vector<string> vs(str, str + 4); cout << Max(vs, 4) << endl;*/ //可以代表ghi system("pause"); return 0; }
-
练习2.6
#include <iostream> using namespace std; #include <string> #include <vector> #include <algorithm> template<typename T> T Max(T a, T b) { return a > b ? a : b; } template<typename T> T Max(vector<T> &v) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } template<typename T> T Max(vector<T>& v, int size) { sort(v.begin(), v.end()); return *(v.end() - 1); //记住要减一 } int main() { //cout << Max(1.2, 2.3) << endl; //可以代表abc /*int a[] = { 1,324,125,45,234 }; vector<int> vi(a, a + 5); cout << Max(vi) << endl;*/ //可以代表def /*string str[] = { "hello","I","am","hmj" }; vector<string> vs(str, str + 4); cout << Max(vs, 4) << endl; */ //可以代表ghi system("pause"); return 0; }
今天是20200306 🎉🎂生日👩快乐!🎂🎉