gpt4 book ai didi

c++ - std::vector 的缓存行为

转载 作者:行者123 更新时间:2023-11-30 04:21:28 33 4
gpt4 key购买 nike

我的问题很简单,给定以下数据结构,std::vector<std::pair<int, std::unique_ptr<foo>>> ,如果我有以下内容:

auto it = std::find_if(begin(v), end(v), [&](std::pair<...> const& p){ return p.first == some_value; });

我是否可以期望指针指向的任何内容都不会纯粹为了查找操作而被提取(我不希望它被提取,稍后会根据需要预提取)到缓存中?或者这是无法确定的(如果是的话我会关闭这个问题..)

最佳答案

当“find”在 vector 中搜索时,它会查看 vector 中条目的值,并将其与您要搜索的内容相匹配。因此,它将使用 find 提供的任何“相等”函数,或者如果没有提供 find 函数,则使用“operator==”。

因为在这种情况下,您只是将对中的 int 值与您的预期值进行比较,unique_ptr<foo>不会被取消引用(因此 unique_ptr<foo> 指向的数据不会进入缓存)。

关于c++ - std::vector 的缓存行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14522166/

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