C++ 给出相同结果的下界上界

C++ 给出相同结果的下界上界,c++,arrays,algorithm,sorting,lower-bound,C++,Arrays,Algorithm,Sorting,Lower Bound,我试图在排序数组中找到最近的值,但上界和下界都给出了最高值 float abc[] = {1,3,4,5,6,7,8,9}; float *a = lower_bound(abc, abc+8, 3.2); cout<< *a; return 0; float abc[]={1,3,4,5,6,7,8,9}; 浮动*a=下限(abc,abc+8,3.2); cout*a在这两种情况下都将是4,因为a指向的值将是3.2,如果该值正确插入容器中 如果传递的值不在容器中,则下限和上

我试图在排序数组中找到最近的值,但上界和下界都给出了最高值

float abc[] = {1,3,4,5,6,7,8,9};

float *a  = lower_bound(abc, abc+8, 3.2);
cout<< *a;

return 0;
float abc[]={1,3,4,5,6,7,8,9};
浮动*a=下限(abc,abc+8,3.2);

cout
*a
在这两种情况下都将是4,因为
a
指向的值将是
3.2
,如果该值正确插入容器中

如果传递的值不在容器中,则
下限
上限
将返回相同的迭代器,这里就是这种情况

lower\u-bound
返回的迭代器定义为传递的元素在容器中可以驻留的最低位置,
higher\u-bound
返回最高位置。它们不返回与数组中最近的元素相关的任何内容


为了找到最近的元素,您知道
下限
的解引用结果大于或等于传递的值。在此之前的值(如果有)必须小于。您可以利用这一点来获得最接近的值。

*a
在这两种情况下都将是4,因为
a
指向的值如果正确插入容器中,将是
3.2

float abc[] = {1,3,4,5,6,7,8,9};

float *a  = lower_bound(abc, abc+8, 3.2);
cout<< *a;

return 0;
如果传递的值不在容器中,则
下限
上限
将返回相同的迭代器,这里就是这种情况

lower\u-bound
返回的迭代器定义为传递的元素在容器中可以驻留的最低位置,
higher\u-bound
返回最高位置。它们不返回与数组中最近的元素相关的任何内容


为了找到最近的元素,您知道
下限
的解引用结果大于或等于传递的值。在此之前的值(如果有)必须小于。您可以利用它来获得最接近的值。

由于数组中没有值3.2,那么
std::lower_bound
std::upper_bound
两种算法都将返回相同的迭代器

float abc[] = {1,3,4,5,6,7,8,9};

float *a  = lower_bound(abc, abc+8, 3.2);
cout<< *a;

return 0;

在这种情况下,您应该考虑上一个迭代器。< /P> 这是一个演示程序

#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main() 
{
    float abc[] = { 1, 3, 4, 5, 6, 7, 8, 9 };
    float value = 3.2f;

    auto it = std::lower_bound( std::begin( abc ), std::end( abc ), value );

    auto closest = it;

    if ( it == std::end( abc ) )
    {
        closest = std::prev( it );
    }
    else if ( it != std::begin( abc ) )
    {
        closest = std::min( std::prev( it ), it, 
                            [&value]( const auto &p1, const auto &p2 )
                            {
                                return abs( value - *p1 ) < abs( value - *p2 );
                            } );
    }

    std::cout << *closest << " at position " << std::distance( std::begin( abc ), closest ) << std::endl;
    return 0;
}

由于数组中没有值3.2,因此
std::lower_bound
std::upper_bound
两种算法都将返回相同的迭代器

在这种情况下,您应该考虑上一个迭代器。< /P> 这是一个演示程序

#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main() 
{
    float abc[] = { 1, 3, 4, 5, 6, 7, 8, 9 };
    float value = 3.2f;

    auto it = std::lower_bound( std::begin( abc ), std::end( abc ), value );

    auto closest = it;

    if ( it == std::end( abc ) )
    {
        closest = std::prev( it );
    }
    else if ( it != std::begin( abc ) )
    {
        closest = std::min( std::prev( it ), it, 
                            [&value]( const auto &p1, const auto &p2 )
                            {
                                return abs( value - *p1 ) < abs( value - *p2 );
                            } );
    }

    std::cout << *closest << " at position " << std::distance( std::begin( abc ), closest ) << std::endl;
    return 0;
}

. 似乎
4
是预期的数字。这可能会有所帮助。似乎<代码> 4 < /COD>是期望的数字。这可能有助于C++中找到最近的元素。@ AJA:在编写时没有;我最后添加了一个段落。C++中有什么东西可以找到最近的元素。@ AJA:不是在写作的时候;我添加了最后一段。在float value=3.2f;,f的用途是什么@aja它是一个浮点文本。如果没有f,它将是一个双字面值。在float value=3.2f;中f的用途是什么@aja它是一个浮点文本。如果没有f,它将是一个双字面值。