【问题标题】:boost::interprocess::string conversion to char*boost::interprocess::string 转换为 char*
【发布时间】:2010-09-15 15:53:27
【问题描述】:

是否可以将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);

谢谢!

【问题讨论】:

    标签: string boost char interprocess


    【解决方案1】:

    boost::interprocess::string 有一个标准的 c_str() 方法。我找到了以下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()); }
    

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

    另外,文档here

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

    【讨论】:

    • 谢谢 SCFrench。 c_str() 是我一开始尝试的,我不知道为什么它不起作用。现在没事了。
    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2011-09-17
    • 2011-03-28
    • 1970-01-01
    • 2011-01-26
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多