为什么在 C++ 中找不到向量 [英] why there is no find for vector in C++

查看:28
本文介绍了为什么在 C++ 中找不到向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么选择?

我应该自己写吗?

推荐答案

std::find() 算法,它在迭代器范围内执行线性搜索,例如,

There is the std::find() algorithm, which performs a linear search over an iterator range, e.g.,

std::vector<int> v;

// Finds the first element in the vector that has the value 42:
// If there is no such value, it == v.end()
std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42);

如果您的向量已排序,则可以使用std::binary_search() 来测试向量中是否存在某个值,以及std::equal_range()获取向量中具有该值的元素范围的开始和结束迭代器.

If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin and end iterators to the range of elements in the vector that have that value.

这篇关于为什么在 C++ 中找不到向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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