c++ empty std::vector begin not equals to end

前端 未结 3 1830
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 16:33

Hi I have situation on windows 10, that declared empty class member variable vector, but this vector\'s begin()(first iterator) and end()(last iterator)

3条回答
  •  旧巷少年郎
    2021-01-29 17:28

    std::vector v;
    assert(v.begin() == v.end());
    

    This should work: begin and end compare equal.

    On the other hand

    auto b = v.begin();
    auto e = v.end();
    assert(&b == &e);
    

    is prohibited from working: the two iterators are different objects and must have different addresses.

    Compare the equivalent:

    int i = 42;
    int j = 42;
    assert(i == j);   // ok
    assert(&i == &j); // fail
    

提交回复
热议问题