You can use std::transform as:
std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
Which requires you to implement convert() as:
char *convert(const std::string & s)
{
char *pc = new char[s.size()+1];
std::strcpy(pc, s.c_str());
return pc;
}
Test code:
int main() {
std::vector<std::string> vs;
vs.push_back("std::string");
vs.push_back("std::vector<std::string>");
vs.push_back("char*");
vs.push_back("std::vector<char*>");
std::vector<char*> vc;
std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
for ( size_t i = 0 ; i < vc.size() ; i++ )
std::cout << vc[i] << std::endl;
for ( size_t i = 0 ; i < vc.size() ; i++ )
delete [] vc[i];
}
Output:
std::string
std::vector<std::string>
char*
std::vector<char*>
Online demo : http://ideone.com/U6QZ5
You can use &vc[0] wherever you need char**.
Note that since we're using new to allocate memory for each std::string (in convert function), we've to deallocate the memory at the end. This gives you flexibility to change the vector vs; you can push_back more strings to it, delete the existing one from vs, and vc (i.e vector<char*> will still be valid!
But if you don't want this flexibility, then you can use this convert function:
const char *convert(const std::string & s)
{
return s.c_str();
}
And you've to change std::vector<char*> to std::vector<const char*>.
Now after the transformation, if you change vs by inserting new strings, or by deleting the old ones from it, then all the char* in vc might become invalid. That is one important point. Another important point is that, you don't need to use delete vc[i] in your code anymore.
char*, notchar**as in your question. Which is it?fnamestransfered intoModelInitialize? (if so: how must it have been allocated?) Is the calling code meant todelete,freeor otherwise deallocate theModelreturned fromModelInitialize? (if so: how must it be deallocated?) Mustfnamesbe a null-terminated string? In what ways mayfnamesbe modified?