Is there a better way in terms of performance to convert a map imap to char* array[] and integer array in C?
map<string, int> imap; // Code below will build the imap
....
....
....
char** carr;
int* iarr;
*carr = (char**)malloc(sizeof(char*)*imap.size());
*iarr = (int*)malloc(sizeof(int)*imap.size());
index = -1;
for(iter = imap.begin(); iter != imap.end(); ++iter)
{
(*carr)[++index] = strdup(iter->first.c_str());
(*iarr)[index] = iter->second;
}
-Kartlee
No, there isn’t. Just beautify your code a little. I guess a good compiler will optimise all the anomalies.
I would avoid calling c_str(), I would just create the array of STL string if possible, because that is the thing that uses up most time in your script.
Also side note: I myself place beautiful, understandable code before performance. Tidy your code and get rid of those ugly malocs. C++ supports NEW operator.