栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ string转const char 和const char *的方法分享

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ string转const char 和const char *的方法分享

(认知有限,如有不妥,还请告知,欢迎讨论)

 

#include
#include
#include

using std::string;
using std::cout;
using std::endl;

int main()
{
    const char* sample_s = "sample pkc";
    cout <<"The type of " << '"' << sample_s << '"' << " is " << typeid(sample_s).name() << "n"<< endl;

    string sample_str = "sample str";
    cout <<"The type of " << '"' << sample_str << '"' << " is " << typeid(sample_str).name() << "n"<< endl;

    const char* s1 = "beginning pkc";
    cout <<"The type of " << '"' << s1 << '"' << " is " << typeid(s1).name() << "n"<< endl;

    //const char* 转 string
    string str_1(s1);
    cout <<"The type of " << '"' << str_1 << '"' << " now is " << typeid(str_1).name() << "n"<< endl;

    string str_2 = "beginning string";
    cout <<"The type of " << '"' << str_2 << '"' << " is " << typeid(str_2).name() << "n"<< endl;

    // string 转 const char*
    const char* s2=str_2.c_str();
    cout <<"The type of " << '"' << s2 << '"' << " now is " << typeid(s2).name() << "n"<< endl;

    return 0;
}
输出结果:

case CCommandLineInfo::FileNew:

           if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))

                    OnFileNew();

           if (m_pMainWnd == NULL)

                    bResult = FALSE;

           break;

CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)

{

if (!pFrame->LoadFrame(m_nIDResource,……..)

           return NULL;

}


The type of "sample pkc" is PKc

The type of "sample str" is NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

The type of "beginning pkc" is PKc

The type of "beginning pkc" now is NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

The type of "beginning string" is NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

The type of "beginning string" now is PKc

=END=

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号