首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::interprocess::string转换为char*

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

Stack Overflow用户
提问于 2010-09-15 23:53:28
回答 1查看 838关注 0票数 3

是否可以将boost::interprocess::string转换为std::stringconst char*?像c_str()这样的东西。

例如:

代码语言:javascript
复制
boost::interprocess::string is = "Hello world";
const char* ps = is.c_str();    // something similar
printf("%s", ps);

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

例如:

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

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-17 10:33:33

string有一个标准的c_str()方法。我找到了下面的here

代码语言:javascript
复制
//! <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。)

此外,documentation here表示

basic_string是std::basic_string的实现,可以在共享内存等托管内存段中使用。它是使用类似向量的连续存储实现的,因此它具有快速的c字符串转换...

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3719460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档