Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::binary_search()查询_C++_List_Indexing_Compare_Binary Search - Fatal编程技术网

C++ std::binary_search()查询

C++ std::binary_search()查询,c++,list,indexing,compare,binary-search,C++,List,Indexing,Compare,Binary Search,我目前正在考虑使用std::binary_search()(来自库)来确定列表中是否存在某个实例。在开始使用之前,我想知道它是如何工作的 我的理解是,它使用比较(对于用户定义的结构/类,需要访问用户定义的比较函数)来确定列表/向量中是否存在对象的实例。根据本网站(),使用的范围是: [first, last) 那么,它是否不包括last,因为它必须将last与last+1进行比较 此外,用户定义的比较函数的逻辑并不重要,只要它区分对象/类中的属性即可。对吗 例如,如果我的结构/类包含以下内容:

我目前正在考虑使用std::binary_search()(来自库)来确定列表中是否存在某个实例。在开始使用之前,我想知道它是如何工作的

我的理解是,它使用比较(对于用户定义的结构/类,需要访问用户定义的比较函数)来确定列表/向量中是否存在对象的实例。根据本网站(),使用的范围是:

[first, last)
那么,它是否不包括last,因为它必须将lastlast+1进行比较

此外,用户定义的比较函数的逻辑并不重要,只要它区分对象/类中的属性即可。对吗

例如,如果我的结构/类包含以下内容:

coord
{
    int X;
    int Y;
}

我必须确保我的比较函数不同(以某种方式,例如大于/小于比较)列表/向量中元素a和b的X和Y属性。

范围不包括作为常规库约定的最后一个元素,这意味着第一个和最后一个迭代器之间的距离等于范围中的元素数,并且可以使用以下方法在循环中测试范围:

while(first != last)
{
    // process stuff
    ++first;
}
必须对使用相同(可能是用户定义的)比较函数排序的排序数据执行
std::binary_搜索

该功能需要在两个元素之间建立一个小于的关系

struct coord
{
    int x;
    int y;
};

struct CoordComparator
{
    bool operator()(const coord& lhs, const coord& rhs) const
    {
        return lhs.x == rhs.x ? lhs.y < rhs.y : lhs.x < rhs.x;
    }
};

std::vector<coord> v { {1, 1}, {2, 1}, {2, 2}, {1, 2} };

std::sort(v.begin(), v.end(), CoordComparator());

if(std::binary_search(v.begin(), v.end(), coord{2, 1}, CoordComparator()))
{
    // element found in range
}
struct-coord
{
int x;
int-y;
};
结构坐标比较器
{
布尔运算符()
{
返回lhs.x==rhs.x?lhs.y

可以定义小于关系,以便报告较大的值小于较小的值,以提供反向排序的关系。

该范围不包括作为一般库约定的最后一个元素,这意味着第一个和最后一个迭代器之间的距离等于该范围以及该范围可通过以下方式在回路中进行测试:

while(first != last)
{
    // process stuff
    ++first;
}
必须对使用相同(可能是用户定义的)比较函数排序的排序数据执行
std::binary_搜索

该功能需要在两个元素之间建立一个小于的关系

struct coord
{
    int x;
    int y;
};

struct CoordComparator
{
    bool operator()(const coord& lhs, const coord& rhs) const
    {
        return lhs.x == rhs.x ? lhs.y < rhs.y : lhs.x < rhs.x;
    }
};

std::vector<coord> v { {1, 1}, {2, 1}, {2, 2}, {1, 2} };

std::sort(v.begin(), v.end(), CoordComparator());

if(std::binary_search(v.begin(), v.end(), coord{2, 1}, CoordComparator()))
{
    // element found in range
}
struct-coord
{
int x;
int-y;
};
结构坐标比较器
{
布尔运算符()
{
返回lhs.x==rhs.x?lhs.y
可以定义小于关系,以便报告较大的值小于较小的值,以提供反向排序的关系。

std::binary_search()是一种常见的二进制搜索算法,它最多执行log2(N)+1个元素的比较。(有关如何实现二进制搜索的更多信息,请检查此项)

所以它不包括last,因为它必须将last与last+1进行比较吗

不,原因只是为了方便使用。您可以通过以下方式调用该函数:

std::binary_search (v.begin(), v.end(), 42)
请注意,v.end()将迭代器返回到序列末尾之后的元素。因此,它不指向任何元素,因此不应在搜索中进行评估

此外,用户定义的比较函数的逻辑并不重要,只要它区分对象/类中的属性即可。对吗

它用于二进制搜索()的比较函数,以了解您正在查找的元素是否在当前正在测试的元素之前或之后。换句话说,compare函数必须能够比较两个元素,并在第一个元素“低于”第二个元素时返回(必须放在第二个元素之前的容器中)

对于Coord示例,您可以编写一个比较器函数,如:

struct lessThanKey
{
    inline bool operator() (const Coord& lhs, const Coord& rhs)
    {
        return (lhs.x < rhs.x) || ((lhs.x == rhs.x) && (lhs.y < rhs.y));
    }
};

std::binary_search(v.begin(), v.end(), Coord{42, 42}, lessThanKey());
struct lessThanKey
{
内联布尔运算符()
{
返回(lhs.x
std::binary_search()是作为一种常见的二进制搜索算法实现的,它最多执行log2(N)+1个元素的比较。(有关如何实现二进制搜索的更多信息,请检查此项)

所以它不包括last,因为它必须将last与last+1进行比较吗

不,原因只是为了方便使用。您可以通过以下方式调用该函数:

std::binary_search (v.begin(), v.end(), 42)
请注意,v.end()将迭代器返回到序列末尾之后的元素。因此,它不指向任何元素,因此不应在搜索中进行评估

此外,用户定义的比较函数的逻辑并不重要,只要它区分对象/类中的属性即可。对吗

它用于二进制搜索()的比较函数,以了解您正在查找的元素是否在当前正在测试的元素之前或之后。换句话说,compare函数必须能够比较两个元素,并在第一个元素“低于”第二个元素时返回(必须放在第二个元素之前的容器中)

对于Coord示例,您可以编写一个比较器函数,如:

struct lessThanKey
{
    inline bool operator() (const Coord& lhs, const Coord& rhs)
    {
        return (lhs.x < rhs.x) || ((lhs.x == rhs.x) && (lhs.y < rhs.y));
    }
};

std::binary_search(v.begin(), v.end(), Coord{42, 42}, lessThanKey());
struct lessThanKey
{
内联布尔运算符()
{
返回(lhs.x