BUPT 数据结构与算法导论---使用vector实现顺序表

使用vector实现顺序表

#include <iostream>
#include <vector>
using namespace std;
class PHONEBOOK
{
private:
	int m_ID;
	string m_name;
	string m_phone;
	string m_group;

public:
	PHONEBOOK() {}
	PHONEBOOK(int id, string name, string phone, string group)
	{
		m_ID = id;
		m_name = name;
		m_phone = phone;
		m_group = group;
	}
	void print()
	{
		cout << m_ID << '\t' << m_name << '\t' << m_phone << '\t' << m_group << endl;
	}
	bool operator==(PHONEBOOK& p) //重载==运算符, 从而使locate()函数可用
	{
		if (p.m_ID == m_ID)
			return true;
		return false;
	}
};
int main()
{
	vector<PHONEBOOK> vec;
	PHONEBOOK pbook[4] = {
		{20181208, "Mary", "13011221827", "classmates"},
		{20181127, "Tom", "13934621123", "family"},
		{20181156, "John", "1324579880", "classmates"},
		{20181123, "Lisa", "1378001822", "clsaamates"} };
	for (int i = 0; i < 4; i++)
		vec.push_back(pbook[i]);  //使用数组方式访问每个元素
	cout << "通信录内容列表:" << endl;
	for (int i = 0; i < vec.size(); i++)
		vec[i].print();
	PHONEBOOK record(20181209, "phoneix", "1593020920", "teacher");
	vec.insert(vec.begin(), record);
	cout << "通信录内容列表:" << endl;
	for (vector<PHONEBOOK>::iterator it = vec.begin(); it != vec.end(); it++)
		(*it).print(); //使用迭代器访问每个元素
	cout << "删除元素:" << endl;
	vec.erase((++ ++vec.begin()))->print();  //删除第三位置的元素
	cout << "通信录内容列表:" << endl;
	for (vector<PHONEBOOK>::iterator it = vec.begin(); it != vec.end(); it++)
		(*it).print(); //使用迭代器访问每个元素
	cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值