最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++容器vector實(shí)現(xiàn)通訊錄功能

 更新時(shí)間:2019年12月18日 10:32:07   作者:zhengqijun_  
這篇文章主要為大家詳細(xì)介紹了C++容器vector實(shí)現(xiàn)通訊錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

之前學(xué)習(xí)C語(yǔ)言的時(shí)候,用鏈表實(shí)現(xiàn)過(guò)通訊錄的基本功能。最近寫(xiě)了一個(gè)C++版本的通訊錄,參考代碼如下所示。

main.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : main.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 16時(shí)47分52秒
Description : 主函數(shù)
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時(shí)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; //用來(lái)選擇哪一個(gè)私有成員!
 
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時(shí)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 << "請(qǐng)輸入新的成員名字:" << endl;
 in >> name;
 p.name_ = name;
 
 cout << "請(qǐng)輸入新的成員地址:" << endl;
 in >> addr;
 p.addr_ = addr;
 
 cout << "請(qǐng)輸入新的成員電話:" << 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時(shí)49分50秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char book_ui()
{
 char ch = 0;
 
 cout << " ____________________________________" << endl;
 cout << "|                  |" << endl;
 cout << "|    歡迎進(jìn)入通訊錄系統(tǒng) 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. 退出通訊錄系統(tǒng)     |" << endl;
 cout << "|____________________________________|" << endl;
 cout << endl << "請(qǐng)輸入你的選擇:" << endl;
 cin >> ch;
 
 return ch;
}

add_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : add_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 17時(shí)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 << "輸入錯(cuò)誤!" << endl;
 return 0;
 }
}

delete_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : delete_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18時(shí)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 << "請(qǐng)輸入你想要查找的方式(1-姓名/2-地址/3-電話):" << endl;
 cin >> d_key;
 
 switch(d_key)
 {
 case 1:
 {
  cout << "請(qǐng)輸入你想要?jiǎng)h除成員的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "請(qǐng)輸入你想要?jiǎng)h除成員的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "請(qǐng)輸入你想要?jiǎng)h除成員的電話:" << 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 << "輸入錯(cuò)誤!" << endl;
 return 0;
 }
}

change_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : change_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18時(shí)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 << "請(qǐng)輸入你想要查找的方式(1-姓名/2-地址/3-電話):" << endl;
 cin >> c_key;
 
 switch(c_key)
 {
 case 1:
 {
  cout << "請(qǐng)輸入你想要修改成員的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "請(qǐng)輸入你想要修改成員的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "請(qǐng)輸入你想要修改成員的電話:" << 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 << "沒(méi)有找到該成員!" << endl;
 }
 
 char ch = 0;
 
 cout << "是否返回主菜單?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'c';
 }
 else
 {
 cout << "輸入錯(cuò)誤!" << endl;
 return 0;
 }
}

find_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : find_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18時(shí)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 << "請(qǐng)輸入查找方式(1-姓名/2-地址/3-電話)" << endl;
 cin >> f_key;
 
 switch(f_key)
 {
 case 1:
 {
  cout << "請(qǐng)輸入你想要查找成員的名字:" << endl;
  cin >> f_info;
  break;
 }
 case 2:
 {
  cout << "請(qǐng)輸入你想要查找成員的地址:" << endl;
  cin >> f_info;
  break;
 }
 case 3:
 {
  cout << "請(qǐng)輸入你想要查找成員的名字:" << 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 << "沒(méi)有找到該成員!" << endl;
 }
 
 char ch = 0;
 
 cout << "是否返回主菜單?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'f';
 }
 else
 {
 cout << "輸入錯(cuò)誤!" << endl;
 return 0;
 }
}

display_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : display_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18時(shí)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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解C++編程中的變量相關(guān)知識(shí)

    詳解C++編程中的變量相關(guān)知識(shí)

    這篇文章主要介紹了詳解C++編程中的變量相關(guān)知識(shí),是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版掃雷的完整過(guò)程

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版掃雷的完整過(guò)程

    這篇文章主要給大家介紹了關(guān)于利用C語(yǔ)言如何實(shí)現(xiàn)簡(jiǎn)易版掃雷的完整過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 詳解C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之棧

    詳解C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之棧

    這篇文章主要為大家介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之棧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • 基于一個(gè)簡(jiǎn)單定長(zhǎng)內(nèi)存池的實(shí)現(xiàn)方法詳解

    基于一個(gè)簡(jiǎn)單定長(zhǎng)內(nèi)存池的實(shí)現(xiàn)方法詳解

    本篇文章對(duì)一個(gè)簡(jiǎn)單定長(zhǎng)內(nèi)存池的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • C語(yǔ)言實(shí)現(xiàn)車輛出租管理系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)車輛出租管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)車輛出租管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C語(yǔ)言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等)

    C語(yǔ)言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等)

    這篇文章主要介紹了C語(yǔ)言中單鏈表的基本操作(創(chuàng)建、銷毀、增刪查改等),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Matlab實(shí)現(xiàn)好看的配對(duì)箱線圖的繪制

    Matlab實(shí)現(xiàn)好看的配對(duì)箱線圖的繪制

    配對(duì)箱線圖,常見(jiàn)于配對(duì)樣本的數(shù)據(jù)分析中,它除了能夠表現(xiàn)兩組的整體差異,還能夠清晰地呈現(xiàn)單個(gè)樣本的前后改變。本文將用Matlab實(shí)現(xiàn)配對(duì)箱線圖的繪制,需要的可以參考一下
    2022-08-08
  • C++使用棧實(shí)現(xiàn)括號(hào)匹配的代碼詳解

    C++使用棧實(shí)現(xiàn)括號(hào)匹配的代碼詳解

    在編程中,括號(hào)匹配是一個(gè)常見(jiàn)問(wèn)題,尤其是在處理數(shù)學(xué)表達(dá)式、編譯器解析等任務(wù)時(shí),棧是一種非常適合處理此類問(wèn)題的數(shù)據(jù)結(jié)構(gòu),能夠精確地管理括號(hào)的匹配問(wèn)題,本文將通過(guò) C++ 代碼,詳細(xì)講解如何使用棧來(lái)實(shí)現(xiàn)括號(hào)匹配,需要的朋友可以參考下
    2025-02-02
  • C++11?constexpr使用詳解

    C++11?constexpr使用詳解

    constexpr是一種比const?更嚴(yán)格的束縛,?它修飾的表達(dá)式本身在編譯期間可知,?并且編譯器會(huì)盡可能的?evaluate?at?compile?time,本文重點(diǎn)給大家介紹C++11?constexpr使用,需要的朋友可以參考下
    2021-12-12
  • C語(yǔ)言系統(tǒng)調(diào)用約定

    C語(yǔ)言系統(tǒng)調(diào)用約定

    這篇文章介紹了C語(yǔ)言系統(tǒng)調(diào)用約定,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。需要的朋友可以收藏下,方便下次瀏覽觀看
    2021-12-12

最新評(píng)論

衡阳市| 新宾| 黑山县| 怀柔区| 罗源县| 伊吾县| 蛟河市| 石景山区| 明水县| 泰兴市| 台北市| 富锦市| 德清县| 宝山区| 金华市| 嘉荫县| 唐海县| 周宁县| 绍兴市| 喀喇| 炉霍县| 腾冲县| 四会市| 南召县| 额济纳旗| 郎溪县| 静乐县| 定西市| 石渠县| 闸北区| 施甸县| 乌拉特后旗| 沧源| 当雄县| 林口县| 石棉县| 庐江县| 宣武区| 甘谷县| 枞阳县| 阳东县|