zoukankan      html  css  js  c++  java
  • 第13章 map映照容器

    /*
    
      第13章 map映照容器
       13.1 map技术原理
       13.2 map应用基础
       13.3 本章小结
    
    */
    
    
    //  第13章 map映照容器
    //   13.1 map技术原理 ---------------------------------------------------------------------------------
    
    //   13.2 map应用基础 ---------------------------------------------------------------------------------
    
    //202 [] 插入
    #include <map>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      //创建map容器对象m
      map < const char *, float > m;
      //插入元素(水果,单价)
      m["apple"] = 3.6f;
      m["orange"] = 3.2f;
      m["banana"] = 1.8f;
      m["pear"] = 2.3f;
      m["lichee"] = 6.3f;
      //打印元素
      cout << "苹果价格: " << m["apple"] << "元/斤
    ";
      cout << "桔子价格: " << m["orange"] << "元/斤
    ";
      cout << "香蕉价格: " << m["banana"] << "元/斤
    ";
      cout << "雪梨价格: " << m["pear"] << "元/斤
    ";
      cout << "荔枝价格: " << m["lichee"] << "元/斤
    ";
      return 0;
    }
    
    
    
    //203
    #include <map>
    #include <iostream>
    struct StudentInfo
    {
      //学生信息结构体
      char *name;
      int year;
      char *addr;
    };
    struct StudentRecord
    {
      //学生记录结构体
      int id; //学号,作键值
      StudentInfo sf; //学生信息,作映照数据
    };
    
    int main(void)
    {
      using namespace std;
      //学生数据
        StudentRecord srArray[] = {  //3笔学生记录
                    { 1, "李强", 21, "北京" },
                    { 2, "王文", 29, "上海" },
                    { 3, "张三", 38, "深圳" }
        };
      //创建map容器对象m,管理学生记录
      map < int, StudentInfo > m;
      //装入3笔学生记录
      for(int j = 0; j < 3; j++)
        m[srArray[j].id] = srArray[j].sf;
      //反向迭代器遍历元素
      map < int, StudentInfo > ::reverse_iterator i, iend;
      iend = m.rend();
      cout << "学号    " << "姓名    " << "年龄    " << "地址    " << endl;
      for(i = m.rbegin(); i != iend; i++)
        cout << (*i).first << '    ' << (*i).second.name << '    ' << (*i).second.year <<
          '    ' << (*i).second.addr << '    ' << endl;
      return 0;
    }
    
    
    /*
    The pair::second element in the pair is set to true if a new element was inserted 
    or false if an element with the same value existed.
    */
    //206
    
    #include <map>
    #include <iostream>
    struct StudentRecord
    {
      //学生记录结构体
      struct StudentInfo
      {
        char *name;
        int year;
        char *addr;
      };
      StudentRecord(int id_, char *name_, int year_, char *addr_)
      {
        id = id_;
        sf.name = name_;
        sf.year = year_;
        sf.addr = addr_;
      }
      int id; //学号,作键值
      StudentInfo sf; //其他信息
    };
    
    int main(void)
    {
      using namespace std;
      //创建map容器对象m
      typedef map < int, StudentRecord::StudentInfo > studentmap;
      studentmap m;
      pair < studentmap::iterator, bool > p;
      //插入第一条学生记录
      StudentRecord student1 = StudentRecord(1, "焦焦", 21, "北京");
      pair < int, StudentRecord::StudentInfo > 
        pairStudent1(student1.id, student1.sf);
      p = m.insert(pairStudent1);
      if(!p.second)
        cout << "插入学生记录失败:
    " << student1.id << '    ' << student1.sf.name <<
          '    ' << student1.sf.year << '    ' << student1.sf.addr << '    ' << endl << endl;
      //插入第二条学生记录
      StudentRecord student2 = StudentRecord(2, "敦介", 18, "上海");
      pair < int, StudentRecord::StudentInfo > pairStudent2(student2.id,
        student2.sf);
      p = m.insert(pairStudent2);
      if(!p.second)
        cout << "插入学生记录失败:
    " << student2.id << '    ' << student2.sf.name <<
          '    ' << student2.sf.year << '    ' << student2.sf.addr << '    ' << endl << endl;
      //插入第三条学生记录
      StudentRecord student3 = StudentRecord(3, "译尹", 20, "深圳");
      pair < int, StudentRecord::StudentInfo > pairStudent3(student3.id,
        student3.sf);
      p = m.insert(pairStudent3);
      if(!p.second)
        cout << "插入学生记录失败:
    " << student3.id << '    ' << student3.sf.name <<
          '    ' << student3.sf.year << '    ' << student3.sf.addr << '    ' << endl << endl;
      //插入键值重复的学生记录,失败
      StudentRecord student4 = StudentRecord(3, "李强", 26, "天津");
      pair < int, StudentRecord::StudentInfo > pairStudent4(student4.id,
        student4.sf);
      p = m.insert(pairStudent4);
      if(!p.second)
        cout << "插入学生记录失败:
    " << student4.id << '    ' << student4.sf.name <<
          '    ' << student4.sf.year << '    ' << student4.sf.addr << '    ' << endl << endl;
      //记录搜索
      studentmap::iterator i = m.find(2);
      cout << "搜索出学号为2的记录:
    " << (*i).first << '    ' << (*i).second.name <<
        '    ' << (*i).second.year << '    ' << (*i).second.addr << '    ' << endl << endl;
      return 0;
    }
    
    
    
    //   13.3 本章小结 ---------------------------------------------------------------------------------

    TOP

  • 相关阅读:
    手把手教你封装属于自己的分段滚动视图(上)
    从 setNeedsLayout 说起
    cocoapods使用指南
    神奇的 BlocksKit(1):源码分析(下)
    Web应用开发中的几个问题
    Jquery Ajax自定义无刷新提交表单Form
    解耦HTML、CSS和JavaScript
    通过预加载器提升网页加载速度
    巧妙使用CSS创建可以打印的页面
    有用的JavaScript开发小建议
  • 原文地址:https://www.cnblogs.com/xin-le/p/4111417.html
Copyright © 2011-2022 走看看