There is something I don't understand about the code that contains the C++ iterator.(Difficulty level: Beginner)

Asked 1 years ago, Updated 1 years ago, 90 views

Result of code:

Question: Although I lack the concept of itator, I'm studying how to write while looking at the code to get a better sense of it. I don't understand the code below in the last for statement in the main function.

As far as I'm concerned... The if clause seems to mean that if the location of the current iterator (in Turtle ~ Frog) is the starting point (Turtle = get the name pointed by the current iterator (in Turtle ~ Frog), and the else if clause seems to mean the name of the iterator if the location of the current iterator is larger than the location in the previous column.

But if the repeater's location is in the first column, Begin, I don't know what it has to do with the largest location value.

The meaning of the question may be difficult. Roughly... I think I know the meaning of the code, but I don't know why this is the way to get the winner value.

if (it == animal.begin())
    winner = (*it)->getName();
else if ((*it)->getLocation() > (*(it - 1))->getLocation()) 
    winner = (*it)->getName();

Full code:

// header.h
#include <iostream>
#include <string>
using namespace std;

class Animal { // Animal class definition
protected:
    string name;
    int location;
    int* speed;
public:
    Animal(string, int, int*);
    int getLocation();
    string getName();
    void setLocation(int a);
    virtual void move()
    {
        cout << "General Animal class" << endl;
    }
};

class Turtle : public Animal { // Turtle class definition
public:
    Turtle(string);
    void move();
};

class Rabbit : public Animal { // Rabbit class definition
public:
    Rabbit(string);
    void move();
};

class Frog : public Animal { // Frog class definition
public:
    Frog(string);
    void move();
};
// header.cpp
#include "Header.h"

int TTspeed[] = { 1,2,5 };
int rabspeed[] = { -1,0,1,2,10 };
int frogspeed[] = { 2,5 };

Animal::Animal(string name, int location, int *speed)
{
    this->name = name;
    this->location = location;
    this->speed = speed;
}

int Animal::getLocation()
{
    return location;
}

string Animal::getName()
{
    return name;
}

void Animal::setLocation(int location)
{
    this->location = location;
}

Turtle::Turtle(string name) : Animal(name, 0, TTspeed){}


void Turtle::move()
{
    int index = rand() % 3;
    location = location + speed[index];
    cout << "Turtle " << speed[index] << endl;
}

Rabbit::Rabbit(string name) : Animal(name, 0, rabspeed) {}

void Rabbit::move()
{
    int index = rand() % 5;
    location = location + speed[index];
    cout << "Rabbit " << speed[index] << endl;
}

Frog::Frog(string name) : Animal(name, 0, frogspeed) {}

void Frog::move()
{
    int index = rand() % 2;
    location = location + speed[index];
    cout << "Frog " << speed[index] << endl;
}
// Source.cpp
#include "Header.h"
#include <vector>

void main() {
    Animal* n = new Turtle("Turtle");
    Animal* n1 = new Rabbit("Rabbit");
    Animal* n2 = new Frog("Frog");
    string winner;
    vector<Animal*> animal; // polymorphic list
    animal.push_back(n);
    animal.push_back(n1);
    animal.push_back(n2);
    vector<Animal*>::iterator it;
    for (int i = 0; i < 10; i++) {
        cout << "<-------round " << i+1 << "------>" << endl;
        for (it = animal.begin(); it != animal.end(); it++) {
            (*it)->move();
        }
    }
    cout << "==============================" << endl;

    for (it = animal.begin(); it != animal.end(); it++) {
        cout << (*it)->getName() << " " << (*it)->getLocation() << endl;
        if (it == animal.begin())
            winner = (*it)->getName();
        else if ((*it)->getLocation() > (*(it - 1))->getLocation()) 
            winner = (*it)->getName();
    }
    cout << "winner : " << winner << endl;
}

c++ iterator

2022-09-22 22:01

1 Answers

I think what you understand is right.

for (it = animal.begin(); it != animal.end(); it++) {
    cout << (*it)->getName() << " " << (*it)->getLocation() << endl;
    if (it == animal.begin())
        winner = (*it)->getName();
    else if ((*it)->getLocation() > (*(it - 1))->getLocation()) 
        winner = (*it)->getName();
}

From the top to the next part is

    if (it == animal.begin())
        winner = (*it)->getName();

In order for some data to represent the predominance, you need at least two. But if you see one for the first time (or if you only have one piece of data), you don't have a comparison. So in this example, it appears that the first one (or if only one exists) was chosen to treat it as a winner.

And then the next part.

    else if ((*it)->getLocation() > (*(it - 1))->getLocation()) 
        winner = (*it)->getName();

feelings such as distance running, I think is probably location That she ran away at the time of the same title. Contextual the animal to win the title to determine program appears to be. Therefore, select the winners shall compare the distance, right?

So if you compare the distance between the data you're looking at (it) and the data you're looking at (it-1), you're going to change the winner, assuming that the winner is selected from the competitors from the beginning to the present (it-1), I think it's the final winner. (Or it could simply be an itterator practice example.) I think it might actually not work out here.

In the example above, the problem is if the Turtle is greater than Frog. If Turtle=50, Rabbit=19, and Frog=38 are considered as follows.

The problem here is choosing Frog as the winner without checking who went further, Turtle or Frog, who beat Rabbit.

If the above is correct, it would be better to change it as follows.

...
Change from Animal *winner; // string winner.
...
// Change the last iteration to refer to winner as follows.
for (it = animal.begin(); it != animal.end(); it++) {
    cout << (*it)->getName() << " " << (*it)->getLocation() << endl;
    if (it == animal.begin())
        winner = it;
    else if ((*it)->getLocation() > (*(winner))->getLocation()) 
        winner = it;
}
 cout << "winner : " << winner.getName() << endl;


2022-09-22 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.