Thread: How to store key value pair?

  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 : [email protected]
    [email protected]

    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
    28,413
    One way would be to use a std::map<std::string, std::vector<std::string> >
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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: [email protected]
    mail: [email protected]
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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: [email protected]
    mail: [email protected]
    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, 11:19 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Replies: 3
    Last Post: 05-25-2005, 01: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, 10:57 PM