map/set iterators incompatible

1. 这是自己遇到的问题,仅做错误记录使用,以前没有做记录的习惯。

2. map/set iterators incompatible说人话就是迭代器不兼容

	- 如果还不像人话,那就可以理解为:不是同一个容器对象的迭代器
	- 我举例代码中的map1,map2是两个容器,不能判断容器位置是否相等

3. 源码

std::map<int, int> map1;
std::map<int, int> map2;

void func() {
    if (map1.begin() != map2.end())
        cout << "???" << endl;
}

4. 分析原因

5.1 崩溃位置

image

	_NODISCARD bool operator==(const _Tree_const_iterator& _Right) const
		{	// test for iterator equality
 #if _ITERATOR_DEBUG_LEVEL != 0
		_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "map/set iterators incompatible");
 #endif /* _ITERATOR_DEBUG_LEVEL != 0 */

		return (this->_Ptr == _Right._Ptr);
		}
		

5.2 代码修正

std::map<int, int> map1;
std::map<int, int> map2;

void func() {
    if (map1.begin() != map1.end())
	// if (map2.begin() != map2.end())
        cout << "???" << endl;
}
posted @ 2022-03-22 18:15  flxx  阅读(633)  评论(0编辑  收藏  举报