Convert std::string to const char* in C++ - Techie Delight
文章推薦指數: 80 %
We can easily get a const char* from the std::string in constant time with the help of the string::c_str function. The returned pointer is backed by the ...
Skiptocontent
Thispostwilldiscusshowtoconvertastd::stringtoconstchar*inC++.Thereturnedpointershouldpointtoachararraycontainingthesamesequenceofcharactersaspresentinthestringobjectandanadditionalnullterminator(‘\0’character)attheend.
1.Usingstring::c_strfunction
Wecaneasilygetaconstchar*fromthestd::stringinconstanttimewiththehelpofthestring::c_strfunction.Thereturnedpointerisbackedbytheinternalarrayusedbythestringobject,andifthestringobjectismodified,thereturnedpointerwillalsobeinvalidated.
123456789101112
#include
延伸文章資訊
- 1Convert std::string to const char* in C++ - Techie Delight
We can easily get a const char* from the std::string in constant time with the help of the string...
- 2How can I convert const char* to string and then back to char
- 3C++之char*,const char*和string的相互转换 - 51CTO博客
C++之char*,const char*和string的相互转换,1.string转constchar*strings="abc";constchar*c_s=s.c_str();2.cons...
- 4Can we assign const char* to a string in cpp? - Stack Overflow
- 5The const keyword. - LIX-polytechnique