STD :: MAP-减少迭代器给出奇怪的结果?

发布于 2025-01-22 08:40:34 字数 624 浏览 1 评论 0原文

似乎无法解决这个问题。简单的示例如下:

#include <iostream>
#include <map>

int main() {

    std::map<uint32_t, char> m;
    
    m[1] = 'b';
    m[3] = 'd';
    m[5] = 'f';
    
    std::map<uint32_t, char>::iterator i = m.lower_bound('d');
    
    std::cout << "First: " << i->first << std::endl;
    
    // Decrement the iterator
    i--;
    
    // Expect to get 1, but get 5?
    std::cout << "Second: " << i->first << std::endl;

    return 0;
}

输出是:

First: 3
Second: 5

为什么我在这里获得5?我认为减少迭代器会导致它指向键1

Can't seem to work this out. Simple example as follows:

#include <iostream>
#include <map>

int main() {

    std::map<uint32_t, char> m;
    
    m[1] = 'b';
    m[3] = 'd';
    m[5] = 'f';
    
    std::map<uint32_t, char>::iterator i = m.lower_bound('d');
    
    std::cout << "First: " << i->first << std::endl;
    
    // Decrement the iterator
    i--;
    
    // Expect to get 1, but get 5?
    std::cout << "Second: " << i->first << std::endl;

    return 0;
}

The output is:

First: 3
Second: 5

Why do I get 5 here? I thought decrementing the iterator would result in it pointing at key 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界和平 2025-01-29 08:40:34

此调用

std::map<uint32_t, char>::iterator i = m.lower_bound('d');

返回迭代器m.end()。因此,将迭代剂删除

std::cout << "First: " << i->first << std::endl;

会导致不确定的行为。

成员函数lower_bound期望一个参数指定密钥而不是值。

考虑以下演示计划。

#include <iostream>
#include <iomanip>
#include <map>
#include <cstdint>

int main()
{
    std::map<uint32_t, char> m;

    m[1] = 'b';
    m[3] = 'd';
    m[5] = 'f';

    std::map<uint32_t, char>::iterator i = m.lower_bound( 'd' );

    std::cout << "i == m.end() is " << std::boolalpha << ( i == m.end() ) << '\n';
}

相反,程序输出是

i == m.end() is true

后,您可以写入。

std::map<uint32_t, char>::iterator i = m.lower_bound( 5 );

在此调用之后降低迭代器后

std::map<uint32_t, char>::iterator i = m.lower_bound('d');

指向地图的最后一个元素

This call

std::map<uint32_t, char>::iterator i = m.lower_bound('d');

returns the iterator m.end(). So dereferencing the iterator

std::cout << "First: " << i->first << std::endl;

results in undefined behavior.

The member function lower_bound expects an argument that specifies a key not value.

Consider the following demonstration program.

#include <iostream>
#include <iomanip>
#include <map>
#include <cstdint>

int main()
{
    std::map<uint32_t, char> m;

    m[1] = 'b';
    m[3] = 'd';
    m[5] = 'f';

    std::map<uint32_t, char>::iterator i = m.lower_bound( 'd' );

    std::cout << "i == m.end() is " << std::boolalpha << ( i == m.end() ) << '\n';
}

The program output is

i == m.end() is true

Instead you could write for example

std::map<uint32_t, char>::iterator i = m.lower_bound( 5 );

After decrementing the iterator after this call

std::map<uint32_t, char>::iterator i = m.lower_bound('d');

it points to the last element of the map.

陪你搞怪i 2025-01-29 08:40:34

lower_bound将作为输入密钥而不是值为输入。这将按照您的期望做到:

std::map<uint32_t, char>::iterator i = m.lower_bound(3);

使用lower_bound,您最终找到end()并迭代一个。

lower_bound takes as input the key, not the value. This would do as you expect:

std::map<uint32_t, char>::iterator i = m.lower_bound(3);

With the lower_bound you are using, you end up finding end() and iterating back one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文