Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


8,090 questions

11,268 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 by avibootz
...