阅读程序(4)

来源:互联网 发布:淘宝快递盒上会有 编辑:程序博客网 时间:2024/05/21 10:27

*Copyright (c) 2014,烟台大学计算机学院

*All right reserved.

*文件名称:test.cpp

*作    者:韩双志

*完成日期:2016年6月20日

*版本号:v1.0

*

*问题描述:理解map

*输入描述:

*输出描述:

/*

#include <algorithm>
#include<map>
#include<iterator>
#include<iostream>
#include<cstring>
using namespace std;
struct ltstr
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) < 0;
    }
};

int main()
{
    map<const char*, int, ltstr> months;

    months["january"] = 31;
    months["february"] = 28;
    months["march"] = 31;
    months["april"] = 30;
    months["may"] = 31;
    months["june"] = 30;
    months["july"] = 31;
    months["august"] = 31;
    months["september"] = 30;
    months["october"] = 31;
    months["november"] = 30;
    months["december"] = 31;

    cout << "june -> " << months["june"] << endl;
    map<const char*, int, ltstr>::iterator cur  = months.find("june");
    map<const char*, int, ltstr>::iterator prev = cur;
    map<const char*, int, ltstr>::iterator next = cur;
    ++next;
    --prev;
    cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
    cout << "Next (in alphabetical order) is " << (*next).first << endl;
    return 0;
}

*/

运行结果

知识点总结

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,

学习心得

   要好好学习

0 0
原创粉丝点击