c++ - How to map 2 char* arrays directly into std::map<std::string,std::string> -
i have 2 char arrays "const char *arr1[arrsize] = {"blah1", "wibble1", "shrug1"};". putting them vector found nice quick solution:
void fillvectest() { const int arrsize = 3; const char *arr1[arrsize] = {"blah1", "wibble1", "shrug1"}; const char *arr2[arrsize] = {"blah2", "wibble2", "shrug2"}; std::vector<std::string> vec1(arr1, arr1+arrsize); std::vector<std::string> vec2(arr2, arr2+arrsize); std::vector<std::string>::iterator l_it1vec1; std::vector<std::string>::iterator l_it = vec1.end(); l_it = find(vec1.begin(), vec1.end(), std::string("blah1")); if(l_it != vec1.end()) { size_t l_pos = l_it - vec1.begin(); printf("found %s, pos=%u val: %s\n", l_it->c_str(),l_pos, vec2[l_pos].c_str()); } }
now thought should possible put both directly map arr1 key , arr2 value. tried ways didn't succeed.
void fillmaptest() { const int arrsize = 3; const char *arr1[arrsize] = {"blah1", "wibble1", "shrug1"}; const char *arr2[arrsize] = {"blah2", "wibble2", "shrug2"}; std::map<std::string,std::string> map1;//(pair(arr1,arr1), pair(arr1+arrsize,arr2+arrsize)); std::map<std::string,std::string>::iterator l_it1map1; //l_it1map1 = find(map1.begin(), map1.end(), std::string("blah1")); if(l_it1map1 != map1.end()) { printf("found key: %s, val: %s\n",l_it1map1->first.c_str(), l_it1map1->second.c_str()); } } int _tmain(int /*argc*/, _tchar* /*argv[]*/) { fillvectest(); fillmaptest(); return 0; }
i think commented lines in function "fillmaptest" need solved. constuctor , find don't work want.
please has stl expert idea?
the easiest way write this:
std::map<std::string, std::string> m { { "key1", "value1" }, { "key2", "value2" }, };
this requires compiler support initializer lists (a feature of c++11).
Comments
Post a Comment