map容器的使用

#include<iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;


int main()
{
bool con=0;
do{
map<string,vector<string> > children;
string surname,childName;

do 
{
cout<<"Enter surname(ctrl+z to end):"<<endl;
cin>>surname;
if(!cin)
break;
vector<string> chd;
pair<map<string, vector<string> >::iterator,bool> ret=
children.insert(make_pair(surname,chd));

if(!ret.second)
{
cout<<"repeated surname:"<<surname<<endl;
continue;
}

cout<<"Enter children's name(ctrl+z to end):"<<endl;
while(cin>>childName)
ret.first->second.push_back(childName);
cin.clear();

} while (cin);

cin.clear();

cout<<"Enter a surname to search:"<<endl;
cin>>surname;

map<string, vector<string> >::iterator iter=
children.find(surname);

if(iter==children.end())
cout<<"no this surname: "<<surname<<endl;
else{
cout<<"children: "<<endl;
vector<string>::iterator it=iter->second.begin();
while(it!=iter->second.end())
cout<<*it++<<endl;
}


cout<<"continue enter 1 else 0: "<<endl;
cin>>con;
}while(con);


return 0;
}
阅读更多
文章标签: iterator search pair
个人分类: C/C++
想对作者说点什么? 我来说一句

SSH增删改查使用Map容器

2009年10月25日 1.3MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭