繁体   English   中英

在 C++ 中显示出意外行为的下限和上限

[英]lower_bound and upper bound showing an unexpected behaviour in c++

我有 2 个向量,每个向量都包含某些元素

   for(int i=0;i<vec1.size();i++){
    int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    }

   for(int i=0;i<vec1.size();i++){
    int low = lower_bound(vec1.begin(),vec1.end(),vec2[i])-vec1.begin();
    int up = upper_bound(vec1.begin(),vec1.end(),vec2[i])-vec1.begin();
    }

这是整个代码片段:

    for(int i=0;i<n;i++)
       for(int j=0;j<n;j++)
          for(int k=0;k<n;k++)
            vec1.push_back((arr[i]*arr[j])+arr[k]);
    for(int i=0;i<n;i++)
       for(int j=0;j<n;j++)
         for(int k=0;k<n;k++)
         {
           if(arr[k]==0)
           continue;
           else
           vec2.push_back((arr[i]+arr[j])*arr[k]);
          }
sort(vec1.begin(),vec1.end());
sort(vec2.begin(),vec2.end());
int res=0;
    for(int i=0;i<vec1.size();i++)
    {
        int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
        int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
        res+=(up-low);
    }


for(int i=0;i<vec1.size();i++)
        {
            int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
            int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
            res+=(up-low);
        }

现在基本上我想要做的是找到一个元素在 vec1 和 vec2 中出现的总次数。我面临的问题是,尽管这两个代码基本上实现了相同的逻辑,但为 res 提供了不同的值。 这不应该发生,因为它们的逻辑是相同的。 使用的向量大小相同。 这是我不知道的下界和上限的意外行为吗?

这是你的完整代码吗?
我不明白你想要的代码是什么?

int res=0;
for(int i=0;i<vec1.size();i++)
{
    int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    res+=(up-low);
}
for(int i=0;i<vec1.size();i++)
        {
            int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
            int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
            res+=(up-low);
        }

如果你想计算一个值在两者中出现的次数,你应该使用 2 个变量,如 res1 和 res2。

我想你想这样做:

例如:如果 arr = [1,2],那么排序后 vec1 和 vec2 都包含 [2,3,3,3,4,4,5,6]。 并且您想在两个向量中找到 2,3,4,5 和 6 的出现次数。 我对吗?

如果是,那么您首先需要在向量中找到唯一值。因为现在您正在循环中搜索值 3 三次。

一旦您将这两个向量都转换为集合。 使用以下代码。

int res=0;
for(int i=0;i<vec1.size();i++)
{
    int low = lower_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    int up = upper_bound(vec2.begin(),vec2.end(),vec1[i])-vec2.begin();
    res+=(up-low);
}

int res2=0;
    for(int i=0;i<vec2.size();i++)
            {
                int low = lower_bound(vec1.begin(),vec1.end(),vec2[i])-vec1.begin();
                int up = upper_bound(vec1.begin(),vec1.end(),vec2[i])-vec1.begin();
                res2+=(up-low);
            }
// Compare res and res2 here.

您错过的另一件事是,在两个循环中,您都在查找 vector1。在第一个循环中,您应该在 vector2 中查找 set1 的值,在 vector1 中查找 set2 的第二个循环值。

希望这是你想要的。 如果没有,请在评论中提问。 我会修改我的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM