C++标准模板库学习(二)---map的初步使用

map相当于建立了一个键值对的映射,剩下的跟vector有些类似。但是map中的key值是不允许重复的。

今天学习了map的增删改查操作。

#include 

#include 

//map的示例
using namespace std;

int main()
{
    //map的功能是自动建立key值到value值的对应
    //查找的复杂度是log(N)

    //定义map,map中的key值不允许有重复!!!
    map map_sample;

    //使用模板类进行类型定义
    typedef map MAP_SAMPLE_INT_CHAR;
    MAP_SAMPLE_INT_CHAR map_sample2;

    //插入元素
    map_sample2.insert(map::value_type(1,'a'));
    map_sample2.insert(map::value_type(2,'b'));
    cout<<"共有元素个数:"<::iterator it = map_sample2.find(1);
    if(it==map_sample2.end())
    {
        cout<<"没有找到!"<second<second<second = 'c';//直接修改
    }

    it = map_sample2.find(2);
    if(it==map_sample2.end())
    {
        cout<<"没有找到!"<second<


你可能感兴趣的:(技术工具)