为可压缩存储,我真是煞费苦心,能不能帮我看看还有没有其他方法实现双下标?

zzunicholas 2006-12-18 10:55:04
能不能帮我看看还有没有其他方法实现双下标

#include <assert.h>
#include <iostream>
#define HALF(X) ((X+1)/2)

class CBit4Matrix
{
typedef struct
{
unsigned char High:4;
unsigned char Low:4;
}DFOURBITS;

class CBit4Value
{
DFOURBITS& _Value;
bool _bLow;

public:
CBit4Value(DFOURBITS& value,bool bLow):_Value(value) {};
virtual ~CBit4Value() {};

unsigned char operator= (unsigned char n)
{assert(n<16);_bLow?_Value.Low=n:_Value.High=n;return n;}
operator unsigned char () {return _bLow?_Value.Low:_Value.High;}
};

class CBit4Line
{
DFOURBITS* _pLine;
CBit4Matrix* _pMat;
public:
CBit4Line(DFOURBITS* p,CBit4Matrix* pMat):_pLine(p),_pMat(pMat)
{ASSERT(nLn>0 && nCol>0);
_pData = new DFOURBITS[_sLn*HALF(_sCol)];}
virtual ~CBit4Line()
if (_pData)
{delete []_pData;_pData = NULL; }

CBit4Value operator [] (long nCol)
{assert(nCol<_pMat->getCol() && nCol>=0);
CBit4Value rMV(_pLine[nCol/2],nCol%2);
return rMV;}
};

DFOURBITS *_pData;
long _sLn,_sCol;
///< 逻辑上的总行列数 \n\e不是物理上的 \n\e 使用时要加HALF宏转变成物理上的

public:
CBit4Matrix();
CBit4Matrix(long nLn,long nCol);
virtual ~CBit4Matrix();

long getLn() const {return _sLn;}
long getCol() const {return _sCol;}

CBit4Line operator [] (long nLn)
{assert(nLn<_sLn && nLn>=0);
CBit4Line r(_pData+nLn*HALF(_sCol),this);
return r;}

friend std::ostream& operator<<(std::ostream& os,CBit4Matrix& m)
{
long i,j;
for (i=0;i<m._sLn;i++)
{
for (j=0;j<HALF(m._sCol);j++)
{
os<<(int)m._pData[i*HALF(m._sCol)+j].High<<" ";
os<<(int)m._pData[i*HALF(m._sCol)+j].Low<<" ";
}
os<<std::endl;
}
return os;
}

};

就是为了实现双下标,好麻烦,呵呵,
void test_run()
{
CBit4Matrix m(3,3);

int i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
m[i][j] = i*3+j;
cerr<<i*3+j;
}cerr<<endl;
}
cerr<<m;//这块还有点问题,莫名奇妙,再研究研究
//error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CBit4Matrix' (or there is no acceptable conversion)

for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cerr<<(int)m[i][j]<<' ';
}cerr<<endl;
}
}
...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzunicholas 2006-12-18
  • 打赏
  • 举报
回复
哈哈哈,楼上的还真实际

有没有给点技术上的评价?
robothn 2006-12-18
  • 打赏
  • 举报
回复
lazy evaluation
KthTes 2006-12-18
  • 打赏
  • 举报
回复
先纠正一下,应该用#include <cassert>
QQ_370566617 2006-12-18
  • 打赏
  • 举报
回复
CBit4Value(DFOURBITS& value,bool bLow):_Value(value) {};

缺了一个参数初始化

这样做中看不中用,频繁的构造与析构会很费时间
当然个人意见,不知道其它人怎么看。

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧