2011-01-12 117 views
2

我可以獲得集合迭代器中類的方法嗎?集合迭代器中的對象

#include <iostream> 
#include <string> 
#include <set> 
class student{ 
    public: 
    student(std::string n){ 
     name=n; 
    } 
    void print(){ 
     std::cout << name << std::endl; 
    } 
    bool operator < (const student & s1){ return true;} 
    bool operator = (const student & s1){ return true;} 
    private: 
    std::string name; 
}; 
int main(){ 
    std::set<student> studs; 
    studs.insert(student("name01")); 
    studs.insert(student("name02")); 
    std::set<student>::iterator it; 
    for(it = studs.begin(); it != studs.end(); it++) 
     (*it).print() ; 
} 

我得到這個錯誤

students.cpp: In function ‘int main()’: 
students.cpp:22: error: passing ‘const student’ as ‘this’ argument of ‘void student::print()’ discards qualifiers 
/usr/include/c++/4.2.1/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = student]’: 
/usr/include/c++/4.2.1/bits/stl_tree.h:982: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = student, _Val = student, _KeyOfValue = std::_Identity<student>, _Compare = std::less<student>, _Alloc = std::allocator<student>]’ 
/usr/include/c++/4.2.1/bits/stl_set.h:307: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = student, _Compare = std::less<student>, _Alloc = std::allocator<student>]’ 
students.cpp:18: instantiated from here 
/usr/include/c++/4.2.1/bits/stl_function.h:227: error: passing ‘const student’ as ‘this’ argument of ‘bool student::operator<(const student&)’ discards qualifiers 

 bool operator<(const student & s1) const { return true;} 
    bool operator==(const student & s1) const { return true;} 

現在的工作! O_o',

#include <iostream> 
#include <string> 
#include <set> 
class student{ 
    public: 
    student(std::string n){ 
     name=n; 
    } 
    void print() const { 
     std::cout << name << std::endl; 
    } 
    bool operator<(const student & s1) const { return true;} 
    bool operator==(const student & s1) const { return true;} 
    private: 
    std::string name; 
}; 
int main(){ 
    std::set<student> studs; 
    studs.insert(student("name01")); 
    studs.insert(student("name02")); 
    std::set<student>::iterator it; 
    for(it = studs.begin(); it != studs.end(); it++) 
     it->print() ; 
} 
+0

你能改變你的問題嗎?無論你在做什麼,似乎都很好。那麼問題是什麼? – Nawaz 2011-01-12 18:59:05

+0

我想你的意思是重載`operator ==`,而不是`operator =`。 – suszterpatt 2011-01-12 19:00:07

回答

5

你需要一個const限定符添加到您的print成員函數:在std::set

void print() const 
{ 
    std::cout << name << std::endl; 
} 

對象必然是const,因爲它們被用作鍵。當對象(或引用)是常量時,只能調用用const限定符聲明的對象的成員函數。

您還希望在==<運算符重載函數上使用const限定符。 (不要忘記改變===在評論中指出。)

3

是的,儘管it->print()更直觀。

一個天真的世界觀是迭代器有點像指針。除此之外還有更多,如解釋here

迭代的最明顯的形式是 指針:一個指針可以指向 元件在陣列中,並且可以使用增量 運算符(++)迭代 通過它們。但是存在其他形式的迭代器。例如,每個容器類型(例如向量)都具有 特定的迭代器類型,其設計爲 以有效的方式遍歷其元素。

1

撰寫您print()功能是這樣的:

void print() const //<---- note this 'const' 
{ 
     std::cout << name << std::endl; 
} 

現在您的代碼應現在的工作。 :-)

順便說一句,右側出現const關鍵字的函數被稱爲const成員函數,因爲它們不能更改類的任何成員數據。

看到這個常見問題:[18.10] What is a "const member function"?

2
  1. 你想==操作符,而不是運營商=。

  2. 您的運營商<定義違反了std :: set的要求,並且與您的運營商<不一致。也就是說,根據您的運營商<,沒有什麼是等價的,但根據您的運營商==,一切都是平等的。運營商<應該定義非反射,傳遞和不對稱(對於非等價值)關係。

  3. 集合中的對象必須是const,所以要調用這個對象上的函數,該函數必須用const限定符聲明。具體來說,print()應該被聲明爲void print() const

  4. 同樣,運算符<應該用const限定符聲明。 std :: set要求運算符<可以用const對象調用。另一個有效的選擇是使運算符<成爲非成員函數,並通過值(壞)或常量引用(好)來同時採用這兩個對象。

  5. 儘管在你的例子中不需要,operator ==也應該用const限定符聲明。

0
#include <iostream> 
#include <set> 
using namespace std; 
class Boxer{ 
    public: 
     string name; 
     int strength; 
}; 
struct Comp{ 
    bool operator()(const Boxer& a, const Boxer& b){ 
     return a.strength > b.strength; 
    } 
}; 
int main(){ 
    Boxer boxer[3]; 
    boxer[0].name="uday", boxer[0].strength=23; 
    boxer[1].name="manoj", boxer[1].strength=33; 
    boxer[2].name="rajiv", boxer[2].strength=13; 

    set< Boxer, Comp> s; 
    s.insert(boxer[0]); 
    s.insert(boxer[1]); 
    s.insert(boxer[2]); 
    set< Boxer, Comp>::iterator it = s.begin(); 
    Boxer b = *it; 
    cout<<b.name; 
    //result is Manoj 

    return 0; 
}