• 关联容器使用举例


    1. 题目:学习计划进度检测程序
    定义一个vector容器,存储你在未来六个月里要阅读的书籍,再定义一个set,
    用来记录你看过的书名,编写程序从vector中为你选择一本没有读过而现在要读的书。
    当它为你返回选中的书名时,应该讲该书名放入记录已读书目的set中。如果实际上你把
    这本书放在一边没有看,则本程序应该支持从已读书目的set中将该记录删除。
    在虚拟的6个月后,输出已读书目和还没有读的书目。

    考查set容器

    #include<iostream>
    #include<set>
    #include<vector>
    #include<string>
    #include<cstdlib>
    #include<ctime>
    
    using namespace std;
    
    
    int main(){
    	
    	vector<string> books;
    	set<string> readedBooks;
    	string name;
    
    	cout<<"Enter books to read:"<<endl;
    	
    	while(cin>>name){
    		books.push_back(name);
    	}
    
    	cin.clear();
    
    	bool timeOver=false;
    	string answer,bookName;
    	srand((unsigned)time(NULL));
    
    	while(!timeOver && !books.empty() ){
    		//时间未到六个月且还有书没有读过
    		cout<<"Would you like to read a book?(yes/no)"<<endl;
    		cin>>answer;
    		if(anser[0]=='y'||answer[0]=='Y'){
    			int i=rand%books.size();
    			bookName=books[i];
    
    			cout<<"you can read this book: "<<bookName<<endl;
    
    			readedBooks.insert(bookName);
    			books.erase(books.begin()+i);
    
    			cout<<"Did you read it? (yes/no)"<<endl;
    			cin>>answer;
    			if(answer[0]=='n' ||answer[0]=='N'){
    				readedBooks.erase(bookName);
    				books.push_back(bookName);
    			}
    			cout<<"Time is over?(yes/no)"<<endl;
    
    			cin>>answer;
    			
    			if(answer[0]=='Y'|| answer[0]=='y'){
    				timeOver=true;
    
    			}
    		}
    
    	}//?? end of while
    	
    	if(timeOver){
    
    		//输出已经读的书目
    		cout<<" books read:"<<endl;
    		for(set<string>::iter=readedBooks.begin();
    		iter!=readedBooks.end();++iter){
    			cout<< *iter << endl;	
    		}
    
    		//输出还未读的书目
    		cout<<"books not read: "<<endl;
    		for(vector<string>::iterator vit=books.begin();
    		vit!=books.end();++vit){
    			cout<<*vit<<endl;
    		}
    	}else{
    		cout<<"All books have readed "<<endl;
    	}
    
    	return 0;
    }

    2. 题目:通过姓氏查询所有孩子姓名
    定义一个map对象,其元素的键是家族姓氏,而值则是家族孩子的名字的vector对象;
    为这个map容器输入至少六个条目。通过家族姓氏的查询,检测你的程序,
    查询应该输出家族所有孩子的名字。

    考查map容器

    #include<iostream>
    #include<map>
    #include<vector>
    #include<string>
    using namespace std;
    
    int main(){
    	map<string,vector<string> > nameBook;
    	
    	string familyName;
    	string childName;
    	string searchName;
    
    
    	cout<<"input family name:
    ";
    	while(cin>>familyName){
    		cin.clear();
    		//如果姓氏已经存在,则continue
    		if( nameBook.count(familyName) ){
    			cout<<"already in book
    ";
    			continue;
    		}
    		
    		cout<<"input children's name:
    ";
    		while(cin>>childName){
    			nameBook[familyName].push_back(childName);
    			cin.clear();
    		}
    
    	}
    	
    	cout<<"input the family you want to search:
    ";
    
    	cin>>searchName;
    
    /*	if( ! nameBook.count[searchName] )
    		cout<<"not found"<<endl;*/
    	
    	map< string,vector<string> >::const_iterator iter=nameBook.find(searchName);
    
    	if(iter==nameBook.end() )
    		cout<<"not found"<<endl;
    	else{
    		vector<string>::const_iterator iter2=iter->second.begin();
    		while(iter2!=iter->second.end()){
    			cout<<iter->first<<" "<<*iter2<<endl;
    			++iter2;
    		}
    	}
    		
    
    
    	return 0;
    }





  • 相关阅读:
    java 小数点取2位并且四舍五入
    批处理(.bat脚本)基本命令语法
    vue-webpack项目本地开发环境设置代理解决跨域问题
    vue项目引入FastClick组件解决IOS系统下h5页面中的按钮点击延迟,连续点击无反应的问题
    用vue构建多页面应用
    单页面应用和多页面应用对比分析
    html5文件读取+按钮样式重置+文件内容预览
    FileReader读取文件详解
    vue的双向数据绑定实现原理
    译:9.使用Redis进行消息传递
  • 原文地址:https://www.cnblogs.com/sjw1357/p/3864025.html
Copyright © 2020-2023  润新知