
STL
文章平均质量分 62
纯属虚构
互联网后台开发大数据Linux
展开
-
operator new【STL】
知识点: C++中的::operator new和::operator delete只负责分配空间,不负责构造与析构,与malloc和free功能差不多。 测试: #include using namespace std; class A { public: A(){cout<<"default construction"<<endl;} A(int t_)原创 2013-06-09 16:07:46 · 860 阅读 · 0 评论 -
auto_ptr自己实现|临时对象是可度的吗
自己实现auto_ptr,与std::auto_ptr一样。 头文件 _auto_ptr.h #ifndef __auto_ptr_H_ #define __auto_ptr_H_ //#define __debug // _auto_ptr.h // 自己实现一个auto_ptr #include using namespace std; template class _auto_p原创 2013-06-14 14:28:58 · 703 阅读 · 0 评论 -
vector如何释放空间的问题
#include #include using namespace std; template void swap(vector &v2) { vectorv1(v2); v1.swap(v2); } int main() { vector v1; cout<<"v1"<<endl; cout<<"原始: "<<v1.capacity(原创 2013-06-16 01:10:42 · 9448 阅读 · 0 评论 -
禁止在栈中初始化的类【2】
将构造和析构设置为private,即可禁止栈中实例化。如果用protected,则可以被继承。 // 禁止在栈中实例化。那就只有在堆中实例化喽? // 在栈中实例化需要干嘛呢?构造和析构。将其中之一设为private即可。 // 然后就是单件模式的套路了~在成员函数中提供接口,执行new和delete操作。 // PS:将构造和析构设计为私有,同时也就禁止了继承。 // 但是如果设计原创 2013-06-15 23:25:31 · 1075 阅读 · 0 评论 -
禁止在堆中实例化的类【1】
设计一个禁止在堆中实例化的类,需要重载operator new和operator delete,并将其设为private。当然,也不是没有破解的方法,那就是placement new。 // 禁止在堆中实例化对象? // 在堆中实例化对象,需要使用new和delete,因此如果想要禁止,那就重载... // (1)禁止用户在堆中实例化对象:将new和delete重载为私有或保护函数原创 2013-06-15 22:46:02 · 1007 阅读 · 0 评论 -
禁止被继承的类【3】
// 不能被继承的类 // 【1】显然继承需要构造函数和析构函数,因此将它们定义为private可以防止继承, // 这样的话只能在栈中实例化,典型的例子是单件模式,或者其退化版本。 #include using namespace std; class S // 标准单件模式1 { public: static S* get_instance() { if原创 2013-06-15 23:48:22 · 787 阅读 · 0 评论 -
1亿个整数求top 10000
参考资料: http://bbs.youkuaiyun.com/topics/250038051 【1】遍历数组,求出最大值,与arr[0]元素交换,再在arr[1]到arr[100000000-1]之间求最大值,与arr[1]交换。。类似选择排序。 #include #include #include #include #include using namespace原创 2013-06-20 19:42:26 · 2163 阅读 · 0 评论