How to convert an std::string to const char* or char* in C++?

文章推薦指數: 80 %
投票人數:10人

You can use the c_str() method of the string class to get a const char* with the string contents. example. Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Howtoconvertanstd::stringtoconstchar*orchar*inC++? C++ServerSideProgrammingProgramming Youcanusethec_str()methodofthestringclasstogetaconstchar*withthestringcontents. example#include usingnamespacestd; intmain(){   stringx("hello");   constchar*ccx=x.c_str();   cout< usingnamespacestd; intmain(){   stringx("hello");   //Allocatememory   char*ccx=newchar[s.length()+1];   //Copycontents   std::copy(s.begin(),s.end(),ccx)   cout<



請為這篇文章評分?