vector的用法以及通讯录的改版

  今天把c++中的vector容器的用法介绍一下,然后我把之前的通讯录改了一下,大家可以参考一下。

1、pop_back()函数

语法

  void pop_back();

pop_back()函数删除当前vector最末的一个元素


2、push_back()函数

语法:

  void push_back( const TYPE &val );

push_back()添加值为val的元素到当前vector末尾


3、begin()函数

语法:

  iterator begin();

begin()函数返回一个指向当前vector起始元素的迭代器.


4、end() 函数

语法:

  iterator end();

end() 函数返回一个指向当前vector末尾元素的下一位置迭代器


5、erase()函数

语法:

  iterator erase( iterator loc );
  iterator erase( iterator start, iterator end );

erase函数要么删作指定位置loc的元素,要么删除区间[start, end)的所有元素.返回值是指向删除的最后一个元素的下一位置的迭代器

6、resize() 函数

语法:

  void resize( size_type size, TYPE val );

resize() 函数改变当前vector的大小为size,且对新创建的元素赋值val


7、swap()函数

语法:

  void swap( vector &from );

swap()函数交换当前vector与vector from的元素


8、insert() 函数

语法:

  iterator insert( iterator loc, const TYPE &val );
  void insert( iterator loc, size_type num, const TYPE &val );
  void insert( iterator loc, input_iterator start, input_iterator end );

insert() 函数有以下三种用法:

  • 在指定位置loc前插入值为val的元素,返回指向这个元素的迭代器,
  • 在指定位置loc前插入num个值为val的元素
  • 在指定位置loc前插入区间[start, end)的所有元素
9、 clear()函数

语法:

  void clear();

clear()函数删除当前vector中的所有元素.


10、 at() 函数

语法:

  TYPE at( size_type loc );

at() 函数 返回当前Vector指定位置loc的元素的引用. at() 函数 比 [] 运算符更加安全, 因为它不会让你去访问到Vector内越界的元素


  先就介绍这几个常用的函数,对于一些基本的操作也够了。下面把我改过的通讯录分享一下:

#ifndef ADD_H
#define ADD_H

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

using namespace std;

class Address_book
{
	int m_ID;
	string m_name;
	string m_tel;
	string m_address;
	string m_company_phone;
public:
	Address_book(){};
	Address_book(int ID, string name, string tel, string address, string company_phone):
	m_ID(ID), m_name(name), m_tel(tel), m_address(address), m_company_phone(company_phone){};
	int getID(){return m_ID;}
	string getname(){return m_name;}
	string gettel(){return m_tel;}
	string getaddress(){return m_address;}
	string getcomphone(){return m_company_phone;}
};

class Control
{
public:
	void show();						//显示最初布局
	int  Init_list();					//添加好友信息
	void Print();						//列表好友信息
	int Search();						//搜索好友
	int Delete();						//删除好友
};

#endif

#include "add.h"

void Control::show()
{
	system("cls"); 
	cout<<"\n\n\n";
	cout<<"\t\t\t**************************************\n";
	cout<<"\t\t\t*           欢迎来到通讯录           *\n";
	cout<<"\t\t\t*          1 添加好友信息            *\n";
	cout<<"\t\t\t*          2 列表好友信息            *\n";
	cout<<"\t\t\t*          3 搜索好友信息            *\n";
	cout<<"\t\t\t*          4 删除好友信息            *\n";
	cout<<"\t\t\t**************************************\n";
	cout<<"\n\n\t\t请输入您的选择:";
}

int ID = 0;

int Control::Init_list()
{
	system("cls");
	ID++;
	string name;
	string tel;
	string address;
	string company_phone;
	
	cout<<"\n\n\n\t\t\t请输入姓名:";
	cin>>name;
	cout<<"\n\t\t\t请输入手机号:";
	cin>>tel;

	int j = 0;
	int len = tel.length();
	
	while(len != 11)                //判断输入的手机号码是不是11位的
	{
		cout<<"\n\t\t\t请输入11位手机号:";
		cin>>tel;
		len = tel.length();
	}

	cout<<"\n\t\t\t请输入家庭住址:";
	cin>>address;
	cout<<"\n\t\t\t请输入家庭号码:";
	cin>>company_phone;

	j = 0;
	len = company_phone.length();
	while(len != 8)             //判断输入的电话号码是不是8位的
	{

		cout<<"\n\t\t\t请输入8位家庭号码:";
		cin>>company_phone;
		len = company_phone.length();
	}

	Address_book *one = new Address_book(ID,name,tel,address,company_phone);

	ofstream out;
	out.open("电话本.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error!"<<endl;
		return -1;
	}
	out.write((char*)one, sizeof(Address_book));
	
	out.close();

	return 0;
}

void Control::Print()
{
	system("cls"); 
	cout<<"\n\n\n";
	ifstream in;
	in.open("电话本.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return;
	}

	Address_book *one = new Address_book;
	vector<Address_book> addr;
	vector<Address_book>::iterator it;
	
	
	if (in.read((char*)one, sizeof(Address_book)))
	{
		addr.push_back(*one);
		while(1)
		{
			memset(one, 0, sizeof(one));
			if (in.read((char*)one, sizeof(Address_book)))
			{
				addr.push_back(*one);
				continue;
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return;
	}
	in.close();

	for (it = addr.begin(); it != addr.end(); it++)
	{
		cout<<"NO "<<it->getID()<<"  姓名:"<<it->getname()<<" 手机号:"<<it->gettel()<<
			" 家庭住址:"<<it->getaddress()<<" 家庭号码:"<<it->getcomphone()<<endl;
	}
}

int Control::Search()
{
	system("cls");
	cout<<"\n\n\n";

	int NUM;
	cout<<"\t\t\t请输入要查找人的序号:";
	cin>>NUM;

	ifstream in;
	in.open("电话本.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return -1;
	}

	Address_book *one = new Address_book;
	vector<Address_book> addr;
	vector<Address_book>::iterator it;


	if (in.read((char*)one, sizeof(Address_book)))
	{
		addr.push_back(*one);
		while(1)
		{
			memset(one, 0, sizeof(one));
			if (in.read((char*)one, sizeof(Address_book)))
			{
				addr.push_back(*one);
				continue;
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return -1;
	}
	in.close();

	int flag = 0;
	for (it = addr.begin(); it != addr.end(); it++)
	{
		if (it->getID() == NUM)
		{
			flag = 1;
			cout<<"\nNO "<<it->getID()<<"  姓名:"<<it->getname()<<" 手机号:"<<it->gettel()<<
				" 家庭住址:"<<it->getaddress()<<" 家庭号码:"<<it->getcomphone()<<endl;
		}
	}

	if(flag == 0)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
		return -1;
	}

	return 0;
}

int Control::Delete()
{
	system("cls");
	cout<<"\n\n\n";

	int NUM, flag = 0;
	cout<<"\t\t\t请输入要删除人的序号:";
	cin>>NUM;

	ifstream in;
	in.open("电话本.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return -1;
	}

	Address_book *one = new Address_book;
	vector<Address_book> addr;
	vector<Address_book>::iterator it;


	if (in.read((char*)one, sizeof(Address_book)))
	{
		addr.push_back(*one);
		while(1)
		{
			memset(one, 0, sizeof(one));
			if (in.read((char*)one, sizeof(Address_book)))
			{
				addr.push_back(*one);
				continue;
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t通讯录中暂时无人!"<<endl;
		in.close();
		return -1;
	}
	in.close();

	if (addr.begin() == addr.end())				//只有一个元素
	{
		remove("电话本.txt");
		ofstream outfile;
		outfile.open("电话本.txt", ios::out);
		outfile.close();

		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
		return -1;
	}

	for (it = addr.begin(); it != addr.end(); it++)
	{
		if(it->getID() == NUM)
		{
			flag = 1;
			addr.erase(it);
			break;
		}
	}

	if(flag)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
	}
	else
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
		return -1;
	}

	remove("电话本.txt");
	ofstream out;
	out.open("电话本.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error"<<endl;
		return -1;
	}

	for (it = addr.begin(); it != addr.end(); it++)
	{
		Address_book w(it->getID(), it->getname(), it->gettel(), it->getaddress(), it->getcomphone());
		out.write((char*)&w, sizeof(Address_book));
		memset(&w, 0, sizeof(Address_book));
	}
	out.close();
	return 0 ;
}

/**********************************************************
File Name:         通讯录项目(文件保存)
Author:                     Date:
Description: 实现通讯录功能
Fuction List:       show()   	 显示最初布局
					Init_list()	 添加好友信息
					Print()		 列表好友信息
					Search()	 搜索好友
					Delete() 	 删除好友
************************************************************/
#include "add.h"

int main()
{
	string option;
	string sel;
	Control control;

	while(1)
	{
		control.show();
		cin>>option;

		switch(option[0])
		{
		case '1':
			{
				control.Init_list();
				system("cls");
				cout<<"\n\n\n\n\n\n\t\t\t添加联系人成功!";
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		case '2':
			{
				control.Print();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		case '3':
			{
				control.Search();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		case '4':
			{
				control.Delete();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		default:
			{
				system("cls");
				cout<<"\n\n\n\n\n请输入1--4!"<<endl;
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		}
	}
}








  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值