如何在唯一指针对象的向量中打印出值(基于范围的循环)? C ++

时间:2021-11-09 20:46:05

I have a snippet of code here and I have a vector of smart pointers objects that are filled, how would I be able to print all the values of all the objects using range based for loops? I do not want the addresses, but the value. Thank you in advance.

我在这里有一段代码,我有一个智能指针对象的向量,我将如何使用基于范围的循环打印所有对象的所有值?我不想要地址,而是价值。先感谢您。

void printInfo(const std::vector< std::unique_ptr<Bird> >&fullData )
{
    for(auto &info : fullData)
    {
        std::cout<< &info <<endl; //I want this to print getName() for each object, but how do I do that with range-based-for-loop? Something like this.. info->getName()

    }
}
void fillFlock( std::vector< std::unique_ptr<Bird> > &cage)  
{

  size_t cageSize;
  std::string name;

  std::cout << "Bird cage size: " <<std::endl;
  std::cin >> cageSize;

  //set container size
  cage.resize(cageSize);

    for(auto &bird : cage)
    {
        std::cout<<"Bird Name " <<std::endl;
        std::cin >> name;

        bird.reset(new Bird(name));
    }
}
int main()  
{
    std::vector< std::unique_ptr<Bird> > m_flock;
    fillFlock(m_flock);
    printInfo(m_flock);
}

1 个解决方案

#1


1  

You're actually making a new pointer to your unique pointers, and then printing the address of that. Use & to make a pointer and * to dereference (get the value). In other words,

你实际上是在创建一个指向你的唯一指针的新指针,然后打印它的地址。使用&来制作指针和*取消引用(获取值)。换一种说法,

std::cout << *info << std::endl;

EDIT: Question was changed and extra context added

编辑:问题已更改,并添加了额外的上下文

If you want to call the function on info, then call the function,

如果你想在info上调用函数,那么调用函数,

std::cout << info->getName() << std::endl;

The type of info is just std::unique_ptr<Bird>& if that is what is throwing you.

信息的类型只是std :: unique_ptr &如果这就是扔你的东西。

Perhaps you want this to happen automatically with the streaming operator. A way of doing this is to define a operator<< that takes your Bird class. You can do this by adding the following declaration to your Bird class,

也许您希望这与流媒体运营商自动发生。这样做的一种方法是定义一个带有Bird类的运算符<<。您可以通过向Bird类添加以下声明来完成此操作,

class Bird {
    friend std::ostream& operator<<(std::ostream& out, const Bird& bird);
};

and then in your source file add the definition,

然后在源文件中添加定义,

std::ostream& operator<<(std::ostream& out, const Bird& bird) {
    return out << bird.getName();
}

This assumes that your getName function is const-qualified, which it really should be.

这假设你的getName函数是const限定的,它确实应该是。

#1


1  

You're actually making a new pointer to your unique pointers, and then printing the address of that. Use & to make a pointer and * to dereference (get the value). In other words,

你实际上是在创建一个指向你的唯一指针的新指针,然后打印它的地址。使用&来制作指针和*取消引用(获取值)。换一种说法,

std::cout << *info << std::endl;

EDIT: Question was changed and extra context added

编辑:问题已更改,并添加了额外的上下文

If you want to call the function on info, then call the function,

如果你想在info上调用函数,那么调用函数,

std::cout << info->getName() << std::endl;

The type of info is just std::unique_ptr<Bird>& if that is what is throwing you.

信息的类型只是std :: unique_ptr &如果这就是扔你的东西。

Perhaps you want this to happen automatically with the streaming operator. A way of doing this is to define a operator<< that takes your Bird class. You can do this by adding the following declaration to your Bird class,

也许您希望这与流媒体运营商自动发生。这样做的一种方法是定义一个带有Bird类的运算符<<。您可以通过向Bird类添加以下声明来完成此操作,

class Bird {
    friend std::ostream& operator<<(std::ostream& out, const Bird& bird);
};

and then in your source file add the definition,

然后在源文件中添加定义,

std::ostream& operator<<(std::ostream& out, const Bird& bird) {
    return out << bird.getName();
}

This assumes that your getName function is const-qualified, which it really should be.

这假设你的getName函数是const限定的,它确实应该是。