• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c++ 搞代码 4年前 (2022-01-06) 25次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了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 person; vector::iterator it; int main() { //personMessage pep; //vector 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  #include  #include  #include  #include  #include  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==(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 person; extern vector::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!" <(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_ >(istream& in, personMessage& p) { string name; string addr; string phone; cout << "请输入新的成员名字:" <> name; p.name_ = name; cout << "请输入新的成员地址:" <> addr; p.addr_ = addr; cout << "请输入新的成员电话:" <> 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 << "请输入你的选择:" <> 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 <> pep; cout << pep << endl; /* 向vector插入元素 */ person.push_back(pep); cout << "插入成员信息成功!" << endl; char ch = 0; cout << "是否返回主菜单?(y/n)" <> 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 Aut<b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>hor    : 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-电话):" <> d_key; switch(d_key) { case 1: { cout << "请输入你想要删除成员的名字:" <> pep_info; break; } case 2: { cout << "请输入你想要删除成员的地址:" <> pep_info; break; } case 3: { cout << "请输入你想要删除成员的电话:" <> pep_info; break; } default: { cout << "输入有误!" <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)" <> 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-电话):" <> c_key; switch(c_key) { case 1: { cout << "请输入你想要修改成员的名字:" <> pep_info; break; } case 2: { cout << "请输入你想要修改成员的地址:" <> pep_info; break; } case 3: { cout << "请输入你想要修改成员的电话:" <> pep_info; break; } default: { cout << "输入有误!" <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)" <> 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-电话)" <> f_key; switch(f_key) { case 1: { cout << "请输入你想要查找成员的名字:" <> f_info; break; } case 2: { cout << "请输入你想要查找成员的地址:" <> f_info; break; } case 3: { cout << "请输入你想要查找成员的名字:" <> f_info; break; } default: { cout << "输入有误!" <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)" <> 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 << "按任意键返回" <> ch; return 0; }

以上就是C++容器vector实现通讯录功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++容器vector实现通讯录功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址