开发者

STL map and c++ [duplicate]

This question already has answers here: 开发者_运维问答 Closed 12 years ago.

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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜