Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to initialize and print a vector of objects in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>
#include <vector>

class Example {
public:
    Example(int _x, int _y, int _z) {
        x = _x;
        y = _y;
        z = _z;
    }
    void Print() const {
        std::cout << std::setw(3) << x << std::setw(3) << y << std::setw(3) << z << "\n";
    }

private:
    int x, y, z;
};

int main() {
    std::vector<Example> vecobjects = {
        Example( 3,  9,  0),
        Example( 1,  7, 32),
        Example( 7,  6,  4),
        Example( 0, 15, 11),
        Example(12, 16, 99)
    };

    for (int i = 1; i < vecobjects.size(); i++ ) {
        vecobjects[i].Print();
    }
}


/*
run:

  1  7 32
  7  6  4
  0 15 11
 12 16 99

*/

 



answered Oct 8, 2024 by avibootz

Related questions

1 answer 103 views
2 answers 139 views
1 answer 167 views
2 answers 164 views
2 answers 165 views
...