C++ 检查对的一个值后,对向量上的下界

C++ 检查对的一个值后,对向量上的下界,c++,sorting,c++11,std-pair,lower-bound,C++,Sorting,C++11,Std Pair,Lower Bound,我在C++中有一个成对向量vectorbool { if(currPair==ele)返回true; 返回(checkPair.first>=ele.first/-->),其值大于或等于给定字符串 &&ele.second第二个值小于一个数字 }); (getLower!=vec.cend())? std::cout首先,你的向量不是按比较排序的,因此按比较排序的下限没有意义。请建议一种执行任务的方法。请。即使按比较排序,在某些情况下,答案也是错误的。为什么这个例子中向量不是按比较排序的?听起来

我在
C++
中有一个成对向量
vector
,我想降低字符串值的下界,但有一个附加约束,即成对向量的第二个值应小于或等于给定值。 目前我正在使用一个比较模板

bool compare(const T &a,const T &b){
if (a.first<=b.first&&b.second<=a.second) return true;
}
我想在
abc,3
上设置下限,因此它应该返回
abcd,1
,但它正在返回
abcdex,3
。请帮助

属于algorithum系列,在该系列中,元素使用运算符<进行比较 第一个版本,第二个版本为comp。范围内的元素 应已根据相同标准进行排序(运算符<或comp),或至少根据val进行分区

这意味着,您需要按照前面提到的方式对向量进行排序,以便按照您的预期操作
std::lower_bound

一旦你按照前面提到的方法对数组向量进行了排序,你就可以使用
compare
函子/(我把它做成lambda),你可以使用
std::lower_bound


注意:为了使用
std::lower_bound
,您需要根据您希望首先应用下限的方式对向量进行排序(这是最基本的)

但是,在排序模式中,
std::lower_bound
不知道数组是否正确排序(
int
)对的第二个值。换言之,即使您对前面提到的内容进行相应的排序,
std::lower_bound
也不能给出所需的结果,因为您对
进行排序的方式是
Pair.first
Pair.second
顺序相反

因此,我建议您使用,它将线性搜索元素,并且必须使用与谓词相同的比较函数。如果事先对向量进行了相应的排序(如您所述),那么它应该会给您一个正确的结果

// sort before
checkPair =  Pair("abc",3);
auto getLower = std::find_if( vec.begin(), vec.end(), [&checkPair](const Pair &ele) -> bool
{
   if(currPair == ele ) return true;

   return (checkPair.first >= ele.first      //--> "whose value is greater than or equal to the given string
          && ele.second < checkPair.second); //--> second value is less than a number
});

(getLower != vec.cend()) ? 
         std::cout << getLower->first << " " << getLower->second:
         std::cout << "Nothing found";
//排序之前
支票对=对(“abc”,3);
auto getLower=std::find_if(vec.begin()、vec.end()、[&checkPair](const-Pair&ele)->bool
{
if(currPair==ele)返回true;
返回(checkPair.first>=ele.first/-->),其值大于或等于给定字符串
&&ele.second第二个值小于一个数字
});
(getLower!=vec.cend())?
std::cout first
属于algorithum系列,在该系列中,元素使用运算符<进行比较
第一个版本,第二个版本为comp。范围内的元素
应已根据相同标准进行排序(运算符<或comp),或至少根据val进行分区

这意味着,您需要按照前面提到的方式对向量进行排序,以便按照您的预期操作
std::lower_bound

一旦你按照前面提到的方法对数组向量进行了排序,你就可以使用
compare
函子/(我把它做成lambda),你可以使用
std::lower_bound


注意:为了使用
std::lower_bound
,您需要根据您希望首先应用下限的方式对向量进行排序(这是最基本的)

但是,在排序模式中,
std::lower_bound
不知道对的第二个值(
int
)数组是否正确排序。换句话说,即使您根据前面提到的内容进行相应排序,
std::lower_bound
也不能给出所需的结果,因为您对
进行排序的方式是
Pair.first
Pair.second

因此,我建议您使用,它将线性搜索元素,并且必须使用与谓词相同的比较函数。如果事先对向量进行了相应的排序(如您所述),那么它应该会给出正确的结果

// sort before
checkPair =  Pair("abc",3);
auto getLower = std::find_if( vec.begin(), vec.end(), [&checkPair](const Pair &ele) -> bool
{
   if(currPair == ele ) return true;

   return (checkPair.first >= ele.first      //--> "whose value is greater than or equal to the given string
          && ele.second < checkPair.second); //--> second value is less than a number
});

(getLower != vec.cend()) ? 
         std::cout << getLower->first << " " << getLower->second:
         std::cout << "Nothing found";
//排序之前
支票对=对(“abc”,3);
auto getLower=std::find_if(vec.begin()、vec.end()、[&checkPair](const-Pair&ele)->bool
{
if(currPair==ele)返回true;
返回(checkPair.first>=ele.first/-->),其值大于或等于给定字符串
&&ele.second第二个值小于一个数字
});
(getLower!=vec.cend())?

std::cout首先,你的向量不是按比较排序的,因此按比较排序的下限没有意义。请建议一种执行任务的方法。请。即使按比较排序,在某些情况下,答案也是错误的。为什么这个例子中向量不是按比较排序的?听起来你希望它按一种方式排序,而不是按一种方式排序搜索另一个。您需要两个比较函数。除了
string(“abcdex”)之外,是否有
string(“abcde”)错误那么你的向量不是按比较排序的,所以它的比较下限没有意义。请建议一种方法来完成这项任务。请。即使按比较排序,在某些情况下答案也是错误的。为什么这个例子中向量不是按比较排序的?听起来你希望它按一种方式排序,然后按另一种方式搜索。您需要2个比较函数。除了
string(“abcde”)不需要担心之外,is
string(“abcdex”)是错误的。您可能无法这样做,因为OP没有聊天权限。Mod创建的房间允许所有参与用户访问。无需担心。您可能无法这样做,因为OP没有聊天权限。Mod创建的房间允许所有参与用户访问。
abcd 1
// sort before
checkPair =  Pair("abc",3);
auto getLower = std::find_if( vec.begin(), vec.end(), [&checkPair](const Pair &ele) -> bool
{
   if(currPair == ele ) return true;

   return (checkPair.first >= ele.first      //--> "whose value is greater than or equal to the given string
          && ele.second < checkPair.second); //--> second value is less than a number
});

(getLower != vec.cend()) ? 
         std::cout << getLower->first << " " << getLower->second:
         std::cout << "Nothing found";