C++容器vector实现通讯录功能

之前学习C语言的时候,用链表实现过通讯录的基本功能。最近写了一个C++版本的通讯录,参考代码如下所示。

main.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : main.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 16时47分52秒
Description : 主函数
Funcion List : main()
*****************************************************/

#include "../../include/head.h"

personMessage pep;
vector<personMessage> person;
vector<personMessage>::iterator it;

int main()
{
 //personMessage pep;
 //vector<personMessage> person;

 char ch = 0;

 //system("clear");

 while(ch != 'q')
 {
 if((ch != 'a') && (ch != 'c') && (ch != 'd') && (ch != 'f'))
 {
  system("clear");
  ch = book_ui();
 }

 switch(ch)
 {
      case 'a':
  {
  ch = add_person();
  break;
  }
  case 'c':
  {
  ch = change_person();
  break;
  }
  case 'd':
  {
  ch = delete_person();
  break;
  }
  case 'e':
  {
  ch = display_person();
  break;
  }
  case 'f':
  {
  ch = find_person();
  break;
  }
  case 'q':
  {
  cout << "Byebye!" << endl;
  return 0;
  break;
  }
  default:
  {
  cout << "input error!" << endl;
  break;
  }
 }
 }

  return 0;
}

head.h

/*****************************************************
Copyright (C): 2017-2018
File name  : head.h
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 17时11分29秒
Description :
Funcion List :
*****************************************************/

#ifndef __HEAD_H__
#define __HEAD_H__

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

#include <stdio.h>
#include <string.h>

using namespace std;

class personMessage
{
public:
 personMessage();
 personMessage(string s);
 ~personMessage();

 personMessage& operator=(string s);
 personMessage& operator=(personMessage& other);

 /* sort排序算法需要重载'<',注意加const! */
 bool operator<(const personMessage& p) const;
 bool operator>(const personMessage& p) const;
 bool operator<=(const personMessage& p) const;
 bool operator>=(const personMessage& p) const;

 bool operator==(string s);

 friend istream& operator>>(istream& in, personMessage& p);
 friend ostream& operator<<(ostream& out, personMessage& p);

 int selectFlag; //用来选择哪一个私有成员!

private:
 string name_;
 string addr_;
 string phone_;
};

extern personMessage pep;
extern vector<personMessage> person;
extern vector<personMessage>::iterator it;

extern char book_ui();
extern char add_person();
extern char change_person();
extern char delete_person();
extern char display_person();
extern char find_person();

#endif

book.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : book.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时53分19秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

personMessage::personMessage() : selectFlag(0)
{
 cout << "default coonstructor!" << endl;
}

personMessage::personMessage(string s)
{
 name_ = s;
}

personMessage::~personMessage()
{
 cout << "destroy person message!" << endl;
}

#if 1
personMessage& personMessage::operator=(string s)
{
 name_ = s;
 return *this;
}
#endif

personMessage& personMessage::operator=(personMessage& other)
{
 if(this == &other)
 {
 return *this;
 }

 name_ = other.name_;
 addr_ = other.addr_;
 phone_ = other.phone_;
 return *this;
}

bool personMessage::operator>(const personMessage& p) const
{
 return name_ > p.name_;
}

bool personMessage::operator>=(const personMessage& p) const
{
 return name_ >= p.name_;
}

bool personMessage::operator<(const personMessage& p) const
{
 return name_ < p.name_;
}

bool personMessage::operator<=(const personMessage& p) const
{
 return name_ <= p.name_;
}

bool personMessage::operator==(string s)
{
 if(selectFlag == 1)
 {
 return name_ == s;
 }
 else if(selectFlag == 2)
 {
 return addr_ == s;
 }
 else if(selectFlag == 3)
 {
 return phone_ == s;
 }
 else
 {
 return false;
 }
}

#if 1
istream& operator>>(istream& in, personMessage& p)
{
 string name;
 string addr;
 string phone;

 cout << "请输入新的成员名字:" << endl;
 in >> name;
 p.name_ = name;

 cout << "请输入新的成员地址:" << endl;
 in >> addr;
 p.addr_ = addr;

 cout << "请输入新的成员电话:" << endl;
 in >> phone;
 p.phone_ = phone;

 return in;
}

ostream& operator<<(ostream& out, personMessage& p)
{
 out << "名字: " << p.name_ << endl;
 out << "地址: " << p.addr_ << endl;
 out << "电话: " << p.phone_ << endl;

 return out;
}
#endif

book_ui.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : book_ui.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 16时49分50秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char book_ui()
{
 char ch = 0;

 cout << " ____________________________________" << endl;
 cout << "|                  |" << endl;
 cout << "|    欢迎进入通讯录系统 v2.0   |" << endl;
 cout << "|                  |" << endl;
 cout << "|====================================|" << endl;
 cout << "|                  |" << endl;
 cout << "|     a. 增加新的成员      |" << endl;
 cout << "|     c. 修改成员信息      |" << endl;
 cout << "|     d. 删除成员信息      |" << endl;
 cout << "|     e. 展示所有成员      |" << endl;
 cout << "|     f. 查找成员信息      |" << endl;
 cout << "|     q. 退出通讯录系统     |" << endl;
 cout << "|____________________________________|" << endl;
 cout << endl << "请输入你的选择:" << endl;
 cin >> ch;

 return ch;
}

add_person.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : add_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 17时22分56秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char add_person()
{
 cout << "This is add person!" << endl;

#if 0
 getchar();
 string tmp;

 getline(cin, tmp);

 cout << "tmp = " << tmp << endl;

 pep = tmp;
#endif

 /* 输入新的成员信息 */
 cin >> pep;
 cout << pep << endl;

 /* 向vector插入元素 */
 person.push_back(pep);

 cout << "插入成员信息成功!" << endl;

 char ch = 0;

 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;

 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'a';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

delete_person.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : delete_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时29分33秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char delete_person()
{
 cout << "This is delete person!" << endl;

 /* 删除成员的信息 */
 string pep_info;

 int d_flag = 0;
 int d_key = 0;

 cout << "请输入你想要查找的方式(1-姓名/2-地址/3-电话):" << endl;
 cin >> d_key;

 switch(d_key)
 {
 case 1:
 {
  cout << "请输入你想要删除成员的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要删除成员的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要删除成员的电话:" << endl;
  cin >> pep_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }

 for(it = person.begin(); it != person.end(); )
 {
 it->selectFlag = d_key;
 if(*it == pep_info)
 {
  person.erase(person.begin()+d_flag, person.begin()+d_flag+1);
  cout << "删除成员信息成功!" << endl;
 }
 else
 {
  ++it;
  d_flag++;
 }
 }

 char ch = 0;

 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;

 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'd';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

change_person.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : change_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时20分15秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char change_person()
{
 cout << "This is change person!" << endl;

 /* 修改成员的信息 */
 string pep_info;

 int ch_flag = 0;
 int c_key = 0;

 cout << "请输入你想要查找的方式(1-姓名/2-地址/3-电话):" << endl;
 cin >> c_key;

 switch(c_key)
 {
 case 1:
 {
  cout << "请输入你想要修改成员的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要修改成员的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要修改成员的电话:" << endl;
  cin >> pep_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }

 for(it = person.begin(); it != person.end(); ++it)
 {
 it->selectFlag = c_key;
 if(*it == pep_info)
 {
  ch_flag = 1;
  cin >> *it;
  cout << "修改成员信息成功!" << endl;
 }
 }

 if(ch_flag != 1)
 {
 cout << "没有找到该成员!" << endl;
 }

 char ch = 0;

 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;

 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'c';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

find_person.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : find_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时21分59秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char find_person()
{
 cout << "This is find person!" << endl;

 int f_key = 0;
 int f_flag = 0;
 /* 输入查找的姓名 */
 string f_info;

 cout << "请输入查找方式(1-姓名/2-地址/3-电话)" << endl;
 cin >> f_key;

 switch(f_key)
 {
 case 1:
 {
  cout << "请输入你想要查找成员的名字:" << endl;
  cin >> f_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要查找成员的地址:" << endl;
  cin >> f_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要查找成员的名字:" << endl;
  cin >> f_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }

 //pep.selectFlag = 2; //it迭代器在变化,不能直接赋值。

 for(it = person.begin(); it != person.end(); ++it)
 {
 it->selectFlag = f_key;
 if(*it == f_info)
 {
  f_flag = 1;
  cout << "找到该成员!" << endl;
  cout << *it << endl;
 }
 }

 if(f_flag != 1)
 {
 cout << "没有找到该成员!" << endl;
 }

 char ch = 0;

 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;

 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'f';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

display_person.cpp

/*****************************************************
Copyright (C): 2017-2018
File name  : display_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时23分04秒
Description :
Funcion List :
*****************************************************/

#include "../../include/head.h"

char display_person()
{
 cout << "This is display person!" << endl;

 sort(person.begin(), person.end());

 for(it = person.begin(); it != person.end(); ++it)
 {
 cout << *it << endl;
 }

 char ch = 0;
 cout << "按任意键返回" << endl;
 getchar();
 cin >> ch;
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C++实现简单通讯录

    本文实例为大家分享了C++实现简单通讯录的具体代码,供大家参考,具体内容如下 说明: 1 程序中运用到两个类,一个是Person类,另一个是List类.前者存储用户信息,后者主要用于操作,如增删改查等.但由于本程序中没有涉及到太复杂的功能,用户信息可以由一个简单的结构体表示,但是为了以后拓展方便,和达到学习运算符重载的目的,还是使用了类. 2 List类中的Reflush()方法用户刷新文件内容,即每次修改了vector后要将最新内容写入到文件.因此增删改操作中都要调用该操作,这种方法在数据库开

  • C++链表实现通讯录管理系统

    用数据结构里面线性结构的链表实现,供大家参考,具体内容如下 文件操作未写 有登录操作,复制源码需要更改登录模块的密码文件存放位置 使用VS2017编译器需要保留开头: #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include "iostream" #include "cstdio" #include "fstream" #include "stdli

  • C++实现通讯录管理系统

    本文实例为大家分享了C++通讯录管理系统的具体代码,供大家参考,具体内容如下 #include<iostream> #include<string> using namespace std; #define MAX 1000 struct Person { string m_Name; int m_Sex; int m_Age; string m_Phone; string m_Addr; }; struct Addressbooks { struct Person personA

  • C++容器vector实现通讯录功能

    之前学习C语言的时候,用链表实现过通讯录的基本功能.最近写了一个C++版本的通讯录,参考代码如下所示. main.cpp /***************************************************** Copyright (C): 2017-2018 File name : main.cpp Author : Zhengqijun Date : 2017年02月12日 星期日 16时47分52秒 Description : 主函数 Funcion List : ma

  • C++(STL库)之顺序容器vector的使用

    一.特点 ①总的来说:可变大小数组.支持快速随机访问.在尾部之外的位置插入或删除元素可能很慢 ②元素保存在连续的内存空间中,因此通过下标取值非常快 ③在容器中间位置添加或删除元素非常耗时 ④一旦内从重分配,和原vector相关的指针,引用,迭代器都失效.内存重分配耗时很长 二.头文件.using声明 头文件:#include <vector> using声明:using std::vector; 三.初始化 vector<T>  v1; ==>v1是一个空的vector ve

  • C++顺序容器(vector、deque、list)的使用详解

    目录 一:STL(Standard Template Library),即标准模板库,是一个高效的C++程序库 二:STL组件 三:容器 四:类型成员 五:迭代器 六:顺序容器 七:顺序容器--向量(vector) 八:顺序容器--双端队列--deque 九:顺序容器 --列表--list 一:STL(Standard Template Library),即标准模板库,是一个高效的C++程序库 1.从实现层次看,整个STL是以一种类型参数化(type parameterized)的方式实现的,基

  • C++利用容器查找重复列功能实现

    复制代码 代码如下: # include <vector> # include <iostream> # include <set> using namespace std; int main(int argc, char * argv[]) { vector<int> v; //找一些数据来测试 for (int i = 0; i < 50; i++) v.push_back(rand() % 25); for (int i = 0; i <

  • C语言实现通讯录功能

    本文实例为大家分享了C语言实现通讯录功能的具体代码,供大家参考,具体内容如下 先定义头文件 #ifndef __CONTACT_H__ #define __CONTACT_H__ #define NAME_LEN 10 #define SEX_LEN 6 #define TELE_LEN 12 #define ADDR_LEN 20 #define MAX_PEO 1000 typedef struct PEO { char name[NAME_LEN]; int age; char sex[S

  • vue实现通讯录功能

    vue实现手机通讯录功能,供大家参考,具体内容如下 <!DOCTYPE html> <html> <head> <title>动态加载组件</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maxi

  • 微信小程序仿通讯录功能

    本文实例为大家分享了微信小程序实现通讯录功能的具体代码,供大家参考,具体内容如下 微信小程序模仿通讯录功能需要用到scroll-view标签 思路:首先需要获取到你所需要展示的数据样式的高度(这就需要用到微信给我们提供的一个API来完成了,因为小程序是没有DOM树结构的,这个可以去看我的前一篇里面有详细的记载怎么获取想要的元素的宽高.),然后组合成一个高度数组(便于后面根据这个数组进行判断),再获取滚动距离,用这两个比较判断之后就可以得出滚动的时候右边选中的字母了,然后再利用scroll-vie

  • JavaScript实现通讯录功能

    本文实例为大家分享了JavaScript实现通讯录功能的具体代码,供大家参考,具体内容如下 直接贴代码 index.css BODY, HTML { width: 100%; height: 100%; margin: 0px; font-family: "PingFang SC", "微软雅黑", sans-serif; font-weight: 300; color: #333; } .header { width: 100%; padding: 32px; }

  • Java基础之容器Vector详解

    一.前言 知识补充:Arrays.copyOf函数: public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; } 可见copyOf()在内部新建一个数组,调用arrayCopy()将ori

  • C语言基于单链表实现通讯录功能

    本文实例为大家分享了C语言基于单链表实现通讯录功能的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> #include<string.h> #pragma warning(disable:4996)://解决VS报严重性代码错误 typedef struct LNode { char name[20]; double ph_number; struct LNode* next; }LinkNode; //创建通

随机推荐