准备今后一段时间安排学习一下设计模式,其实大家都说学习编程要多写程序,可是自己的想法是想要从学习设计模式的方面提高自己的代码设计水平,因为现在自己只能在懂得了C++的基本的语法上,做一些垒代码的工作,感觉没啥设计可言。
以后每周学习一个设计模式,不求快,只求坚持,这样的话六个月时间可以学完所有的设计模式了。但是自己前段时间始终从《大话设计模式》这本书的前面往后看,感觉很枯燥,所以写下下面这个代码生成书中介绍的28中设计模式的随机值,在运行后贴出来,然后按照此随机值来学习。
#include <iostream>
#include <vector>
#include<algorithm>
#include<time.h>
using namespace std;
#define DESIGN_PATTEN_NUM 28
#define random(x) (rand()%x + 1)
void PrintVec(const vector<int> &vec)
{
vector<int>::const_iterator citer = vec.begin();
while(citer != vec.end())
{
cout << *citer << " ";
++citer;
}
cout << endl;
}
int main(void)
{
srand((int)time(0));
vector<int> designPatten;
while(designPatten.size() < DESIGN_PATTEN_NUM)
{
int dpNum = random(DESIGN_PATTEN_NUM);
if (designPatten.end() == find(designPatten.begin(), designPatten.end(), dpNum))
{
designPatten.push_back(dpNum);
}
}
PrintVec(designPatten);
return 0;
}
任何一个小程序都是不写不知道,在写这个小程序的时候,发现在调用算法find的时候犯了一个错误,开始是这么调用的:
if (dp.end() == dp.find(dp.begin(), dp.end(), db_num))
然后报错:’find’ : is not a member of ‘std::vector<_Ty>’ ;最后还在网上查了一下才知道find的具体用法。
好运行程序生成的结果如下:
就按照上面的顺序从《大话设计模式》中学习了。