《Essential C++》系列笔记之第二章(面向过程的编程风格)之第九节(设定头文件)

在这里插入图片描述
代码实践

  • 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 🎉🎂生日👩快乐!🎂🎉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值