[英]C++11 : map::lower_bound doesn't work correctly for 2 or less elements in Linux
如果我在Linux(Debian 7,GCC 4.8.2,Eclipse CDT)中遵循以下C ++ 11示例運行,則while周期是無限的。 第一循環是正確的。 迭代器遞減1,它引用第一個map元素。 但是第二個循環和其他循環是不正確的。 遞減運算符不遞減迭代器。 它仍然引用第一個元素。 如果刪除注釋(在地圖初始化中),則循環將停止。 你能告訴我,我做錯了什么嗎? 非常感謝您的每條評論。
#include <iostream>
#include <map>
using namespace std;
int main() {
std::map<int, int> mymap = {{1, 100}, {2, 200}/*, {3, 300}*/};
auto it = mymap.lower_bound(2);
cout << "mymap key: " << it->first << endl;
while(--it != buff.end())
cout << "mymap key: " << it->first << endl;
return 0;
}
注意:此代碼在Windows平台(Visual Studio 2013 Express)下正常工作。
您將begin()
迭代器傳遞給此行:
while(--it != buff.end())
--begin()
產生不確定的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.