std 库中有什么函数可以二分搜索向量并找到元素?

发布于 2024-07-11 12:03:51 字数 137 浏览 6 评论 0原文

有一个节点结构

struct Node{CString text, int id;};

我在排序向量中

。 我想知道算法中是否有一个函数可以对向量进行二分搜索并找到一个元素。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平安喜乐 2024-07-18 12:03:51

std::binary_search() 会告诉您容器中是否存在某个值。

std::lower_bound()/std::upper_bound() 将返回一个指向值的第一个/最后一个出现的迭代器。

您的对象需要实现 operator< 才能使这些算法发挥作用。

std::binary_search() will tell you if a value exists in the container.

std::lower_bound()/std::upper_bound() will return an iterator to the first/last occurrence of a value.

Your objects need to implement operator< for these algorithms to work.

似梦非梦 2024-07-18 12:03:51

是的,有一个名为“binary_search”的函数 std::binary_search

你给它第一个、最后一个以及一个值或谓词。

请参阅此处获取示例

将其与Martin York's 运算符 == 你应该没问题(或者你可以写一个谓词函子

Yes, there's a function called "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

断爱 2024-07-18 12:03:51

而不是排序向量
为什么不使用地图。 这是一个已排序的容器。 因此,通过 std::find() 对此进行的任何搜索都会自动具有与二分搜索相同的属性。

Rather than a sorted vector<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.

昨迟人 2024-07-18 12:03:51

使用 std::equal_range 查找排序向量中的元素范围。 std::equal_range 返回一个 std::pair 迭代器,为您提供元素向量中等于您提供的参数的范围。 如果范围为空,则您的项目不在向量中,并且范围的长度告诉您项目在向量中出现的次数。

下面是一个使用 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;
}

要使其与 struct Node 一起工作,您必须提供一个运算符 用于struct Node 或将比较器传递给std::equal_range。 您可以通过提供 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; });

Use std::equal_range to find a range of elements in a sorted vector. std::equal_range returns a std::pair of iterators, giving you a range in the vector of elements equal to the argument you provide. 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:

#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. You can do that by providing a lambda as an argument to std::equal_range to compare your structs.

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; });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文