2010-09-15 4 views
3

Est-il possible de convertir un boost::interprocess::string en un std::string ou un const char*? Quelque chose comme c_str() ...boost :: interprocess :: string conversion en char *

.: par exemple

boost::interprocess::string is = "Hello world"; 
const char* ps = is.c_str(); // something similar 
printf("%s", ps); 

je pourrais même obtenir une copie de la chaîne dans un bloc de mémoire non partagée.

.: par exemple

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

Merci!

Répondre

3

boost :: interprocess :: string a une méthode c_str() standard. J'ai trouvé ce qui suit 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()); } 

(C'est pour le basic_stringstring est une instanciation de modèle dans lequel le paramètre de modèle CharT est char..)

En outre, la documentation here dit

basic_string est l'implémentation de std :: basic_string prête à être utilisée dans segments de mémoire gérés comme sh mémoire. Il est mis en œuvre au moyen d'un stockage contigu vecteur semblable à , donc il a string conversion rapide c ...

+0

Merci SCFrench. c_str() est ce que j'ai essayé au début, et je ne sais pas pourquoi cela n'a pas fonctionné. Maintenant tout va bien. – Pietro

Questions connexes