c++中如何使用vector对自己创建的新类型操作

c++中如何使用vector对自己创建的新类型操作(如Student类)

vector<Student>v;

 

你说的是这样吗?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student {
public:
	Student(string _name, int _age) {
		name = _name;
		age = _age;
	}

	void description() {
		cout << "Name:" << name << "\t" << "Age:" << age << endl;
	}


public:
	string name;
	int age;
};

int main(void) {
	vector<Student> v;

	Student stu1("小明", 20);
	Student stu2("小红", 21);
	Student stu3("小黄", 22);

	v.push_back(stu1);
	v.push_back(stu2);
	v.push_back(stu3);

	for (int i = 0; i < v.size(); i++) {
		v.at(i).description();
	}

	return 0;
}

 

 

用vector<Student>定义一个对象就好了,例如

vector<Student> a,