set迭代器中的对象

Jua*_*blo 2 c++ iterator set

我可以在set迭代器中获取类的方法吗?

#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() ;
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

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
Run Code Online (Sandbox Code Playgroud)

     bool operator<(const student & s1) const { return true;}  
     bool operator==(const student & s1) const { return true;}  
Run Code Online (Sandbox Code Playgroud)

现在工作!! 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() ;
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*via 5

您需要constprint成员函数中添加qualifer :

void print() const
{
  std::cout << name << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

因为它们被用作键,所以它们中的对象std::set是必需的const.当对象(或引用)是常量时,您只能调用使用const限定符声明的该对象的成员函数.

您还需要和运算符重载函数的const限定符.(不要忘了修改,以在评论中指出.)==<===