STL map and c++ [duplicate]
Possible Duplicate:
Convert std::string to const char* or char*
is there any to get the string back from the stl map and into a char array??
multimap<string, string> testcase;
testcase.insert(pair<string,string>("DB","something"));
for( i=testcase.begin(); i!=testcase.end(); ++i){
char cate[20] =(*i).first;
my code looks something like this... hw can i save (*i).first(or second for that matter) into a character array?
Assuming you know the size of the string, using what appears to be your original intention:
char cate[20];
assert(i->first.size() < sizeof(cate));
strcpy(i->first.c_str());
However if you want to take a copy of the string you want:
string cate(i->first);
Or C-style:
char *cate(strdup(i->first.c_str()));
Lastly to access the string with a "C" pointer:
char const *cate = i->first.c_str();
all i had to do was this.. strcpy never hit me. Thanks matt.
char cate[20];
char server[30];
strcpy(cate,i->first.c_str());
strcpy(server,i->first.c_str());
精彩评论