Here is a solution which does not use C++11. This example also uses three files just to 'prove' it works.
If the map is not intended to be changed, then the initialization should be moved from foo.cpp to foo.h and made const.
foo.h
// FOO.H STARTS HERE
#include <map>
#include <string>
#include <vector>
typedef std::pair<std::string, std::string> stringpair_t;
const stringpair_t map_start_values[] = {
stringpair_t("Cat", "Feline"),
stringpair_t("Dog", "Canine"),
stringpair_t("Fish", "Fish")
};
const int map_start_values_size = sizeof(map_start_values) / sizeof(map_start_values[0]);
extern std::map <std::string, std::string> my_map;
// FOO.H ENDS HERE
foo.cpp
// FOO.CPP STARTS HERE
#include <iostream>
#include "foo.h"
std::map<std::string, std::string> my_map (map_start_values, map_start_values + map_start_values_size);
int main2(void);
int main (void) {
for (std::map<std::string, std::string>::const_iterator i = my_map.begin(); i != my_map.end(); ++i) {
std::cout << "A " << i->first << " is a " << i->second << std::endl;
}
main2();
return 0;
}
// FOO.CPP ENDS HERE
foo2.cpp
// FOO2.CPP STARTS HERE
#include "foo.h"
#include <iostream>
int main2 (void) {
for (std::map<std::string, std::string>::const_iterator i = my_map.begin(); i != my_map.end(); ++i) {
std::cout << "A " << i->first << " is a " << i->second << std::endl;
}
return 0;
}
// FOO2.CPP ENDS HERE