11月1日学习小结
2010-11-01 21:09
260 查看
1、单件模式
上面程序的缺点:就是在getInstance()函数用new产生的Singleton对象在程序结束的时候,不会调用西沟函数,也就是说delete函数没调用,这样会产生内存泄漏。
把上面的程序改为:
这样通过智能指针可以避免内存泄漏。
另外的实现方法:
网上其它的实现方法:
http://blog.csdn.net/shark0001/archive/2010/05/31/5638009.aspx
2、FALSE/TRUE与false/true的区别:
3、
4、C++中static的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927077.aspx
5、C++中const的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927083.aspx
#include <iostream> using namespace std; class Singleton { public: ~Singleton() { printf("析构函数"); } static Singleton* getInstance() { if (NULL == pInstance) { pInstance = new Singleton; } return pInstance; } private: static Singleton* pInstance; Singleton(){} }; Singleton* Singleton::pInstance = NULL; int main() { Singleton *p = Singleton::getInstance(); }
上面程序的缺点:就是在getInstance()函数用new产生的Singleton对象在程序结束的时候,不会调用西沟函数,也就是说delete函数没调用,这样会产生内存泄漏。
把上面的程序改为:
include <iostream> #include <memory> using namespace std; class Singleton { public: ~Singleton() { printf("析构函数/n"); } static Singleton* getInstance() { if (NULL == pInstance.get()) { pInstance = auto_ptr<Singleton>(new Singleton); } return pInstance.get(); } private: static auto_ptr<Singleton> pInstance; Singleton(){} }; auto_ptr<Singleton> Singleton::pInstance(NULL); int main() { Singleton *p1 = Singleton::getInstance(); Singleton *p2 = Singleton::getInstance(); if (p1 == p2) { printf("相等/n"); } }
这样通过智能指针可以避免内存泄漏。
另外的实现方法:
// 练习.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> #include <set> using namespace std; #pragma once class CSingle { public: CSingle() { if (NULL == pInstace) { pInstace = this; printf("构造函数/n"); } } static CSingle* getInstace() { return pInstace; } ~CSingle() { printf("析构函数/n"); } private: CSingle(const CSingle& oth); static CSingle *pInstace; }; CSingle* CSingle::pInstace = NULL; int main() { CSingle instace; //创建CSingle类的唯一实例 CSingle *p; for (int i=0; i<10; i++) { p = CSingle::getInstace();//获取创建的唯一实例的指针 } }
网上其它的实现方法:
http://blog.csdn.net/shark0001/archive/2010/05/31/5638009.aspx
2、FALSE/TRUE与false/true的区别:
1.FALSE/TRUE与false/true的区别: false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义: #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif 也就是说FALSE/TRUE是int类型,而false/true是bool类型;所以两者不一样的,只不过我们在使用中没有这种感觉,因为C++会帮你做隐式转换。 2.bool的大小与BOOL的区别: bool在C++里是占用1字节,而BOOL是int类型,int类型的大小是视具体环境而定的;所以来说:false/true只占用1个字节,而TRUE/FALSE视具体环境而言,以下是BOOL在windef.h中的定义:typedef int BOOL;
3、
一道分治法的题目 现有16枚外形相同的硬币,其中有一枚比真币的重量轻的假币,若采用分治法找出这枚假币,至少比较(63)次才能够找出该假币。 http://topic.csdn.net/u/20101024/21/94EC718E-0F61-4CE2-98BD-C4945494FF18.html
4、C++中static的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927077.aspx
5、C++中const的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927083.aspx
两个三位数相加得到一个四位数,并且这三个数字的每一位都不相同。(也就是要把0-9这10个数全用上) bool check(set<int>& s, int n) { int first = n%10; int second = (n/10)%10; int third = (n/100)%10; pair<set<int>::iterator, bool> result1, result2, result3; s.insert(1); if (1 == s.size()) { s.insert(first); s.insert(second); s.insert(third); return true; } else if (4 == s.size()) { if (s.insert(first).second && s.insert(second).second && s.insert(third).second) { return true; } } else if (7 == s.size()) { if (s.insert(first).second && s.insert(second).second && s.insert(third).second) { return true; } } return false; } int main() { for (int i=200; i<500; i++) { for (int j=500; j<999; j++) { set<int> s; if (check(s, i) && check(s,j) && (i+j-1000 > 100) && check(s, (i+j-1000))) { printf("%d + %d = %d/n", i, j, i+j); } s.clear(); } } }
// 练习.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> #include <set> using namespace std; int main() { int result , a, b, c; a = 9; b = 1; c = 2; result = c-- + c; //加法为右结合,所以result = 4. printf("%d/n", result); return 0; } /* 避免重复包含的方法: (1)#pragma once (2)#ifndef _CLASSNAME_ #define _CLASSNAME_ 这里书写类的代码 #endif */ /* 操作内存:memcpy和memset */ /* 字符串操作 strcpy、strncpy、strlen、strcat、strncat、strcmp、strncmp、strchr、strstr、atoi、atof、atol、sprintf、strftime 一般情况下,使用strncpy时,建议将n置为dest串长度(除非你将多个src串都复制到dest数组,并且从dest尾部反向操作), 复制完毕后,为保险起见,将dest串最后一字符置NULL,避免发生在第2)种情况下的输出乱码问题。 */
// 练习.cpp : 定义控制台应用程序的入口点。 #include "stdafx.h" #include <iostream> #include <set> using namespace std; #pragma once class Point { public: Point(){} Point(int m_x, int m_y); //注意下面两个重载运算符的不同 Point operator + (const Point &oth);//重载加法运算符 Point& operator += (const Point &oth);//重载加法赋值运算符 friend ostream& operator << (ostream& out, const Point& Point); private: int x; int y; }; int main() { Point p1(3, 4); Point p2(4, 5); cout<<"point1 is "<<p1<<endl; cout<<"point2 is "<<p2<<endl; Point p3 = p1 + p2; cout<<"point3 is "<<p3<<endl; p3 += p2; cout<<"point3 is "<<p3<<endl; } Point::Point(int m_x, int m_y) { x = m_x; y = m_y; } Point Point::operator +(const Point &oth) { return Point(this->x + oth.x, this->y + oth.y); } Point& Point::operator +=(const Point &oth) { x += oth.x; y += oth.y; return *this; } ostream& operator <<(ostream& out, const Point& point) { out<<"X="<<point.x<<" Y="<<point.y<<endl; return out; }
相关文章推荐
- 学习生活小结 -- W70 D2
- python学习小结6:模块
- 我的NodeJs学习小结
- sed 命令学习小结
- Vue2.2版本学习小结
- Android学习之 WebView使用小结
- Re:objective-c基础教程——学习小结
- TestLink学习六:TestLink1.9.13工作使用小结
- 网络编程学习小结
- 精通Visual C++图像编程之面向对象的位图编程小结(学习笔记)
- 网络编程学习小结
- PCI9054 学习小结
- CSS选择器学习小结
- 四月,五月学习小结
- javaScript事件学习小结(四)event的公共成员(属性和方法)
- cakephp 学习小结 6
- 中国剩余定理(CRT)学习小结
- 2010年12月份Web技术学习小结