vector具体使用方法

// Vectors.cpp : Defines the entry point for the console application.
//vector类称作向量类,它实现了动态的数组,用于元素数量变化的对象数值。像数值一样,vector类也用从0开始的下标表示元素的位置;但
//和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大而自动变化

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <iterator>

using namespace std;

class Student{
public:
	string name;
	int age;
	Student(string n, int a) :name(n), age(a){}
};
ostream & operator<<(ostream &os, const Student &s){
	os << s.name << "\t" << s.age;
	return os;
}
class StudentManager{
public:
	vector<Student> S;
	void add(Student&s){
		S.push_back(s);
	}
	//遍历
	void display(){
		vector<Student>::iterator te = S.begin();
		while (te != S.end()){
			cout << *te;
			cout << endl;
			te++;
		}
	}
	void displays(){
		for (int i = 0; i < S.size(); i++){
			cout << S.at(i);
		}
	}
	
};
int _tmain(int argc, _TCHAR* argv[])
{
	//初始化
	Student s1("张三", 19);
	Student s2("李四", 20);
	Student s3("王五", 22);
	Student s4("赵六", 18);
	StudentManager sm;
	sm.add(s1), sm.add(s2), sm.add(s3), sm.add(s4);
	//sm.display();
	sm.displays();
	return 0;
}

 

转载于:https://my.oschina.net/869088067/blog/809746

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值