随笔 - 87  文章 - 279  trackbacks - 0
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 211617
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

find y-mis -type d -exec mkdir -p utf/{} \;
find y-mis -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} \;
posted @ 2010-05-06 17:20 豪 阅读(355) | 评论 (0)编辑 收藏
   char result[100];
   int num=24;
   sprintf( result, "%d", num );
posted @ 2008-07-25 11:34 豪 阅读(1084) | 评论 (5)编辑 收藏
     摘要: 再参考了《Modern C++ Design》的FixedAllocator的设计,并且优化了一下算法,虽然最坏时间复杂度还是O(N)的,但是在通常情况下,new/delete的使用已经获得了比较好的性能了。Chunk.h和version1.0的差不多,只是去掉了析构函数,让Chunk直接被FixedAlloctor操作Chunk.h #ifndef CHUNK_H#define&nb...  阅读全文
posted @ 2008-04-21 16:15 豪 阅读(3218) | 评论 (3)编辑 收藏
     摘要: 用了《Modern C++ Design》上的那个Chunk,在Chunk查找Block的时间是O(1),但是在MemPool的ChunkList里面查找某内存地址却需要O(n)的时间复杂度,因为我的算法只是线性的便利ChunkLisk的链表,所以但内存池里面同时存在大量小对象时候,效果不是很好,比普通的new还差,但是如果内存池内同时存在的小对象的数目较小的时候,可以获得不错的性能,计划vers...  阅读全文
posted @ 2008-04-20 17:53 豪 阅读(646) | 评论 (0)编辑 收藏
智能指针源码,可像普通指针一样使用,但可以自动释放内存,代码很短,看一下就明白原理了。
#ifndef AUTOPTR
#define AUTOPTR

/**
 * 智能指针类
 
*/

template
<class T>
class AutoPtr {
public :
    AutoPtr(T
* p = 0) : pointee(p) {} //默认构造函数

    template
<class U>
        AutoPtr(AutoPtr
<U>& rhs) : pointee(rhs.release()) {}//复制构造函数

    
~AutoPtr() {delete pointee;}

    template
<class U>
    AutoPtr
<T>& operator=(AutoPtr<U>& rhs) //赋值函数
        if (this != &rhs) {
            reset(rhs.release());
        }

        
return *this;
    }


    T
& operator*() const {return *pointee;} 
    
    T
* operator->() const {return pointee;}

    T
* get() const {return pointee;} //获取dumb pointer

    T
* release() //释放dumb pointer 的拥有权,并返回其值
        T* oldPointee == pointee;
        pointee 
= 0;
        
return oldPointee;
    }
 
    
    
void reset(T* p=0//重复置p指针
        if (pointee != p) {
            delete pointee;
            pointee 
= p;
        }

    }


private :
    T
* pointee;
}
;

#endif AUTOPTR

test.cpp
#include "AutoPtr.h"
#include 
<iostream>
#include 
<string>
using namespace std;

int main() {
    AutoPtr
<int> p = new int;
    
*= 100;
    printf(
"%d\n"*p);

    AutoPtr
<string> sp = new string;
    
*sp = "hello world";
    printf(
"%s\n", sp->c_str());
    
return 0;
}

posted @ 2008-04-17 17:01 豪 阅读(974) | 评论 (3)编辑 收藏
仅列出标题
共18页: 1 2 3 4 5 6 7 8 9 Last