简体   繁体   English

std库中的哪个函数可以二元搜索向量并找到一个元素?

[英]What function in the std library is there to binary search a vector and find an element?

I've got a node struct 我有一个节点结构

struct Node{CString text, int id;};

in a sorted vector. 在一个有序的矢量。

I'm wondering if there's a function in algorithm that will do a binary search of the vector and find an element. 我想知道算法中是否有一个函数可以对向量进行二元搜索并找到一个元素。

std::binary_search() will tell you if a value exists in the container. std::binary_search()将告诉您容器中是否存在值。

std::lower_bound()/std::upper_bound() will return an iterator to the first/last occurrence of a value. std::lower_bound()/std::upper_bound()将返回一个值的第一个/最后一个迭代器。

Your objects need to implement operator< for these algorithms to work. 您的对象需要实现operator<才能使这些算法正常工作。

Yes, there's a function called "binary_search" std::binary_search 是的,有一个名为“binary_search”的函数std :: binary_search

You give it first, last and a value or a predicate. 你先给它,最后给它一个值或一个谓词。

See here for a sample 请看这里的样本

Combine that with Martin York's operator== and you should be OK (alternatively you can write a predicate functor 将它与Martin York的运算符==结合起来你应该没问题(或者你可以写一个谓词仿函数

Use std::equal_range to find a range of elements in a sorted vector. 使用std::equal_range查找已排序向量中的一系列元素。 std::equal_range returns a std::pair of iterators, giving you a range in the vector of elements equal to the argument you provide. std::equal_range返回一个std::pair迭代器,在向量中给出一个等于你提供的参数的元素范围。 If the range is empty, then your item isn't in the vector, and the length of the range tells you how many times your item appears in the vector. 如果范围为空,则您的项目不在向量中,并且范围的长度会告诉您项目在向量中出现的次数。

Here is an example using ints instead of struct Node : 以下是使用int而不是struct Node的示例:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

int main(int argc, const char * argv[])
{
    std::vector<int> sorted = { 1, 2, 2, 5, 10 };

    auto range = std::equal_range(sorted.begin(), sorted.end(), 20);
    // Outputs "5 5"
    std::cout << std::distance(sorted.begin(), range.first) << ' '
              << std::distance(sorted.begin(), range.second) << '\n';

    range = std::equal_range(sorted.begin(), sorted.end(), 5);
    // Outputs "3 4"
    std::cout << std::distance(sorted.begin(), range.first) << ' '
              << std::distance(sorted.begin(), range.second) << '\n';

    range = std::equal_range(sorted.begin(), sorted.end(), -1);
    // Outputs "0 0"
    std::cout << std::distance(sorted.begin(), range.first) << ' '
              << std::distance(sorted.begin(), range.second) << '\n';

    return 0;
}

To make this work with struct Node you'll either have to provide an operator < for struct Node or pass in a comparator to std::equal_range . 要使用struct Node你必须提供一个operator < for struct Node或者将一个比较器传递给std::equal_range You can do that by providing a lambda as an argument to std::equal_range to compare your structs. 你可以通过提供一个lambda作为std::equal_range的参数来比较你的结构。

std::vector<Node> nodes = { Node{"hello", 5}, Node{"goodbye", 6} };
Node searchForMe { "goodbye", 6 };
auto range = std::equal_range(nodes.begin(), nodes.end(), searchForMe,
                               [](Node lhs, Node rhs) { return lhs.id < rhs.id; });

Rather than a sorted vector<Node> 而不是排序的矢量<Node>
Why not use a map. 为什么不使用地图。 This is a sorted container. 这是一个已排序的容器。 So any search done on this via std::find() automatically has the same properties as a binary search. 因此,通过std :: find()对此进行的任何搜索都会自动具有与二进制搜索相同的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM