+ 1

How to use easy search on unordered map

Hi I have three field to be stored. For example, Id, name and last name but this three fields are available at different point of time during code execution. First of all, I have all id with me as input and available to me at one go initially. Later on during execution further, I will have id and name together and later on name and last name together. I thought to add unordered map with these three fields as structure and id as key. When I get name and Id , it's easy to find Id in map and append name.... This should be O(1) operation. Now when I get name and last name , it is difficult to search name as it is not key of map.this might be costly as it will not be O(1)... Will it be? Please suggest design decisions on this... Is unordered map right choice ? Feel free to ask for clarification on issue and many thanks in advance for your help.

21st Oct 2021, 10:26 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 4
Maybe 2 maps will work. (mp1(int,string), mp2(string,string)) Like one for id->name, and other for name->lastname lastname you can get from id using above 2 maps. lastname=mp2[mp1[id]] //but O(1) complexity ? I am not sure.
22nd Oct 2021, 10:14 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
0
Are these two additional fields of structure ? When I get last name as input, it is like I will get name and last name... How to link this with name and id ? Even if I have two additional fields or entities , what would be key to search for ?
21st Oct 2021, 12:55 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Name is only first name Like below example 001 : id Ketan : name Lalcheta : last name
21st Oct 2021, 2:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
- 1
I am sorry but that would not solve the purpose in my opinion... What you are achieving through is solved by creating map<id,clsStruct> where clsStruct can be class or struct having three fields name , id and last name Only issue I am facing is to improve performance when mapping last name with Id and name present into map
21st Oct 2021, 2:44 PM
Ketan Lalcheta
Ketan Lalcheta - avatar