简体   繁体   English

如何打印出包含对象的向量?

[英]How to print out a vector which holds objects?

This question stems from a larger code I am working on but hopefully I can break it down to simplify:这个问题源于我正在处理的更大的代码,但希望我可以将其分解以简化:

Say I have two classes, one which holds 2 integer variables, and another which contains a vector that holds the first class假设我有两个类,一个包含 2 个 integer 变量,另一个包含一个包含第一个 class 的向量

class TwoInts {
     int num1;
     int num2;
}

class NumHold {
    std::vector<TwoInts> numVector;
}

How would I go about printing my numHold class exactly?我将如何 go 准确地打印我的 numHold class ? I know I would have to provide an overloaded stream operator我知道我必须提供一个重载的 stream 运算符

std::ostream &operator<< (std::ostream &out, const NumHold &myNumHold){;}

But I'm a little confused as to the syntax regarding how I would print out these integers from the vector.但是我对如何从向量中打印出这些整数的语法有点困惑。 I also understand that in order for the stream operator to work I would have to add it to my class as a friend, I just wanted to keep my NumHold class as simplified as possible for the examples sake.我也明白,为了让 stream 运算符工作,我必须将它作为朋友添加到我的 class 中,我只是想尽可能简化我的 NumHold class 示例。 Hopefully this was clear enough of an explanation, any help is appreciated希望这是足够清楚的解释,任何帮助表示赞赏

Treat out how you would treat std::cout , then return it at the end of the function:处理你out如何处理std::cout ,然后在 function 的末尾返回它:

std::ostream& operator<<(std::ostream& out, const NumHold &myNumHold){
    //one possible output format
    for(const auto& twoInt : myNumHold.numVector){
        out << twoInt.num1 << ", " << twoInt.num2 << '\n'; //pretend out is std::cout
    }

    return out; //don't forget to return it at the end
}

If appropriate, declare the overload as a friend function within the NumHold class:如果合适,将重载声明为 NumHold class 内的友元 function:

#include<iostream>

class NumHold {
    private:
        std::vector<TwoInts> numVector;

    public:
        //friend operator overload declaration
        friend std::ostream& operator<<(std::ostream& out, const NumHold& myNumHold);
};

In your example, it is appropriate, as the overload function accesses private variables.在您的示例中,这是合适的,因为重载 function 访问私有变量。

You need to be able to access the members of each TwoInts in order to print them.您需要能够访问每个TwoInts的成员才能打印它们。 One way to do this is to make them public.做到这一点的一种方法是将它们公开。

class TwoInts {
public: //lets NumHold access variables to print
    int num1;
    int num2;
};

class NumHold {
public:
    std::vector<TwoInts> numVector;

    friend std::ostream& operator << (std::ostream& out, const NumHold& myNumHold) {
        for (const TwoInts& ti : myNumHold.numVector) {
            out << "(" << ti.num1 << ", " << ti.num2 <<  ")" << std::endl;
        }
        return out;
    }
};

You can also consider adding printing methods to your classes:您还可以考虑将打印方法添加到您的类中:

#include<iostream>

class TwoInts {
    private:
        int num1;
        int num2;
    public:
        void printnum1(){
            std::cout << num1;
        }
        void printnum2(){
            std::cout << num2;
        } 
}


class NumHold {
    private:
         std::vector<TwoInts> numVector;

    public:
         //this function prints the private attributes of the class
         void printnumVector(){
             for (int i = 0; i < numVector.size(); i++){
                 std::cout << numVector[i].printnum1() << " " << numVector[i].printnum2() << std::endl;
             }    
         }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM