iterating objects

What is it called when you iterate objects? ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class snake
{
public:
void setAge(int a) {_age=a;}
int getLength();
private:
int _age;
}

int snake::getLength()
{
int age;
std::cin>>age;
setAge(age);
return age*3;
}

int main()
{
snake mary,joe,betty;
int numSnakes=3,length;
for(int i=0;i<numSnakes;i++)
{
length=SNAKE.getLength()
std::cout<<"The length of snake "<<SNAKE<<" is "<<length<<std::endl;
}
return 0;
}

where the program would iterate SNAKE as mary, joe and betty.
closed account (1yR4jE8b)
ummmmmm....I think you answered your own question there buddy....

iterate
That code doesn't make any sense. What is SNAKE? It isn't even a valid object or container. Does it compile?
I think the question is more along the lines of what is an array or container...
If you want to iterate through your three defined snakes mary, joe and betty then you need to put them in some kind of container.

You could put them in a std::vector for instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>

int main()
{
    snake mary, joe, betty;

    std::vector<snake> pit;
    
    pit.push_back(mary);
    pit.push_back(joe);
    pit.push_back(betty);

    std::vector<snake>::iterator i;
    for(i = pit.begin(); i != pit.end(); ++i)
    {
        std::cout << "The length of snake " << *i << " is " << i->getLength() << std::endl;
    }

    return 0;
}
Oh, I get it.
What is it called when you iterate objects?
It's called nothing, because you can't do that. Well, not directly, anyway.

1
2
3
4
T a,b,c;
T *array[]={&a,&b,&c,0};
for (int i=0;array[i];i++)
    array[i]->f();
Do i need to do something special here with the vector pointer *i? I am getting an error in relation to the pointer.
Okay, I see the problem. There is no operator<< for snake objects and there is no accessor for the _age attribute. So make an accessor and use it instead of *i. You cannot send a snake object to operator<< without defining that operator for the type. Don't have time to do that right now but let me show you something else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>

int main()
{
    snake mary, joe, betty;

    std::vector<snake> pit;
    pit.reserve(3); // reserve for 3 otherwise the push_back might create more memory than needed.
    pit.push_back(mary);
    pit.push_back(joe);
    pit.push_back(betty);
    
    // first initialize i and end at the beginning
    // second, if you want to name the snake then create attribute, accessor for it and send that
    // to stream.  Or define operator<< for snake and send *i to operator<<.
    for(std::vector<snake>::iterator i = pit.begin(), end = pit.end();; i != end; ++i)
    {
        std::cout << "The length of snake " << " is " << i->getLength() << std::endl;
    }

    return 0;
}


Perhaps someone else can show you the operator<< or you can google for that.
You can't output the literal names "mary", "joe" and "betty" unless you include them in the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <vector>

class snake
{
public:
    snake(const std::string& name): _name(name) {}
    void setAge(int a) {_age=a;}
    int getLength();
    std::string getName() { return _name; }
private:
    int _age;
    std::string _name;
}


Then you can print their names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

int main()
{
    snake mary("Mary"); // give them names
    snake joe("Joe");
    snake betty("Betty");

    std::vector<snake> pit;
    pit.reserve(3); // reserve for 3 otherwise the push_back might create more memory than needed.
    pit.push_back(mary);
    pit.push_back(joe);
    pit.push_back(betty);
    
    for(std::vector<snake>::iterator i = pit.begin(), end = pit.end(); i != end; ++i)
    {
        std::cout << "The length of snake " << i->getName() << " is " << i->getLength() << std::endl;
    }

    return 0;
}

Last edited on
Topic archived. No new replies allowed.