0

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?

Community
  • 1
  • 1
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
  • 3
    Duplicate of [Convert std::string to const char* or char*](http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char) (this has nothing to do with `std::map` at all. – James McNellis Nov 11 '10 at 06:16
  • 2
    Yes there is a way (or there are ways), as per James' comment, but *why* do you want to populate the char-array? What's the real problem that you are trying to solve? – johnsyweb Nov 11 '10 at 06:24

2 Answers2

3

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();
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
0

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());
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94