How to store key value pair?

This is a discussion on How to store key value pair? within the C++ Programming forums, part of the General Programming Boards category; Hi , I want to store data which is in key value pair. Here one key can have many values. ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Question How to store key value pair?

    Hi ,
    I want to store data which is in key value pair.
    Here one key can have many values.

    example:
    email : abc@gmail.com
    pqr@rediff.com

    phone : 9877766655
    3452781190

    fax : 876765785
    8686798798

    uid: hhg


    name : john


    Here email , phone ,fax,uid ,name ---These are keys.
    Key may have one or many values.

    Please suggest the way to store these multivalued keys.

    Thanks and Regards
    Ketu

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    One way would be to use a std::map<std::string, std::vector<std::string> >
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Smile

    Thanks a lot.
    But I'll not be knowing how many vectors will be required at run time.
    Because i'll be reading this data from text file. For example -

    # entry-id: 5
    dn: uid=aadsfvg,ou=ou1,dc=persistent,dc=co,dc=in
    telephoneNumber: 435
    telephoneNumber: 5345
    telephoneNumber: 243
    mail: dfs@f.fsr
    mail: gfdf@gf.fer
    uid: aadsfvg
    facsimileTelephoneNumber: 4235
    facsimileTelephoneNumber: 442
    givenName: abc
    objectClass: top
    objectClass: person
    objectClass: organizationalPerson
    objectClass: inetorgperson
    sn: adsfvg
    cn: abc adsfvg
    creatorsName: uid=admin,ou=administrators,ou=topologymanagement, o=netscaperoot
    modifiersName: uid=admin,ou=administrators,ou=topologymanagement, o=netscaperoo
    t
    createTimestamp: 20080101141712Z
    modifyTimestamp: 20080101141712Z
    nsUniqueId: 2c1baa81-1dd211b2-80ef99ff-c4c2cf3a


    This data I'll be reading from text file.

    I'll process data after storing it into map and then I'll erase data in map and will read next entry from file.
    Next entry may have different parameters
    example -

    next entry from file mifght be like this :

    # entry-id: 2
    dn: cn=Directory Administrators, dc=persistent,dc=co,dc=in
    objectClass: top
    objectClass: groupofuniquenames
    cn: Directory Administrators
    creatorsName: cn=directory manager
    modifiersName: cn=directory manager
    createTimestamp: 20080101134100Z
    modifyTimestamp: 20080101134100Z
    nsUniqueId: 2f606782-1dd211b2-80ea99ff-c4c2cf3a


    x

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    But I'll not be knowing how many vectors will be required at run time.
    I dont see a problem since you can add as many key/value pairs as is needed with the std::map.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Hi


    I tried it using multimap.
    But I am not able to find all the values of a particular key using multimap.
    Last edited by ketu1; 01-03-2008 at 02:38 AM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Out of this Following is the code where I am tring to read values from multimap:
    Code:
    mapAttributes.insert(pair<const char *,const char *>(pchAttType,pchAttValue));							
    						}						
    					}	
    					else
    					{
    						//This is end of entry, so break while loop.
    						break;
    					}
    				}
    				
    				const int iSize = mapAttributes.size();
    				cout<<"\n Size of Multimap : "<<iSize<<endl;
    				
    				
    		
    				
    				for(itrMap=mapAttributes.begin(); itrMap!= mapAttributes.end(); itrMap++)
    				{					
    					cout<<endl<<(*itrMap).first<<"    : "<<(*itrMap).second<<endl;	
    					const char * const & pchKey=(*itrMap).first;
    					itrKey= mapAttributes.find(pchKey);
    					itrUpper=mapAttributes.upper_bound(pchKey);
    					cout<<"\n.................Elements associated with "<<pchKey<<"............................\n";
    					for (;itrKey!= itrUpper;itrKey++)
    					{
    						cout<<"\t"<<itrKey->second;
    					}
    				}

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    But every time it is returning single value and not all the values for that key.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    If I'll use std::map<std::string, std::vector<std::string> >
    How I'll come to know how many vectors are required?
    Please help.
    thanks in advance.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ketu1 View Post
    If I'll use std::map<std::string, std::vector<std::string> >
    How I'll come to know how many vectors are required?
    Please help.
    thanks in advance.
    What do you mean?

    You need one vector per key-string. The vector will have as many elements as you have added. Vectors are dynamically resized unless you specifically work towards NOT doing so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Yes .. But I'll not be knowing how many keys are there in a file.
    Like I am reading keys fro file.
    The file data may be like below:
    telephoneNumber: 435
    telephoneNumber: 5345
    telephoneNumber: 243
    mail: dfs@f.fsr
    mail: gfdf@gf.fer
    uid: aadsfvg
    facsimileTelephoneNumber: 4235
    facsimileTelephoneNumber: 442
    givenName: abc
    objectClass: top
    objectClass: person
    objectClass: organizationalPerson
    objectClass: inetorgperson
    sn: adsfvg
    cn: abc adsfvg
    creatorsName: uid=admin,ou=administrators,ou=topologymanagement, o=netscaperoot
    modifiersName: uid=admin,ou=administrators,ou=topologymanagement, o=netscaperoo
    t
    createTimestamp: 20080101141712Z
    modifyTimestamp: 20080101141712Z
    nsUniqueId: 2c1baa81-1dd211b2-80ef99ff-c4c2cf3a



    or it may be as below:
    dn: dc=persistent,dc=co,dc=in
    objectClass: top
    objectClass: domain
    dc: persistent
    creatorsName: cn=directory manager
    createTimestamp: 20080101134059Z
    nsUniqueId: 2f606781-1dd211b2-80ea99ff-c4c2cf3a
    aci: (targetattr != "userPassword") (version 3.0; acl "Anonymous access"; allo
    w (read, search, compare)userdn = "ldap:///anyone"


    So depending on data in the file , each tme number of keys required will be different. and hence the number of vectors as well.
    am I right?
    Please guide how to implememt that.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you have different sets of data, you will need/get a different set of vectors - but isn't that exactly what you want?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Yes... i want to implement that only.
    But not getting which way should I follow.
    First I tried implementation using multimap.
    In that case storing data was OK.
    But i was not able to retrive the data.
    i.e. I was not able to retrive how many and which values are there for key "telephonenumber" in my multiset.
    So As "laserlight' author has suggested, now I am tring to implement the same using vector.
    Please help.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    What I am implementing here is reading the data from LDIF file and add entries into iPlanet directory server accordingly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "action on key hold" programs?
    By siddharth2212 in forum C++ Programming
    Replies: 11
    Last Post: 07-29-2007, 12:19 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  3. Replies: 3
    Last Post: 05-25-2005, 02:50 PM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  5. key problem
    By madsmile in forum Game Programming
    Replies: 20
    Last Post: 06-05-2002, 11:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21