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

标签 c++ linux gcc c++11 debian

如果我在 Linux(Debian 7、GCC 4.8.2、Eclipse CDT)中运行以下 C++11 示例,则 while 循环是无限的。第一个循环是正确的。迭代器减 1,它引用第一个 map 元素。但是第二个和其他循环是不正确的。递减运算符不会递减迭代器。它仍然引用第一个元素。 如果我删除评论(在 map 初始化中),while 循环将停止。 你能告诉我,我做错了什么吗? 非常感谢您的每一条评论。

#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() 产生未定义的行为。

关于C++11:map::lower_bound 在 Linux 中对于 2 个或更少的元素不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864160/

相关文章:

gcc - 如何禁用警告: null character(s) preserved in literal?

c++ - 如何制作接受任何原始类型并在 C++ 中转换为 int 的构造函数?

linux - 我的 zsh 在终端(linux)中出现错误?

gcc - 宏 asm 内联错误

c - 如何在 Linux 中从用户空间找到变量的物理地址?

linux - 奇怪的 "sh"行为

gcc - assembly 中的位测试 (BT)

C++,对指向的 map 容器使用迭代器时出错。映射/设置迭代器不可递增

c++ - 运行函数时出错

c++ - C++代码中的 “Undefined reference to operator<<”错误