比较下界函数 [英] compare function in lower bound

查看:21
本文介绍了比较下界函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构

    enum quality { good = 0, bad, uncertain };

    struct Value {
       int time;
       int value;
       quality qual;
    };

    class MyClass {

public:
    MyClass() {
        InsertValues();
    }

       void InsertValues();

       int GetLocationForTime(int time);

private:

    vector<Value> valueContainer;
};

void MyClass::InsertValues() {
    for(int num = 0; num < 5; num++) {
        Value temp;
        temp.time = num;
        temp.value = num+1;
        temp.qual = num % 2;
        valueContainer.push_back(temp);
    }
}


int MyClass::GetLocationForTime(int time)
{

   // How to use lower bound here.
   return 0;
}

在上面的代码中,我被抛出了很多编译错误.我想我在这里做错了我是 STL 编程的新手,你能纠正我错误在哪里吗?有没有更好的办法?

In above code I have been thrown with lot of compile errors. I think I am doing wrong here I am new to STL programming and can you please correct me where is the error? Is there better to do this?

谢谢!

推荐答案

谓词需要带两个参数,返回bool.

The predicate needs to take two parameters and return bool.

由于您的函数是成员函数,因此签名错误.

As your function is a member function it has the wrong signature.

此外,您可能需要使用函子将 Value 与 int、Value 与 Value、int 与 Value 以及 int 与 int 进行比较.

In addition, you may need to be able to compare Value to int, Value to Value, int to Value and int to int using your functor.

struct CompareValueAndTime
{
   bool operator()( const Value& v, int time ) const 
   {
       return v.time < time;
   }

   bool operator()( const Value& v1, const Value& v2 ) const 
   {
       return v1.time < v2.time;
   }

   bool operator()( int time1, int time2 ) const
   {
       return time1 < time2;
   }

   bool operator()( int time, const Value& v ) const
   {
      return time < v.time;
   }
};

这很麻烦,所以让我们减少它:

That is rather cumbersome, so let's reduce it:

struct CompareValueAndTime
{
   int asTime( const Value& v ) const // or static
   {
      return v.time;
   }

   int asTime( int t ) const // or static
   {
      return t;
   }

   template< typename T1, typename T2 >
   bool operator()( T1 const& t1, T2 const& t2 ) const
   {
       return asTime(t1) < asTime(t2);
   }
};

然后:

std::lower_bound(valueContainer.begin(), valueContainer.end(), time,
   CompareValueAndTime() );

还有一些其他错误,例如类声明末尾没有分号,而且类的成员默认是私有的,在这种情况下,整个类都是私有的.您是否在构造函数之前错过了 public: ?

There are a couple of other errors too, e.g. no semicolon at the end of the class declaration, plus the fact that members of a class are private by default which makes your whole class private in this case. Did you miss a public: before the constructor?

您的函数 GetLocationForTime 不返回值.您需要获取 lower_bound 的结果并从中减去 begin() .函数也应该是常量.

Your function GetLocationForTime doesn't return a value. You need to take the result of lower_bound and subtract begin() from it. The function should also be const.

如果这个调用的目的是在这里插入,那么考虑到在向量中间插入是一个 O(N) 操作,因此向量在这里可能是错误的集合类型.

If the intention of this call is to insert here, then consider the fact that inserting in the middle of a vector is an O(N) operation and therefore vector may be the wrong collection type here.

请注意,lower_bound 算法仅适用于预先排序的集合.如果您希望能够在不不断求助的情况下查找不同的成员,您将需要在这些字段上创建索引,可能使用 boost 的 multi_index

Note that the lower_bound algorithm only works on pre-sorted collections. If you want to be able to look up on different members without continually resorting, you will want to create indexes on these fields, possibly using boost's multi_index

这篇关于比较下界函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆