c++ - 在 C++ 中二进制搜索具有给定元素值的元组

标签 c++ c++11 tuples binary-search

我想在以下位置找到所有元组:

vector<tuple<int,int>> tuples; //already sorted

将下一个命题评估为“真”:

(get<0>(tuple) == val)

我正在尝试使用 equal_range 函数来获取范围:

equal_range (tuples.begin(), tuples.end(), val);

如何正确表达“val”以便在元组的第一个元素中进行值比较?

最佳答案

你可以像这样创建一个元组并忽略它的最后一个元素:

#include <tuple>
#include <algorithm>
#include <vector>
#include <functional>

int main()
{
    std::vector<std::tuple<int, int>> v = {{1,0}, {2,3}, {4,5}};

    const int val = 2;

    std::equal_range(v.cbegin(), v.cend(),
        std::make_tuple(std::cref(val), 0),
        [&val] (const auto& l, const auto& r) {
            return std::get<0>(l) < std::get<0>(r);
        });
}

关于c++ - 在 C++ 中二进制搜索具有给定元素值的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555428/

相关文章:

c++ - opengl重新编译显示列表

c++ - 连接到 postgresql 数据库的奇怪错误

multithreading - C++ 静态变量初始化和线程

c++ - 我认为 §7.3.1.2/1 中的示例不正确

C# 多重赋值

arrays - 将 Swift Array 的内容复制到 Struct embedded Tuple

c++ - 一个数组来保存 C++ 中的任何对象?

c++ - 静态初始化惨败 - 故意的

c++ - 编译器错误初始化 std::array of structs with clang

python - 在 pandas 中使用元组作为索引键时如何返回 "explicitly specify the categories order by passing in a categories argument"?