gpt4 book ai didi

c++ - 映射最大有效插入

转载 作者:太空宇宙 更新时间:2023-11-04 16:27:38 24 4
gpt4 key购买 nike

这是一些工作代码:

std::pair<typename std::map< time_t, T >::iterator, bool> ret;    
ret = prt_map->insert ( std::pair< time_t, T >( seconds , value ) );
if (ret.second == false) // exists
{
... update exising value
}

我想将它升级到最有效的插入代码,并给出插入需要从 --prt_map->end() 开始的提示;像这样的东西:

  prt_map->insert ( --prt_map->end(), std::pair< time_t, T >( seconds , value ) );

但是这个插入函数 prototupe 不会返回 std::pair 和 bool 来通知是否添加了值(检查是否存在并更新如果是...如何解决这个问题以具有更新现有值的能力?

最佳答案

a.insert(hint, t) 的后置条件是:

a contains an element whose key is the same as that of t. The size of a is incremented by either 1 or 0.

这应该是足够的信息来确定元素是否被插入。

关于c++ - 映射最大有效插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com