V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenggiant
V2EX  ›  程序员

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

  •  
  •   chenggiant · 2014-03-12 16:19:48 +08:00 · 3457 次点击
    这是一个创建于 3690 天前的主题,其中的信息可能已经有所发展或是发生改变。
    const unsigned char Str[] = "s\n\sabcv";

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

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

    这个方法对不对呀?会有什么潜在的问题么?
    8 条回复    1970-01-01 08:00:00 +08:00
    chenggiant
        1
    chenggiant  
    OP
       2014-03-12 17:15:38 +08:00
    V2用C++的真心少呀...
    nybux
        2
    nybux  
       2014-03-12 17:16:05 +08:00   ❤️ 1
    对的
    没有
    jybox
        3
    jybox  
       2014-03-12 17:23:25 +08:00   ❤️ 1
    为什么要用 const unsigned char* 来表示字符串?
    如果没有特殊用途的话,应该用 const char*, 然后就可以直接用 std::string 的构造函数了啊。
    xdeng
        4
    xdeng  
       2014-03-12 17:33:47 +08:00   ❤️ 1
    const unsigned char Str[] = "s\n\sabcv"; 干嘛不用 const char *Str = "s\n\sabcv";
    dorentus
        5
    dorentus  
       2014-03-12 18:04:26 +08:00 via iPhone
    为什么要用字符数组来表示字符串…
    1423
        6
    1423  
       2014-03-12 18:08:38 +08:00
    可以直接用 const char* 初始化 string 啊,费这些劲干嘛。。
    icenan2
        7
    icenan2  
       2014-03-12 18:57:45 +08:00
    const unsigned char Str[] = "s\n\sabcv";
    这语句编译能通过吗
    nelson
        8
    nelson  
       2014-03-12 19:21:23 +08:00
    没\0没问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3493 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:47 · PVG 12:47 · LAX 21:47 · JFK 00:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.