insert_or_assign允许迭代器 [英] insert_or_assign is allowing iterator

查看:500
本文介绍了insert_or_assign允许迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

auto it = my_map.lower_bound(my_key);

以下断言给我错误:

static_assert(std::is_same<decltype(it), std::map<K, V>::const_iterator>::value, "Error");

以下一个是好的:

static_assert(std::is_same<decltype(it), std::map<K, V>::iterator>::value, "Error");

然后,编译器没有给我一个 const_iterator 。好。但是这里:

Then, the compiler is not giving me a const_iterator. Ok. But here:

my_map.insert_or_assign(it, my_key, some_val);

即使使用迭代器(不是<$ c) $ c> const_iterator ),该函数正常工作。但是,在链接中, insert_or_assign 签名,我只有 const_iterator 参数。我还在Visual Studio上搜索了 .h 文件,这些信息匹配。在GCC 7.2+和Visual Studio 2015上进行测试,所有内容都会编译并运行。

even with iterator (not const_iterator), the function is working. But, in this link, on insert_or_assign signatures, I only have const_iterator arguments. I also searched for the .h file on Visual Studio and this information matches. Tested on GCC 7.2+ and Visual Studio 2015, everything compiles and runs.

为什么要编译?为什么 insert_or_assign 接受 iterator

Why does it compile? Why insert_or_assign accepts iterator?

推荐答案

所有容器都需要提供可转换为 const_iterator 迭代器类型。请参阅 容器要求

All containers are required to provide an iterator type that is convertible to const_iterator. See the Container requirements table

X :: iterator 必须是任何满足前向迭代器要求的迭代器类别。 可转换为 X :: const_iterator

所以a const_iterator 正在通过调用中的 lower_bound 返回的迭代器构造到 insert_or_assign

So a const_iterator is being constructed from the iterator returned by lower_bound in the call to insert_or_assign.

这篇关于insert_or_assign允许迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆