gpt4 book ai didi

c++ - std:find()用于排序与未排序

转载 作者:行者123 更新时间:2023-12-01 15:04:51 27 4
gpt4 key购买 nike

考虑两个整数 vector v1v2,其中v1已排序,而v2未排序。使用find()在这两个 vector 中搜索元素的时间复杂度是多少?

最佳答案

std::find() 执行linear search,也就是说,它逐个元素地移动直到找到所需的值。不管集合是否排序,它都是线性的。
如果您有排序的 vector ,则可能要考虑用binary search执行 std::lower_bound() 。 vector 大小将是对数。

关于c++ - std:find()用于排序与未排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63740250/

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