现在的位置: 首页 > 综合 > 正文

指针+内存池的优化

2017年12月16日 ⁄ 综合 ⁄ 共 579字 ⁄ 字号 评论关闭

在 quque 或 set 或 map 中,存结构体指针加速(省去调用构造函数)。

附一个队友 yzwsm 共享的链接

#include <set>
#include <stdio.h>
#include <algorithm>

using namespace std;

struct T {
    int x;
    T(){}
    T(int _x):x(_x){}
    bool operator < (const T& B) const {
        return x < B.x;
    }
};

template<typename T>
struct Cmp {
    bool operator () (const T& pa,const T& pb) const {
        return *pa < *pb;
    }
};

set< T*, Cmp<T*> > s;

T a[400];

int main()
{
    T* ptop = &a[0];
    ptop->x = 5;
    s.insert(ptop++);
    ptop->x = 1;
    s.insert(ptop++);
    ptop->x = 4;
    s.insert(ptop++);
    ptop->x = 3;
    s.insert(ptop++);
    ptop->x = 2;
    s.insert(ptop++);
    ptop->x = 6;
    s.insert(ptop++);
    for(set<T*>::iterator it=s.begin();it!=s.end();it++)
    {
        printf("%d\n",(*it)->x);
    }
    return 0;
}

抱歉!评论已关闭.