boost::interprocess::string 转换为 char*

发布于 2024-09-24 05:10:41 字数 508 浏览 9 评论 0原文

是否可以将 boost::interprocess::string 转换为 std::stringconst char*?类似 c_str()...

例如:

boost::interprocess::string is = "Hello world";
const char* ps = is.c_str();    // something similar
printf("%s", ps);

我什至可以在非共享内存块中获取字符串的副本。

例如:

boost::interprocess::string is = "Hello world";
const char cs[100];
strcpy(cs, is.c_str());    // something similar
printf("%s", cs);

谢谢!

Is it possible to convert a boost::interprocess::string to an std::string or to a const char*? Something like c_str()...

E.g.:

boost::interprocess::string is = "Hello world";
const char* ps = is.c_str();    // something similar
printf("%s", ps);

I could even get a copy of the string in a non-shared memory block.

E.g.:

boost::interprocess::string is = "Hello world";
const char cs[100];
strcpy(cs, is.c_str());    // something similar
printf("%s", cs);

Thank you!

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

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

发布评论

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

评论(1

病女 2024-10-01 05:10:41

boost::interprocess::string 有一个标准的 c_str() 方法。我在此处找到了以下内容:

//! <b>Returns</b>: Returns a pointer to a null-terminated array of characters 
//!   representing the string's contents. For any string s it is guaranteed 
//!   that the first s.size() characters in the array pointed to by s.c_str() 
//!   are equal to the character in s, and that s.c_str()[s.size()] is a null 
//!   character. Note, however, that it not necessarily the first null character. 
//!   Characters within a string are permitted to be null. 
const CharT* c_str() const 
{  return containers_detail::get_pointer(this->priv_addr()); }

(这是针对 basic_string 的。string 是一个模板实例化,其中 CharT 模板参数是 char

) ,文档此处

basic_string 是以下的实现
std::basic_string 准备用于
托管内存段,如共享内存段
记忆。它是使用一个
类似向量的连续存储,所以
具有快速的 C 字符串转换
...

boost::interprocess::string has a standard c_str() method. I found the following here:

//! <b>Returns</b>: Returns a pointer to a null-terminated array of characters 
//!   representing the string's contents. For any string s it is guaranteed 
//!   that the first s.size() characters in the array pointed to by s.c_str() 
//!   are equal to the character in s, and that s.c_str()[s.size()] is a null 
//!   character. Note, however, that it not necessarily the first null character. 
//!   Characters within a string are permitted to be null. 
const CharT* c_str() const 
{  return containers_detail::get_pointer(this->priv_addr()); }

(That's for the basic_string. string is a template instantiation in which the CharT template parameter is char.)

Also, the documentation here says

basic_string is the implementation of
std::basic_string ready to be used in
managed memory segments like shared
memory. It's implemented using a
vector-like contiguous storage, so it
has fast c string conversion
...

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