How to create a map with two keys

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    #1

    How to create a map with two keys

    How to make map with two kwys. I am using struct.
    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()(cons t 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(&ke y,&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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are writing C++ so there's little point in using C-string library functions.

    In C++ you use a map for associating a key with a value. In this case you would use two maps:

    Code:
    map<string, string* value> key1;
    map<string, string* value> key2;
    So, then create a string and insert its address into the key1 map along with whatever the key1 value is. Then insert the same string address into the key2 map and whatever the key2 vaue is.

    You are done.

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      Originally posted by weaknessforcats
      You are writing C++ so there's little point in using C-string library functions.

      In C++ you use a map for associating a key with a value. In this case you would use two maps:

      Code:
      map<string, string* value> key1;
      map<string, string* value> key2;
      So, then create a string and insert its address into the key1 map along with whatever the key1 value is. Then insert the same string address into the key2 map and whatever the key2 vaue is.

      You are done.
      This will be expensive in terms of speed. and Also if there are four keys then .It should be generalized.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If you need a general-purpose structure where the number of keys is not known you may be inrterested in a association table. This is a database term for the Mediator design pattern.

        Here you have a key and a value:

        Code:
        string*  v1 = new string("Hello");
        string* v2 = new string("Howdy");
        string* v3 = new string("Man4ish");
        double* v4 = new double(3.14159);
        
        //----the association table-------
        Greeting     v1
        Greeting     v2
        Name         v3
        pi               v4
        v1             Greeting
        v1             StringAddress
        v2             Greeting
        v2             StringAddress
        v3             Name
        v3             StringAddress
        v4             pi
        v4            DoubleAddress
        StringAddress     v1
        StringAddress     v2
        StringAddress     v3
        DoubleAddress    v4
        The column on the left is the key and the column on the right is the value.

        This table can be implemented as a multimap<string , string>. This allows duplicate keys. The string addresses are converted to strings and inserted in the map.

        To see the strings in the database you use a key of StringAddress. The multimap<>::low er_bound method returns an iterator to the first StringAddress key in the database and multimap<>::upp er_bound returns an iterator one beyond the last StringAddress key.

        Using these iterators you can iterate through the StringAddress keys. Each value is a string containing the address of a string. Convert that string into a string* to see the actual string.

        Notice pi is a double. By using DoubleAddress as the key you can locate all the doubles in the database.

        By using Greeting, you locate all the Greeting strings in the database.

        When you delete a string, say v2, then access the data base for v2 as a StringAddress and delete every entry for v2. That means: a) fetch all v2 entries and delete those entries and b) fetch all values for those v2 entries and delete thise entries as well.

        In this case you would delete:

        Code:
        Greeting     v2
        StringAddress     v2
        v2             Greeting
        v2             StringAddress
        Finally, you could probably generalize this by using a polymorphic base class and store pointers to derived objects of this class as values. Most likely you would use a Visitor to do the actual fetching.

        Comment

        Working...