5

There are n no of string which need to map with another string.

Ex :     Bacardi_old - > Facundo 
         Smirnoff_old -> Pyotr 
         Seagram_old  -> Joseph
         This keep on ..... may be around 1000

There are some string which need to map with duplicate string.

Ex :     Bacardi_new  -> Facundo 
         Smirnoff_new -> Facundo 
         Seagram_new  -> Facundo 

Requirement: As Below case

case 1: when brand name input. Owner name as output.

input : Bacard_old
output: Facundo

case 2: When owner name input brand name as output.

input : Facundo
output : Bacardi_old, Bacardi_new ,Smirnoff_new ,Seagram_new 

My Approach:

1.I have a map as below :

std::map<std::string,std::vector<std::string>> Mymap;

2.Should i create two map one unique mapping and another for duplicate

std::map<std::string,std::string>Mymap
std::map<std::string,std::vector<std::string>>Mymap

Is the second option good than first in terms of all the aspect. Please suggest the best approach.

Note : I am stick with c++11. No boost library.

ava
  • 368
  • 1
  • 7
  • 2
    may be a duplicate of https://stackoverflow.com/questions/42209189/add-values-to-a-multiset-in-a-boostbimap Did you have a look at boost bimap? https://www.boost.org/doc/libs/1_68_0/libs/bimap/doc/html/index.html – Marc Sep 26 '18 at 17:42
  • I have mentioned i am stick to c++11 because boost library is not allowed – ava Sep 26 '18 at 17:50
  • Thanks for updating the question accordingly (boost was not mentioned there, before). There is not necessarily a causal relationship between boost and c++11, though. boost has been around before c++11 was a thing – Marc Sep 26 '18 at 17:53
  • 1
    if you can search by key or by value what about just using the map> and putting both the brand and the owner in as keys and keeping both vectors up to date. Only one map so less memory than two maps but still two insertions instead of one. I think it is a good compromise. You could prepend the key with "brand:" or "owner:" if you are worried that an owner might have the same name as a brand. – Jerry Jeremiah Sep 26 '18 at 23:28

2 Answers2

2

The best approach depends on your needs. Are you interested in access speed or insertion speed? Or are you interested in reducing the used memory space?

The first solution that you propose (a map with key=brand and value=owner) uses less memory but requires a full scan to perform a search by owner.

The second solution:

  • a map with key=brand and value=owner
  • a map with key=brand and value= list of owners

is faster for both search by owner and search by brand. However, it requires more memory and you also need to perform 2 insertions for each new pair.

Amedeo
  • 847
  • 7
  • 17
  • interested in access speed and insertion speed – ava Sep 26 '18 at 17:46
  • 1
    Keeping it simple, the second solution is better for access and insertion speed. You want to avoid a full scan of the map (that requires an amount of time that increases with the number of elements in the map). – Amedeo Sep 26 '18 at 18:01
1

Best is highly relative :)

You can use std::multimap for the same.

std::multimap<std::string,std::string> my_map;
my_map.insert(std::make_pair("owner_name", "brand_name"));

Now you can search based on key or value depending upon what you need.

Shravan40
  • 8,922
  • 6
  • 28
  • 48