• 请不要在回答技术问题时复制粘贴 AI 生成的内容
chenggiant
V2EX  ›  程序员

C++程序员们来解答下疑惑吧

  •  
  •   chenggiant · Mar 12, 2014 · 4008 views
    This topic created in 4473 days ago, the information mentioned may be changed or developed.
    const unsigned char Str[] = "s\n\sabcv";

    因为想用std::string自带的一些member function, 把Str按照下面的方法转化为string S_str:

    string S_str(reinterpret_cast<const char*>(Str));

    这个方法对不对呀?会有什么潜在的问题么?
    8 replies    1970-01-01 08:00:00 +08:00
    chenggiant
        1
    chenggiant  
    OP
       Mar 12, 2014
    V2用C++的真心少呀...
    nybux
        2
    nybux  
       Mar 12, 2014   ❤️ 1
    对的
    没有
    jybox
        3
    jybox  
       Mar 12, 2014   ❤️ 1
    为什么要用 const unsigned char* 来表示字符串?
    如果没有特殊用途的话,应该用 const char*, 然后就可以直接用 std::string 的构造函数了啊。
    46fo
        4
    46fo  
       Mar 12, 2014   ❤️ 1
    const unsigned char Str[] = "s\n\sabcv"; 干嘛不用 const char *Str = "s\n\sabcv";
    dorentus
        5
    dorentus  
       Mar 12, 2014 via iPhone
    为什么要用字符数组来表示字符串…
    slowman
        6
    slowman  
       Mar 12, 2014
    可以直接用 const char* 初始化 string 啊,费这些劲干嘛。。
    icenan2
        7
    icenan2  
       Mar 12, 2014
    const unsigned char Str[] = "s\n\sabcv";
    这语句编译能通过吗
    nelson
        8
    nelson  
       Mar 12, 2014
    没\0没问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1142 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 23:11 · PVG 07:11 · LAX 16:11 · JFK 19:11
    ♥ Do have faith in what you're doing.