列表和迭代器c ++是困扰我这些天的主题 [英] Lists and Iterators c++ are the topics bothering me these days a lot

查看:63
本文介绍了列表和迭代器c ++是困扰我这些天的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我是c +的新手,并且在很短的时间内学习了这些列表和迭代器。

我现在卡住了困惑。

首先我要了解的列表和迭代器是

列表就像一个普通的列表,你可以在其中推送你的对象或值(字符串,整数等等) )就像我们在堆栈中做的那样

迭代器只是为了说明我们推送的东西,我们也可以通过取消它来知道我们推高了什么。

现在我的问题是为什么有些人有时会使用数据类型的指针。例如

std :: list< animals *> animal_list; //动物是我的班级

std :: list< animals *> :: iterator iter; //

我们这样做是为了访问类动物的指针,例如我尝试制作这段代码

Hallo
I am new to c++ and learning this Lists and Iterators from quite a few days.
I am stuck and confused now.
First what I got to know abouts Lists and Iterators were
Lists ist just like a normal list in which you can push your objects or values(strings, int and so on) like we do in stack
Iterators are just to poitn out the stuff we pushed in and we can also know what we pushed up just by dereferecing it.
Now my question is Why some people put a pointer with a datatype sometimes. For eg
std::list<animals*> animal_list; // where animal is my class
std::list<animals*> ::iterator iter; //
We do this to access the pointers to the class animals for eg I tried making this code

#include <stdio.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Animals
{
public:
	void getName(){
		cout << "this is a"<< *it;
	}
};
class Dog : public Animals{
public:
	Dog(string dog){
		cout << "the breed of the dog is"<< dog<< endl ;
	}
};
class Cat : public Animals{
public:
	Cat(string colour){
		cout << "the color of the cst is"<<colour<<endl;
	}
};
class Rabbit : public Animals{
public:
	Rabbit(string colour){
		cout << "the color of the rabbit is"<< colour<<endl;
	}
};
int main()
{
	list<animals*> animals;
	Dog *d = new Dog("labrador");
	Cat *c = new Cat("braun");
	Rabbit *r = new Rabbit("black n white");
	animals.push_back(d);
	animals.push_back(c);
	animals.push_back(r);
	list<animals*>::iterator it;
	for (it = animals.begin(); it != animals.end(); ++it){
		cout << (*it)->getName(); // here I'm gettng error because of << operands. 
	}	
}



在这种情况下,我制作了3个派生类,所有这些都继承了动物类。我的主要目的是访问在基类中声明的函数getName()。

现在我做了三个指向这些类的指针。然后我把它们推到列表中,以便将狗,猫和兔子放在我的清单中。我不知道我是对还是错。

我真的搞砸了。它没有在我的脑海里。

请帮助和抱歉这样的lng描述。我只是想清楚地解释一下自己

谢谢

Ps:请不要在阅读后感到困惑。


In this case I made 3 derived classes and all these are inheriting animals class. My main purpose is to access the function getName() declared in base class.
Now I made three pointers pointing to these classes. And then I pushed them up in the list in order to have the dog, cat and rabbit in my list. I don't know if I'm right or wrong.
I am really screwed up.Its not getting in my head.
Please help and sorry for such a lng description. I just wanted to clearly explain myself
Thanks
P.s : Please don't get confuse after reading it.

推荐答案

您在 Animals 指针上调用 getName(),但该函数调用了一些未定义的迭代器在课堂里。您应该将类​​更改为:

You are calling getName() on your Animals pointer, but that function calls some iterator that is not defined in the class. You should change your classes to something like:
class Animal
{
protected string _animal;
public:
    Animal(string type)
    {
        _animal = type;
    }

};
class Dog : public Animal
{
public:
    string getName()
    {
        return _animal + " is a dog";
    }
};


可能你的意思是:

Possibly you mean something like:
 #include <iostream>
 #include <list>
 #include <string>
 using namespace std;


class Animals
{
public:
  virtual void showName(){
    cout << "this is a animal" << endl;
  }
  virtual ~Anumals(){}
};
class Dog : public Animals{
  string breed;
public:
  Dog(string breed):breed(breed){}
  void showName()
  {
    cout << "the breed of the dog is " << breed << endl ;
  }
};
class Cat : public Animals{
string colour;
public:
  Cat(string colour):colour(colour){}
  void showName()
  {
    cout << "the colour of the cat is " << colour << endl;
  }
};
class Rabbit : public Animals{
string colour;
public:
  Rabbit(string colour):colour(colour){}
  void showName()
  {
    cout << "the colour of the rabbit is "<< colour << endl;
  }
};
int main()
{
  list<Animals*> animals;
  Dog *d = new Dog("labrador");
  Cat *c = new Cat("braun");
  Rabbit *r = new Rabbit("black n white");
  animals.push_back(d);
  animals.push_back(c);
  animals.push_back(r);
  list<Animals*>::iterator it;
  for (it = animals.begin(); it != animals.end(); ++it){
    (*it)->showName(); 
  }
  for (it = animals.begin(); it != animals.end(); ++it)
    delete (*it);
}


这篇关于列表和迭代器c ++是困扰我这些天的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆