c++ - 为什么在 C++ 中找不到 vector

标签 c++ stl stdvector

还有什么选择?

我应该自己写吗?

最佳答案

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

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);

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

关于c++ - 为什么在 C++ 中找不到 vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994073/

相关文章:

c++ - 链表内存管理

c++ - 无法在 C++ 中使用 unique_ptr 返回派生类型

c++ - 什么时候应该使用 STL 算法而不是使用您自己的算法?

c++ - 如何洗牌 std::vector?

c++ - C/C++判断文件是否写完

C++ 线程安全的均匀分布随机数生成

c++ - 从函数返回 vector 以打印内容

c++ - Char 变量作为 Y/N 问题的输入

c++ - 二进制 '==' : no operator found error for vector C++

c++ - std::vector 作为原始数组