关于 Vector容器的使用实例

用vector容器的存储结构写了一篇通讯录;

直接上代码

#pragma once
#include "head.h"
#include <vector>

class User
{
public:
	User();
	~User();
	void Insert();
	void Search();
	void Display();
	void Delete();
	void Work();
private:
	vector<PersonInfor> v_;
};

int chooseInMenu();

#include "user.h"

#include <string>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<ctype.h>

using namespace std;

User::User() 
{
	ifstream infile("book.txt", ios::in);
	if (!infile)
	{
		cerr << "open error" << endl;

		exit(1);
	}
	string name;
	string address;
	string count;
	//PersonInfor tmp;
	
	while (infile >> name >> address >> count)
	{
		PersonInfor tmp(name,address,count);
		v_.push_back(tmp);

		
	}
	
	infile.close();
}

User::~User()
{
	//ofstream outfile("book.txt", ios::out);
	//if (!outfile)
	//{
	//	cerr << "open error" << endl;
	//	exit(1);
	//}
	//vector<PersonInfor>::iterator it;
	//for (it = v_.begin(); it != v_.end();)
	//{

	//	outfile << it->getName() << " ";
	//	outfile << it->getAddress() << " ";
	//	outfile << it->getCount()<< " ";


	//}

	//
	//outfile.close();

}

void User::Insert()
{
	PersonInfor p;
	p.setPerson();
	v_.push_back(p);

}

void User::Display()
{
	vector<PersonInfor>::iterator it;
	for (it = v_.begin(); it != v_.end();++it)
	{
		it->PersonDisplay();
	}
}


void User::Search()
{
	string name;
	int  i = 0;
	cout << "请输入要查找的人的姓名:" << endl;
	cin >> name;

	vector<PersonInfor>::iterator it;
	for (it = v_.begin(); it != v_.end();++it)
	{
		if (it->getName() == name)
		{
			it->PersonDisplay();
			i++;
		}
	}
	if (0 == i)
		cout << "查无此人" << endl;
	
}


void User::Delete()
{
	string name;
	int  i = 0;
	cout << "请输入要删除人的姓名:" << endl;
	cin >> name;

	vector<PersonInfor>::iterator it;
	for (it = v_.begin(); it != v_.end();)
	{
		if (it->getName() == name)
		{
			it->PersonDisplay();
			i++;
			it = v_.erase(it);     
		}
		else {
			++it;
		}
	}
	if (0 == i)
		cout << "查无此人" << endl;
}


void User::Work()
{
	int iChoice;
	int j = 0;

	while (1)
	{
		iChoice = chooseInMenu();
		switch (iChoice)
		{
		case 1:Insert();break;
		case 2:Delete();break;
		case 3:Search();break;
		case 4:Display();break;
		case 0:j = 1;break;
		default:
			break;
		}
		if (j == 1)
			break;
	}
}


int chooseInMenu()
{
	int i;
	while (1)
	{
		cout << endl;
		cout << "+------------------------------------+" << endl;
		cout << "+      1.插入信息        2.删除信息    +" << endl;
		cout << "+      3.查找信息        4.显示信息    +" << endl;
		cout << "+-------------------------------------+" << endl;
		cout << "+                       0.退出        +" << endl;
		cout << "+------------------------------------+" << endl;
		cout << "请输入操作指令:";
		cin >> i;
		if (i >= 0 && i <= 9)
			break;
		else
			cout << "请重新输入" << endl;

	}
	return i;
}


#include "head.h"

PersonInfor::PersonInfor(string name, string address, string count)
{
	name_ = name;
	address_ = address;
	count_ = count;
}


PersonInfor PersonInfor::setPerson()
{

	cout << "请输入姓名:地址:手机号 中间用空格隔开 " << endl;
	cin >> name_ >> address_ >> count_;
	return *this;
}

PersonInfor PersonInfor::PersonDisplay()
{
	cout << "姓名:" << name_ << endl << "地址:" << address_ << endl << "手机号" << count_ << endl;
	return *this;
}

string PersonInfor::getName()
{
	return name_;
}
string PersonInfor::getAddress()
{
	return address_;
}
string PersonInfor::getCount()
{
	return count_;
}

#pragma once
#include "user.h"


int main()
{
	cout << "+-------------------------------------------+" << endl;
	cout << "                     ͨѶ¼                   " << endl;
	cout << "+-------------------------------------------+" << endl;

	User t;

	t.Work();

}  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值