简体   繁体   English

C ++ 11:map :: lower_bound对于Linux中的2个或更少元素无法正常工作

[英]C++11 : map::lower_bound doesn't work correctly for 2 or less elements in Linux

If I run following C++11 example in Linux (Debian 7, GCC 4.8.2, Eclipse CDT), the while cycle is infinite. 如果我在Linux(Debian 7,GCC 4.8.2,Eclipse CDT)中遵循以下C ++ 11示例运行,则while周期是无限的。 First loop is correct. 第一循环是正确的。 Iterator is decremented by 1 and it references to the first map element. 迭代器递减1,它引用第一个map元素。 But second and other loops are incorrect. 但是第二个循环和其他循环是不正确的。 Decrement operator doesn't decrement iterator. 递减运算符不递减迭代器。 It still references to the first element. 它仍然引用第一个元素。 If I remove comment (in map initialization), while cycle will stop. 如果删除注释(在地图初始化中),则循环将停止。 Could you please tell me, what I did wrong? 你能告诉我,我做错了什么吗? Thank you very much for every comment. 非常感谢您的每条评论。

#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;
}

Note: This code works correct under Windows platform (Visual studio 2013 Express). 注意:此代码在Windows平台(Visual Studio 2013 Express)下正常工作。

You pass a begin() iterator to this line: 您将begin()迭代器传递给此行:

while(--it != buff.end())

And --begin() yields undefined behaviour. --begin()产生不确定的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM