C++ 如何找到向量中第一个小于整数X的元素?(c+;+;)

C++ 如何找到向量中第一个小于整数X的元素?(c+;+;),c++,stl,vector,binary-search,C++,Stl,Vector,Binary Search,如果我有以下向量{10 10 20 30} 我想要一个函数返回=X的整数的位置,或者直接返回X后面的较小元素,例如,如果我搜索11,我希望函数返回2,因为第二个元素(10)是向量中比11小的第一个元素。我尝试使用下限,但这不起作用 int myints[] = {10,20,30,30,20,10,10,20}; vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 vector<int>

如果我有以下向量{10 10 20 30} 我想要一个函数返回=X的整数的位置,或者直接返回X后面的较小元素,例如,如果我搜索11,我希望函数返回2,因为第二个元素(10)是向量中比11小的第一个元素。
我尝试使用下限,但这不起作用

int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20
vector<int>::iterator low,up;

sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

low=lower_bound (v.begin(), v.end(), 11); //
up= upper_bound (v.begin(), v.end(), 11); //

cout << "lower_bound at position " << int(low- v.begin()) << endl;
cout << "upper_bound at position " << int(up - v.begin()) << endl;

return 0;

那么,
upper\u bound
返回第一个大于测试项目的项目,因此之前的项目(如果存在)将是您想要的项目?

cppreference通知我
std::lower\u bound

返回一个迭代器,该迭代器指向范围[first,last]中不小于value的第一个元素

std::上限

返回一个迭代器,该迭代器指向范围[first,last]中大于value的第一个元素

在这种情况下,给定一个包含
10 10 10 20 30
的向量,我希望两个函数都指向第一个
20
,它位于向量中的位置3,实际上是两次都得到的结果。如果您要求
20
std::lower_bound
将返回一个指向向量中的第一个
20
(位置3)…第一个数字不小于20,与请求
11
时得到的结果相同。但在这种情况下,
std::upper_bound
将返回指向第一个
30
(位置6)的迭代器,该迭代器是大于20的第一个值


只需将迭代器向后移动一次,即可获得比目标值小的最后一个值,
std::prev
是一种方法。

可以这样做……如果向量为空,最好返回迭代器

auto find_next_smaller(vector<int> vec, const int x) { 
    std::sort(vec.begin(), vec.end());
    auto it = std::lower_bound(vec.begin(), vec.end(), x); 
    if (it == vec.end()) { 
      it = (vec.rbegin()+1).base();
    }
    else if (it != vec.begin() && *it > x) { 
        --it; 
    }

    return it; 
} 
自动查找下一个较小的(向量向量,常量int x){
排序(vec.begin(),vec.end());
auto it=std::下限(vec.begin(),vec.end(),x);
如果(it==vec.end()){
it=(vec.rbegin()+1.base();
}
如果(it!=vec.begin()&&*it>x){
--它;
}
归还它;
} 

如果必须查找小于或等于某个x的元素,则可以使用multiset来查找

#include <iostream> 
#include <set> 
#include <iterator> 

using namespace std; 

int main() 
{
    multiset <int, greater <int> > iammultiset;
    iammultiset.insert(10);
    iammultiset.insert(10);
    iammultiset.insert(14);
    iammultiset.insert(20);
    iammultiset.insert(20);
    iammultiset.insert(30);
    iammultiset.insert(40);
    iammultiset.insert(50);
    //{10,10,14,20,20,30,40,50}
    
    cout<<*iammultiset.lower_bound(17) << endl;
    //The Output here will be 14.
    
    cout<<*iammultiset.lower_bound(20) << endl;
    //The Output here will be 20.
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
多集;
插入(10);
插入(10);
IAMMILISET.插入(14);
IAMMILISET.插入(20);
IAMMILISET.插入(20);
IAMMILISET.插入(30);
IAMMILISET.insert(40);
IAMMILISET.插入(50);
//{10,10,14,20,20,30,40,50}

coutBut position 2不是示例中第一个小于11的元素。它是最后一个小于11的元素。可能
upper_bound()-1
但你确实需要清楚你想要的是什么,并对它进行适当的编码。你的向量总是排序吗?或者这只是巧合吗?我想这就解决了问题。一个小问题,为什么下限在这里返回20?@LoersAntario请参阅
下限
的文档,我在回答中引用了;-)
#include <iostream> 
#include <set> 
#include <iterator> 

using namespace std; 

int main() 
{
    multiset <int, greater <int> > iammultiset;
    iammultiset.insert(10);
    iammultiset.insert(10);
    iammultiset.insert(14);
    iammultiset.insert(20);
    iammultiset.insert(20);
    iammultiset.insert(30);
    iammultiset.insert(40);
    iammultiset.insert(50);
    //{10,10,14,20,20,30,40,50}
    
    cout<<*iammultiset.lower_bound(17) << endl;
    //The Output here will be 14.
    
    cout<<*iammultiset.lower_bound(20) << endl;
    //The Output here will be 20.
}