3

boost::interprocess::stringstd::stringまたはに変換することは可能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であるテンプレートのインスタンス化のためです。)CharTchar

また、ここのドキュメントには次のように書かれています

basic_string は std::basic_string の実装であり、共有メモリなどのマネージド メモリ セグメントですぐに使用できます。ベクトルのような連続ストレージを使用して実装されているため、高速な c 文字列変換が可能です...

于 2010-09-17T02:33:33.300 に答える