C++ 集合迭代器中的对象

C++ 集合迭代器中的对象,c++,iterator,set,C++,Iterator,Set,我能在集合迭代器中得到一个类的方法吗 #include <iostream> #include <string> #include <set> class student{ public: student(std::string n){ name=n; } void print(){ std::cout << name << std::endl; }

我能在集合迭代器中得到一个类的方法吗

#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() ;
}
#包括
#包括
#包括
班级学生{
公众:
学生(标准::字符串n){
name=n;
}
作废打印(){

std::cout是的,不过
it->print()
更直观

一个天真的世界观是迭代器有点像指针

迭代器最明显的形式是 指针:指针可以指向 元素,并且可以迭代 通过它们使用增量 运算符(++)。但其他形式的 迭代器存在。例如,每个 容器类型(如向量)具有 一种特定的迭代器类型,用于 以循环方式遍历其元素 有效的方法


您需要将
const
限定符添加到
print
成员函数:

void print() const
{
  std::cout << name << std::endl;
}
void print()常量
{
std::cout像这样编写
print()
函数:

void print() const //<---- note this 'const'
{
        std::cout << name << std::endl;
}
void print()常量//
  • 您需要的是运算符=,而不是运算符=

  • 您的运算符<定义违反了std::set的要求,并且与运算符<代码>#包含不一致 #包括 使用名称空间std; 职业拳击手{ 公众: 字符串名; 内在力量; }; 结构组件{ 布尔运算符()(常量Boxer&a、常量Boxer&b){ 返回a.strength>b.strength; } }; int main(){ 拳击手拳击手[3]; 拳击手[0]。name=“uday”,拳击手[0]。力量=23; 拳击手[1]。name=“manoj”,拳击手[1]。力量=33; 拳击手[2]。name=“rajiv”,拳击手[2]。力量=13; 设置s; s、 插入(boxer[0]); s、 插入(boxer[1]); s、 插入(boxer[2]); set::迭代器it=s.begin(); 拳击手b=*it;
    你能重新表述你的问题吗?不管你在做什么,看起来都很好。那么问题出在哪里呢?我想你的意思是重载
    操作符==
    ,而不是
    操作符=
    。我想你的意思是
    布尔操作符==
    。重载操作符==相同的错误你的
    操作符错误与此用法无关,请参阅@Charles Salvia的ans我们期待解决这个问题。
    
    #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() ;
    }
    
    void print() const
    {
      std::cout << name << std::endl;
    }
    
    void print() const //<---- note this 'const'
    {
            std::cout << name << std::endl;
    }
    
    #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;
    }