3

是否可以将 a 转换为 aboost::interprocess::stringstd::stringa const 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);

谢谢!

4

1 回答 1

3

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 字符串转换......

于 2010-09-17T02:33:33.300 回答