c++_primer_exercise_113132

write a program that defines a multimap of authors and their works. Use find to find an element in the multimap and erase that element. Be sure your program works correctly if the element you look for is not in the map. Print the list of authors and their works alphabetically.

/*****************************************
 * IDE: vs2010
 *****************************************/
#include 
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       	// for make_pair

using namespace std;

/************* test data ******************
	adds elements to authors:
1 a b c next
2 c d e next
3 f g h i next
4 j next
5 k l m n
	input search_item:
2 (if not 1-5, output warning)
	after erase:
1 a b c
2 d e
3 f g h i
4 j
5 k l m n
 ******************************************/

int main(void)
{
	multimap
       
       
         authors; string author, work; // separate author from authors // adds elements to authors: // input authors and their works, separate authors with "next" // stop input with "Ctrl + Z" while (cin >> author) { while (cin >> work) { if (work == "next") { break; } authors.insert(make_pair(author, work)); } } cin.clear(); // make cin valid // input the author you want to erase string search_item; cin >> search_item; // the type of pos is multimap 
        
          ::iterator auto pos = authors.find(search_item); if (pos->first != search_item) { cout << "warning::not found the author" << endl; } else { authors.erase(pos); } // print the list of authors and their works alphabetically // when using iterator traverse authors, only support ++/-- auto iter = authors.begin(); bool same_author = false; while (iter != authors.cend()) { if (same_author == false) { same_author = true; cout << "author: " << iter->first << " ,works: "; } cout << iter->second << " "; auto it = iter++; if (iter != authors.cend() && it->first != iter->first) { same_author = false; cout << endl; } } return 0; } 
         
       
      
      
     
     
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值