What structure to choose

V

Vince

Hi,

I would like to associate a couple of value(SFID and nRecNo) with a byte
array.
So I tried the following structure :

typedef std::vector<BYTE> ByteArray;

class CardIndex
{
public:

CDataIndex(int nSFID, int nRecNo){ m_nSFID = nSFID; m_nRecNo = nRecNo; }

int m_nSFID;
int m_nRecNo;
};


class CCardData
{
public:
CCardData();
virtual ~CCardData();

private:
int AddTag(int nSFID, int nRecNo);

map<CardIndex, ByteArray> m_FileData;
}

CCardData::AddTag(int nSFID, int nRecNo)
{

m_FileData[new CardIndex(nSFID, nRecNo) ] = new ByteArray


return 0;
}


but it cannot work.
I don't think this structure is good. How would you do it ?
For instance I want ta declare several buffer like this :
 
R

Rob Williscroft

Vince wrote in in
comp.lang.c++:
Hi,

I would like to associate a couple of value(SFID and nRecNo) with a
byte array.
So I tried the following structure :

typedef std::vector<BYTE> ByteArray;

class CardIndex
{
public:

CDataIndex(int nSFID, int nRecNo){ m_nSFID = nSFID; m_nRecNo =
nRecNo; }

int m_nSFID;
int m_nRecNo;

bool operator < ( CDataIndex const & rhs ) const
{
return m_nSFID == rhs.m_nSFID ?
m_nRecNo < rhs.m_nRecNo : m_nSFID < rhs.m_nSFID
;
}
};


class CCardData
{
public:
CCardData();
virtual ~CCardData();

private:
int AddTag(int nSFID, int nRecNo);

map<CardIndex, ByteArray> m_FileData;

A std::map<> 's KeyType must be Have "Strict Weak Ordering" or
you must give a third "functor" argument to std::map<> that
provides this ordering.

The simplest way to do this in this case is to provide the
defenition of operator < () as I did above.
}

CCardData::AddTag(int nSFID, int nRecNo)
{

m_FileData[new CardIndex(nSFID, nRecNo) ] = new ByteArray


return 0;
}


but it cannot work.
I don't think this structure is good. How would you do it ?
For instance I want ta declare several buffer like this :

You could also use std::pair<> :

#include <ostream>
#include <iostream>
#include <iomanip>
#include <map>
#include <utility>
#include <vector>

typedef
std::map< std::pair< int, int >, std::vector< unsigned char > >
CCardData
;


int main()
{
CCardData ccd;

for ( int i = 0; i < 10; ++i )
for ( int j = 0; j < 10; ++j )
{
std::vector< unsigned char > &buf = ccd[ std::make_pair( i, j ) ];
for ( unsigned c = 0; c < 256; ++c )
{
buf.push_back( (unsigned char)c );
}
}

std::vector< unsigned char > &buf = ccd[ std::make_pair( 4, 5 ) ];

std::vector< unsigned char >::iterator ptr, lim;
ptr = buf.begin();
lim = buf.end();

for (; ptr != lim; ++ptr )
{
std::cout << std::hex << unsigned(*ptr) << '\n';
}
}

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top