supman
V2EX  ›  问与答

cpp 判断数组的某一个位置 是不是空的 应该怎么写呢?

  •  
  •   supman · Oct 5, 2015 · 1929 views
    This topic created in 3872 days ago, the information mentioned may be changed or developed.

    if(array[i]==null)

    很不幸。。上面那个不对, 数组的类型是 string , 提示错误好像是说类型不匹配。 那么正确姿势应该是啥样呢?

    5 replies    2015-10-05 20:59:52 +08:00
    wbingeek
        1
    wbingeek  
       Oct 5, 2015
    if(array[i].empty())
    htfy96
        2
    htfy96  
       Oct 5, 2015
    数组默认初始化的时候 string 是都被默认构造的

    if (array[i]==std::string()) ...
    supman
        3
    supman  
    OP
       Oct 5, 2015
    @htfy96
    @wbingeek
    爱死二位了 谢谢你们啦
    thinkIn
        4
    thinkIn  
       Oct 5, 2015 via iPhone
    不是 null ,应是 nul
    htfy96
        5
    htfy96  
       Oct 5, 2015
    @supman 对于 string 类型 1 楼方法比较好,我的方法*可能*会发生重复构造 string 导致性能降低的情况(在循环中)。但是这个方法对大多数类型都能使用。

    当然,归根结底是因为当一个数组被定义的时候是默认填满的,而没有*空*这个概念。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5986 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 01:54 · PVG 09:54 · LAX 18:54 · JFK 21:54
    ♥ Do have faith in what you're doing.