百战程序员:编程里有一个术语叫做容器,它是什么意思?

百战程序员:编程里有一个术语叫做容器,它是什么意思?,第1张

容器,我们常规的说就是一个器皿,比如,杯子,它可以装水

程序中的容器也是一样,指的就是程序中的器皿,它可以存放数据,

但是程序中的数据是多种多样的,所以容器也就慧多种多样。

但是也有很多万能的容器,比如object,ArrayList,哈西表等等。

//初始数据库是空的,要先执行多次i *** 作才行

//每次按提示输入字符即可,字符的定义是按照你题目的顺序给的。

//在VC上会有很多warning,不过能通过编译

//用g++编译就没有warning

//作者的作品是可以重复的,如果不想重复就用map类好了。

#include <iostream>

#include <map>

#include <string>

using namespace std

// define a new class author's name

class name_t

{

friend ostream &operator<<(ostream &os, name_t &n)

friend istream &operator>>(istream &is, name_t &n)

public:

string data

public:

name_t()

{

}

name_t(const char *c):data(c)

{

}

name_t(const string s):data(s)

{

}

name_t(const name_t &n):data(n.data)

{

}

bool operator<(const name_t &n) const

{

if(data[0] == n.data[0])

return (data <n.data)

else

return (data[0] <n.data[0])

}

bool operator==(const name_t &n) const

{

return (data==n.data)

}

}

ostream &operator <<(ostream &os, name_t &n)

{

os <<n.data

return os

}

istream &operator >>(istream &is, name_t &n)

{

is >>n.data

return is

}

int main()

{

multimap<name_t, string>m

typedef multimap<name_t, string>::iterator m_t

char c

cout <<"* wecome to use my program" <<endl

cout <<"* instructions:" <<endl

cout <<"* a: search an author's work" <<endl

cout <<"* b: delete all of the author's work" <<endl

cout <<"* c: output all the authors and works ordered by the first letter of the author" <<endl

cout <<"* i: insert a new pair of author and work" <<endl

cout <<"* q: quit the program" <<endl

cout <<"now please input flag: "

while(cin >>c) {

if(c == 'a') { // search an author's work

cout <<"please input author's name: " <<flush

name_t author

cin >>author.data

pair<m_t, m_t>pos = m.equal_range(author)

if(pos.first == pos.second) {

cout <<"he has no work" <<endl

} else {

while(pos.first != pos.second) {

cout <<pos.first->second <<endl

++pos.first

}

}

} else if(c == 'b') { // delete all of the author's work

cout <<"please input author's name: " <<flush

name_t author

cin >>author.data

pair<m_t, m_t>pos = m.equal_range(author)

if(pos.first == pos.second) {

cout <<"he has no work" <<endl

} else {

m.erase(pos.first, pos.second)

cout <<"all his works has been deleted" <<endl

}

} else if(c == 'c') { // output all the authors and works ordered by the first letter of the author

m_t mite = m.begin()

while(mite != m.end()) {

cout <<mite->first.data <<": " <<mite->second <<endl

++mite

}

}else if(c == 'i') { // insert a new pair of author and work

cout <<"please input author's name: " <<flush

name_t author

cin >>author.data

cout <<"please input author's work: " <<flush

string work

cin >>work

m.insert(multimap<name_t, string>::value_type(author, work))

} else if(c == 'q') { // quit the program

cout <<"quit now" <<endl

return 0

} else { // illegal input

cout <<"unrecognized char" <<endl

}

cout <<"\nplease input flag: " <<flush

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7931139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存