将 STL 字符串数组转换为 const char* 数组的最有效方法是什么?

发布于 2024-09-09 01:36:48 字数 287 浏览 2 评论 0原文

我们有:

 std::string string_array[2];

 string_array[0] = "some data";

 string_array[1] = "some more data";

 char* cstring_array[2];

将数据从 string_array 复制到 cstring_array 的最有效方法是什么?或者将 string_array 传递给函数,需要“const char* cstring_array[]”?

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[]"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你没皮卡萌 2024-09-16 01:36:48

采用 char const * 的函数只能一次接受一个字符串,而不是字符串数组。要将 std::string 传递给此类函数,只需使用 your_string.c_str() 作为参数调用该函数即可。

编辑:对于采用字符串数组的函数,明显的选择(至少对我来说)是编写一个最小的前端,让您传递 vector

// 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());
}

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());
}
梦途 2024-09-16 01:36:48

使用 string::c_str 获取 const指向字符的指针。

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

煮茶煮酒煮时光 2024-09-16 01:36:48

只需使用:
string_array[0].c_str()
string_array[1].c_str()

请参阅c_str()< /a>:

const charT* c_str()
const basic_string 返回一个指针
到一个以 null 结尾的数组
代表字符串的字符
内容。

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.

森林散布 2024-09-16 01:36:48

您可以通过将 string_array[1].c_str() 传递给任何需要该字符串的函数来访问 c 字符串版本。

但请注意,返回的指针取决于 stl::string!如果你获取指针然后释放字符串,你将得到一个无效的指针

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

南烟 2024-09-16 01:36:48

确实是两个不同的问题。要传递给函数,只需调用c_str()。要复制,只需调用 strncpy() 并记住我所说的有关传递给函数的内容。

请记住,std::string 可以包含 '\0' 而不会实际终止。在这种情况下,您只能获得字符串的一部分。如果您尝试复制此类数据,则需要使用 memcpy()

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.

戈亓 2024-09-16 01:36:48

看来您在使用数组时遇到了一些问题。正如 Brian 指出的,数组索引从 0 而不是 1 开始。因此,您的两个字符串是 string_array[0] 和 string_array[1],<强>不是 string_array[1]string_array[2]

另外,如果您的函数采用参数 const char* cstring_array,那么您的变量 char* cstring_array[2] 可能不是您想要的。如果您要按原样传递它,那么您实际上会传递一个 char**,而不是 char*,因为您可以将数组的名称视为相当于指向其第一个元素的指针

当您提出问题时,您可以执行以下操作(纠正索引错误,并假设您提到的函数名为 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]);
...

但是,根本不需要 cstring_array 的更简单的等价物是:

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

或者,如果您的意图是传递包含两个字符串的单个 char 数组(根据我的解释,这是可能的)问题),那么您真正想要做的是您想要生成一个字符串,将原来的两个字符串连接起来:

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

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

希望有帮助!

编辑:考虑到自我写这篇文章以来出现的评论,我想说第一个示例接近您所需要的。我会执行以下操作(其中 N 是任一数组中的条目数):

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

myFunction (cstring_array);
...

这是唯一可能的方法。没有标准函数(据我所知),给定一个 string 数组将给出一个 char* 数组。我怀疑原因是:
- 这不是需要经常执行的事情,因此很少需要广泛使用这样的功能。
- 如果有这样的功能,它很可能与上面的非常相似,在这种情况下,它是如此微不足道,以至于没有必要。

基本上,为了回答你的问题,我不认为有任何更有效的方法,即使有一个标准库函数可以做到这一点,它无论如何都会使用这种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文