2011-01-12 94 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; 
}