还有什么选择?
我应该自己写吗?
最佳答案
有 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/