开发者

What is the most efficient way to convert STL string array to const char* array?

开发者 https://www.devze.com 2023-01-06 17:03 出处:网络
We have: std::string string_array[2]; string_array[0] = \"some data\"; string_array[1] = \"some more data\";

We have:

 std::string string_array[2];

 string_array[0] = "some data";

 string_array[1] = "some more data";

 char* cstring_array[2];

What is the most efficient way to copy data from string_array to cstring_array? Or pass string_array to the function, needed "const char* cstring_array[]"?开发者_Python百科


A function that takes a char const * is only written to accept a single string at a time, not an array of strings. To pass an std::string to such a function, just call the function with your_string.c_str() as the parameter.

Edit: for a function that takes an array of strings, the obvious choice (at least to me) would be to write a minimal front-end that lets you pass a vector<std::string>:

// The pre-existing function we want to call.
void func(char const *strings[], size_t num) { 
    for (size_t i=0;i<num; i++)
        std::cout << strings[i] << "\n";
}

// our overload that takes a vector<string>:
void func(std::vector<std::string> const &strings) { 
    std::vector<char const *> proxies(strings.size());

    for (int i=0; i<proxies.size(); i++)
        proxies[i] = strings[i].c_str();
    func(&proxies[0], proxies.size());
}


Use string::c_str to get const pointer to a char.


Simply use:
string_array[0].c_str()
string_array[1].c_str()

See c_str():

const charT* c_str() const basic_string Returns a pointer to a null-terminated array of characters representing the string's contents.


You can access a c-string version by passing string_array[1].c_str() to whatever function requires the string.

Be aware, however, that the pointer returned is dependent on the stl::string! If you take the pointer and then free the string, you will have an invalid pointer


Two different questions, really. To pass to a function simply call c_str(). To copy over simply call strncpy() and remember what I said about passing to functions.

Keep in mind that std::string can contain '\0' without actually terminating. Under such conditions you'll only get part of the string. If you're trying to copy such data you'll need to use memcpy() instead.


It looks like you're having some trouble with arrays. As Brian pointed out, array indices start at 0 not 1. Thus your two strings are string_array[0] and string_array[1], not string_array[1] and string_array[2].

Also, if your function takes a parameter const char* cstring_array, then your variable char* cstring_array[2] is probably not what you want. If you were to pass it in as is, you would essentially be passing in a char**, not a char*, as you can consider the name of an array to be equivalent to a pointer to its first element.

As you've presented your question, you could do something like the following (correcting for the indexing error, and assuming the function you mentioned is called myFunction):

...
cstring_array[0] = string_array[0].c_str( );
cstring_array[1] = string_array[1].c_str( );

myFunction (cstring_array[0]);
myFunction (cstring_array[1]);
...

However, a simpler equivalent that doesn't require cstring_array at all is:

...
myFunction (string_array[0].c_str( ));
myFunction (string_array[1].c_str( ));
...

Or, if your intention was to pass a single char array containing both strings (which is a possibility from my interpretation of the question), then what you really mean you want to do is you want to produce a single string being the concatenation of your original two:

...
std::string str = string_array[0] + string_array[1];

myFunction (str.c_str( ));
...

Hope that helps!

EDIT: given the comments that have appeared since I wrote this, I'd say the first example is close to what you need. I'd do the following (where N is the number of entries in either array):

...
for (int i = 0; i < N; i++)
    cstring_array[i] = string_array[i].c_str( );

myFunction (cstring_array);
...

This is the only approach possible. There is no standard function (AFAIK) that, given an array of string will give an array of char*. I suspect the reasons are that: - It's not something that needs to be done often, so there's little call for such a function to be widely available. - If there were such a function, it would most likely be very similar to the above anyway, in which case it's so trivial as to be unnecessary.

Basically, to answer your question, I don't think there is any more efficient method, and even if there were a standard library function to do this it would use this approach anyway.

0

精彩评论

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

关注公众号