2010-09-15 470 views
3

是否可以將boost::interprocess::string轉換爲std::stringconst char*?喜歡的東西c_str() ...boost :: interprocess ::字符串轉換爲char *

例如爲:

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); 

謝謝!

回答

3

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_stringstring是一個模板實例,其中CharT模板參數是char。)

同時,文檔here

basic_string是 std :: basic_string的實現,準備用於 託管的內存段,如sh內存。它使用 矢量般的連續的存儲實現的,所以它有 快速的C字符串轉換 ...

+0

謝謝SCFrench。 c_str()是我第一次嘗試的,我不知道爲什麼它不起作用。現在沒事了。 – Pietro 2010-09-17 22:21:06