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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,111 questions

40,781 answers

573 users

How to store a class object in a vector with C++

1 Answer

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

using std::cout;
using std::endl;
using std::vector;

class Test {
	int n;
public:
	Test() { n = 0; }
	Test(int _n) { n = _n; }
	Test &operator=(int _n) {
		n = _n; 
		return *this;
	}
	int get() { return n; }
};

int main()
{
	vector<Test> vec;

	for (int i = 0; i < 5; i++)
		vec.push_back(Test(i * 2));

	for (int i = 0; i < vec.size(); i++)
		cout << vec[i].get() << " ";
	cout << endl;

	for (int i = 0; i < vec.size(); i++)
		vec[i] = vec[i].get() * 5;

	for (int i = 0; i < vec.size(); i++)
		cout << vec[i].get() << " ";
	cout << endl;

	return 0;
}


/*
run:

0 2 4 6 8
0 10 20 30 40

*/

 





answered May 17, 2018 by avibootz

Related questions

...