M
Matt Garman
What is the "best" way to copy a vector of strings to an array of
character strings? By "best", I mean most elegantly/tersely written,
but without any sacrifice in performance.
I'm writing an application using C++ and the STL for handling my data.
Unfortunately, I must interact with a (vanilla) C API. I use vectors of
strings (for simplicity and less memory hassle), but the function calls
for this API require arrays of character strings (char**).
Below is some code to copy a vector of strings to an array of character
strings. It works (based on minimal testing anyway
. I'm just
curious if there is a "better" way to do it.
Thanks,
Matt
email at: http://raw-sewage.net/index.php?file=email
// this code is public domain
// compiles with the GNU C++ compiler (g++) v3.2.3
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv)
{
// handy typedefs just to save some typing
typedef std::vector<std::string> strvec_t;
typedef std::vector<std::string>::iterator strveciter_t;
strvec_t strvec;
// fill the string vector with some sample strings
strvec.push_back("Hello"); strvec.push_back(", ");
strvec.push_back("world"); strvec.push_back("... ");
strvec.push_back("How "); strvec.push_back("are ");
strvec.push_back("you "); strvec.push_back("today");
strvec.push_back("?");
// print out the strings (just for sanity)
for (strveciter_t ii=strvec.begin(); ii!=strvec.end(); ++ii)
cout << *ii;
cout << endl;
// allocate memory for an array of character strings
char** cstr = new char*[strvec.size()];
// for each string, allocate memory in the character array and copy
for (unsigned long i=0; i<strvec.size(); i++) {
cstr = new char[strvec.size()+1];
strncpy(cstr, strvec.c_str(), strvec.size());
}
// print out the newly copied strings
for (unsigned long i=0; i<strvec.size(); i++) cout << cstr;
cout << endl;
// free dynamic memory
for (unsigned long i=0; i<strvec.size(); i++) delete[] cstr;
delete[] cstr;
return 0;
}
character strings? By "best", I mean most elegantly/tersely written,
but without any sacrifice in performance.
I'm writing an application using C++ and the STL for handling my data.
Unfortunately, I must interact with a (vanilla) C API. I use vectors of
strings (for simplicity and less memory hassle), but the function calls
for this API require arrays of character strings (char**).
Below is some code to copy a vector of strings to an array of character
strings. It works (based on minimal testing anyway
curious if there is a "better" way to do it.
Thanks,
Matt
email at: http://raw-sewage.net/index.php?file=email
// this code is public domain
// compiles with the GNU C++ compiler (g++) v3.2.3
#include <string>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv)
{
// handy typedefs just to save some typing
typedef std::vector<std::string> strvec_t;
typedef std::vector<std::string>::iterator strveciter_t;
strvec_t strvec;
// fill the string vector with some sample strings
strvec.push_back("Hello"); strvec.push_back(", ");
strvec.push_back("world"); strvec.push_back("... ");
strvec.push_back("How "); strvec.push_back("are ");
strvec.push_back("you "); strvec.push_back("today");
strvec.push_back("?");
// print out the strings (just for sanity)
for (strveciter_t ii=strvec.begin(); ii!=strvec.end(); ++ii)
cout << *ii;
cout << endl;
// allocate memory for an array of character strings
char** cstr = new char*[strvec.size()];
// for each string, allocate memory in the character array and copy
for (unsigned long i=0; i<strvec.size(); i++) {
cstr = new char[strvec.size()+1];
strncpy(cstr, strvec.c_str(), strvec.size());
}
// print out the newly copied strings
for (unsigned long i=0; i<strvec.size(); i++) cout << cstr;
cout << endl;
// free dynamic memory
for (unsigned long i=0; i<strvec.size(); i++) delete[] cstr;
delete[] cstr;
return 0;
}