-1

i want to load a map by reading a file in a application startup, and want to utilize this map in some other class to look up for a particular string and execute logic.

Loading of a map should be done only once in application life cycle.

would like to know best approach to declare this map and access in some other logic.

kalamad
  • 3
  • 2
  • _"would like to know best approach to declare this map and access in some other logic."_ Pass a `std::map<>` instance (`const` reference) around. – πάντα ῥεῖ Jul 05 '15 at 15:58

1 Answers1

1

Best approach would be to load it once

const std::map<key_type,value_type>& theMap = loadMap();

and pass the const reference to other functions:

 std::map<key_type,value_type>::const_iterator 
 find_key(key_type key, const std::map<key_type,value_type>& map) {
     return map.find(key);
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Probably, since you need somewhere to store the map. A class with an appropriate member variable is probably a better solution that a global variable or a static local variable in the `loadMap()` function. – πάντα ῥεῖ Jul 06 '15 at 10:20