2012-10-19 88 views
5

我有以下結構比較功能下界

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編程新手,能否請你糾正我錯誤在哪裏?有沒有更好的做到這一點?

謝謝!

+2

我沒有得到所有這些投票結束,這個問題沒有錯。代碼有錯誤,但這使得它成爲一個有效的問題。 – CashCow

+0

@CashCow - 我完全同意。 StackOverflow幾乎致命的缺陷,在我看來,是過度熱心的近距離選民的活動。由於SO usership增長,接近投票所需的數量不*不*增長,並接近選民更積極通過訂單或幅度比重新打開訂單的選民,導致此問題。 –

+1

如果問題是他有編譯器錯誤,他應該告訴我們他們是什麼。如果我們發現錯誤,我們可以立即發佈答案。如果沒有這些錯誤,我們必須經歷自己編寫它的額外工作,這使我們不想回答,這使得這是一個不好的問題。 –

回答

10

謂詞需要帶兩個參數並返回bool。

由於您的函數是成員函數,它具有錯誤的簽名。另外,您可能需要能夠使用函數將Value與int,Value與Value,int和Value進行比較,將int與int進行比較。

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; 
    } 
}; 

這是相當麻煩的,所以讓我們減少碳排放量

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:

您的函數GetLocationForTime不返回值。你需要把lower_bound的結果和begin()從中減去。該函數也應該是const。

如果此呼叫的意圖是在這裏插入,則考慮的事實是在載體中的中間插入是O(N)的操作,並且因此矢量可能是錯誤的集合類型在這裏。

注意,lower_bound算法只適用於預排序的集合。如果您希望能夠查找不同的成員,而不訴諸不斷,你會希望在這些領域創建索引,可能使用升壓轉換器的multi_index

0

class是關鍵字,而不是 「類」:

class MyClass { 

其車身應遵循分號;
可能存在其他錯誤,但您可能需要將它們粘貼到問題中才能獲得進一步的幫助。

2

一個錯誤是lower_bound的第四個參數(代碼中的compareValue)不能是成員函數。它可以是一個仿函數或一個自由函數。使它成爲MyClass的朋友的免費函數似乎是最簡單的。你也錯過了return關鍵字。

class MyClass { 
    MyClass() { InsertValues(); } 
    void InsertValues(); 
    int GetLocationForTime(int time); 
    friend bool compareValue(const Value& lhs, const Value& rhs) 
    { 
     return lhs.time < rhs.time; 
    } 
0

你只是想讓compareValue()成爲一個正常的功能。你現在已經實現了它的方式,你需要一個類型爲MyClass的對象。 std::lower_bound()將嘗試調用它,它只會傳入兩個參數,沒有額外的對象。如果您真的想要成爲會員的功能,您可以將其設爲static成員。

也就是說,直接使用函數會有性能損失。你可能希望有比較類型與inline函數調用操作:

struct MyClassComparator { 
    bool operator()(MyClass const& m0, MyClass const& m1) const { 
     return m0.time < m1.time; 
    } 
}; 

...並使用MyClassComparator()作爲比較。

2
  1. Class關鍵字必須從低c開始 - class
  2. struct Value有錯誤類型qualtiy,而不是quality
  3. 我沒有看到using namespace std使用STL類型離不開它。
  4. vector<value> - 錯誤類型的value代替Value
  5. 等等

你必須利用這種簡單的錯誤,我想在這裏發帖之前先檢查一下。 這裏的主要問題是比較函數不能成爲類的成員。使用它作爲免費功能:

bool compareValue(const Value lhs, const int time) { 
    return lhs.time < time ; 
} 
+0

我修復了幾個錯別字,假設他使用了namespace std;幷包含相關標題。 (當然更好的是限定而不是使用命名空間)。 – CashCow