简体   繁体   中英

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

class TwoInts {
     int num1;
     int num2;
}

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

How would I go about printing my numHold class exactly? I know I would have to provide an overloaded stream operator

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. 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:

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:

#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.

You need to be able to access the members of each TwoInts in order to print them. 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;
             }    
         }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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