C++ 在对象列表上使用查找



我似乎无法让std::find使用std::list的对象。 我收到错误"与'运算符=='不匹配(操作数类型为'兔子'和'const int')"。相当确定我需要使用对象迭代器和 lambda 函数,我只是完全不知道如何做到这一点,我只需要一些指导。我最终要做的是迭代到列表中的特定位置,并从列表中的该对象中提取颜色字符串。经过编辑以澄清问题并简化代码

    #include <iostream>
    #include <list>
    #include <algorithm>
    #include "Rabbit.h"
    #include "Population.h"
    using namespace std;
    int main()
    {
        Population Tracker;
        Tracker.parentSeed(); //generate first random 8, and populate the list
        Tracker.Breed(); //generate children, not working nothing happening  as   getMother does not work
        return 0;
    }
    class Rabbit
    {
        protected:
            const std::vector<std::string> namesList
            {"Maxwell", "David", "Laura", "Sarah" , "Benjamin", "Carl",
             "Rick", "Maggie", "Glenn", "Daryl", "Michonne", "Roseita",
             "Leslie", "Randy", "Ethan", "Survan", "Leah", "Tisha", "Marcus"};
            const std::vector<std::string> colorList
            {"Red", "Green", "Blue",
             "Grey", "Tan", "Brown",
             "Calico", "White"};
        public:
            Rabbit(); //blank for the initial population
            Rabbit(int); // pass an int for color inherited from mother
            ~Rabbit();
            void getRabbit();
            void randomNumGen(int);
            std::string name;
            std::string color;
    };
    Rabbit::Rabbit()
    {
        std::random_device random; //random seed obj
        std::default_random_engine e1(random()); //get a random number seed
        std::uniform_int_distribution<int> name_dist(0, 17); // vector position of name
    std::uniform_int_distribution<int> color_dist(0, 7); // vector position of col
     color = colorList[color_dist(e1)];
     name = nameList[name_dist(e1)];
    }
    class Population
    {
        friend class Rabbbit;
        protected:
        public:
            Population();
            void popStats(int, bool);
            void popList(Rabbit);
            int getMother();
            void parentSeed(); // generate initial population.  All stats are random.
            std::list<Rabbit> femaleList;
            std::list<Rabbit> maleList;
            std::list<Rabbit>::iterator male_it;
            std::list<Rabbit>::iterator female_it;
    };
    int Population::getMother()
    {
       female_it++
       //do something to iterate list and get data from the object at that position.  Not to be sequential, just used that as example. 
    }
    void Population::Breed()
    {
        /*
            generate a new rabbit with color inhereited from the mother
            father does not matter as all other stats are random
        */
        if(maleList.size() > 2 && femaleList.size() > 2)
        {
            getMother(); // does nothing right now
            std::cout << "Breed Success!" << std::endl;
            Rabbit newRabbit;
            popList(newRabbit);
        }
    }
void Population::parentSeed()
{
    /*
        Generate the initial seed count
    */
    for (int i = 0; i < 8; i++)
    {
        Rabbit newRabbit;
        popList(newRabbit);
    }
}

编译器不知道应该如何区分Rabbit和原始整数。因此,您需要通过为Rabbit对象和整数定义重载相等运算符来向它展示如何区分它们。应将以下重载运算符添加到代码中:

bool
operator==(int const &i, Rabbit const &r) {
  ...
  return ?;
}
bool
operator==(Rabbit const &r, int const &i) {
  ...
  return ?;
}

相关内容

  • 没有找到相关文章

最新更新