如果地图擦除范围中的第一个和最后一个相等,是否将删除该元素?
If first and last in a map erase range are equal will the element be removed or not?
根据this:
Iterators specifying a range within the map container to be removed:
[first,last). i.e., the range includes all the elements between first
and last, including the element pointed by first but not the one
pointed by last.
这是模棱两可的,因为它没有解决 first 和 last 相等的情况。如果 first 和 last 相等,元素是否被删除?这是相关代码的表示:
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), it);
If first and last are equal will the element be removed or not?
当 first 和 last 相等时表示空范围,不会删除任何元素。声明为 std::vector::erase()
documentation:
The iterator first does not need to be dereferenceable if first==last: erasing an empty range is a no-op.
std::map::erase()
的语义应该相同。
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), it);
代码表示 - 删除从开始到 it
的所有内容,但不包括 it
。如果 it
等于 m_MyMap.begin()
,则不会删除任何内容。
如果你想包含键等于 ack
的元素,你需要提前 it
:
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), std::next(it));
根据this:
Iterators specifying a range within the map container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
这是模棱两可的,因为它没有解决 first 和 last 相等的情况。如果 first 和 last 相等,元素是否被删除?这是相关代码的表示:
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), it);
If first and last are equal will the element be removed or not?
当 first 和 last 相等时表示空范围,不会删除任何元素。声明为 std::vector::erase()
documentation:
The iterator first does not need to be dereferenceable if first==last: erasing an empty range is a no-op.
std::map::erase()
的语义应该相同。
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), it);
代码表示 - 删除从开始到 it
的所有内容,但不包括 it
。如果 it
等于 m_MyMap.begin()
,则不会删除任何内容。
如果你想包含键等于 ack
的元素,你需要提前 it
:
map<uint16_t, boost::shared_ptr<SPacket> >::iterator it = m_MyMap.find(ack);
if (it != m_MyMap.end()) m_MyMap.erase(m_MyMap.begin(), std::next(it));