1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Map with key as structure

Discussion in 'C' started by man4ish, Mar 10, 2009.

  1. man4ish

    man4ish New Member

    Joined:
    Apr 5, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    How to make map with two kwys. I am using struct.
    Code:
    struct Key
    {
    char * gi;
    char * offset;
    Key(const char * _gi, const char * _offset)
    {
    gi = new char [strlen(_gi) + 1];
    strcpy(gi, _gi);
    offset = new char [strlen(_offset) + 1];
    strcpy(offset, _offset);
    }
    ~Key()
    {
    if (gi != NULL)
    {
    delete [] gi;
    gi = NULL;
    }
    if (offset != NULL)
    {
    delete [] offset;
    offset = NULL;
    }
    }
    };
    struct Value
    {
    char * orient;
    char * allele ;
    Value(const char * _orient, const char * _allele)
    {
    orient = new char [strlen(_orient) + 1];
    strcpy(orient, _orient);
    allele = new char [strlen(_allele) + 1];
    strcpy(allele, _allele);
    }
    ~Value()
    {
    if (orient != NULL)
    {
    delete [] orient;
    orient = NULL;
    }
    if (allele != NULL)
    {
    delete [] allele;
    allele = NULL;
    }
    }
    };
    
    struct ltstr
    {
    bool operator()(const Key* _lhs, const Key* _rhs) const
    {
    return (strcmp(_lhs->gi,_rhs->gi) > 0 && strcmp(_lhs->offset,_rhs->offset) );
    }
    };
    Key key ("34", "abcd");
    Value value ("sdf", "asdf");
    n.insert(dbmap::value_type(&key,&value));
    
    When i try to retrieve the keys and values using iterator to map, i am not getting
    anything.
    Can you tell me where i am wrong?
    I will be really thankful if you can help me.

    Regards
    Man
     
    Last edited by a moderator: Mar 10, 2009

Share This Page