What structure to choose

Discussion in 'C++' started by Vince, May 25, 2005.

  1. Vince

    Vince Guest

    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 :
     
    Vince, May 25, 2005
    #1
    1. Advertising

  2. Vince wrote in news:4294dbca$0$8039$ 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.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, May 25, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    783
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,276
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    633
  4. Replies:
    9
    Views:
    25,491
    Lal Bahadur Singh
    Nov 11, 2011
  5. bigbang

    Help me choose appropriate data structure

    bigbang, Jun 1, 2010, in forum: C Programming
    Replies:
    24
    Views:
    857
    Fire Crow
    Jun 4, 2010
Loading...

Share This Page