supman
V2EX  ›  问与答

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

  •  
  •   supman · Oct 5, 2015 · 2129 views
    This topic created in 4047 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
    wbingeek
    Oct 5, 2015
    1
    if(array[i].empty())
    htfy96
    htfy96
    Oct 5, 2015
    2
    数组默认初始化的时候 string 是都被默认构造的

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

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