C++STL库中map容器常用应用

#include<iostream>
#include<cstdio>
#include<map> //按键值大小构成二叉搜索树
using namespace std;
map<int, string> a;
int main()
{
    a.insert(map<int, string>::value_type(1,"li"));
    a.insert(map<int, string>::value_type(1,"LI"));//键值存在,插入失败
    a.insert(pair<int, string>(2, "yang"));
    a.insert(pair<int, string>(2, "YANG"));//键值存在,插入失败
    a[3]="wang";
    a[3]="WANG";//键值存在,进行覆盖
    a.insert(make_pair(4,"dong"));
    a.insert(make_pair(4,"DONG"));//键值存在,插入失败
    map<int, string>::iterator it;
    for(it=a.begin(); it!=a.end(); it++)
    {
        cout<<it->first<<"   "<<it->second<<endl;
    }

    if(a.find(1)!=a.end())
    {
        cout<<"find success!"<<endl;
    }
    else
    {
        cout<<"losing finding!"<<endl;
    }

    if(a.count(1)==true)
    {
        cout<<"find success!"<<endl;
    }
    else
    {
        cout<<"losing find! "<<endl;
    }
    cout<<"map中元素个数为"<<a.size()<<endl;
    a.erase(a.begin(),a.end());
    if(a.empty())
    {
        cout<<"map is empty"<<endl;
    }
    else
    {
        cout<<"map is not empty"<<endl;
    }
    return 0;
}
时间: 2024-08-29 21:46:58

C++STL库中map容器常用应用的相关文章

C++STL库中vector容器常用应用

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { vector<int> vec; vec.push_back(1);//在尾部插入元素 vec.push_back(2); // cout<<vec[1];//按下标访问元素,从[0]开始 /* //使用迭代器访问元素 vector<int>::iterat

C++STL 库中set容器应用

#include<iostream> #include<cstdio> #include<set> using namespace std; set<int> a; int main() { //插入元素 a.insert(1); a.insert(3); a.insert(5); //用迭代器遍历容器; set<int>::iterator it; for(it=a.begin(); it!=a.end(); it++) { cout<&

【转】C++中map容器的说明和使用技巧

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明    1   头文件   #include   <map>     2   定义   map<string,   int>   my_Map;   或者是typedef     map<string,   int>   MY_MAP;   MY_MAP   my_Map;     3   插入数据   (1)   my_Map["

C++中map容器的使用说明

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明 1.头文件 #include<map.h> 2.定义方法 (1)map<string,int> m; (2)typedef map<string,int> M; M m; 3.插入数据 (1)m['a'] = 1 (2)m.insert(map<string,int>::value_type("b",2));

sort在STL库中是排序函数

sort在STL库中是排序函数,有时冒泡.选择等O(N^2)算法会超时时,我们可以使用STL中的快速排序O(N log N)完成排序 sort在<algorithm>库里面,原型如下: 1 2 3 4 template <class RandomAccessIterator>  void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIte

STL中map容器使用自定义key类型报错详解

引言 STL的map容器中,key的类型是不是随意的呢? 实践 编写测试代码 定义一个结构体来试试: struct a { char* pName; int m_a; }; map<a, int> mp; a a1; a1.m_a = 100; a1.pName = "a1"; a a2; a2.m_a = 200; a2.pName = "a2"; mp.insert(std::make_pair(a1, 1)); mp.insert(std::mak

《深入实践C++模板编程》之六——标准库中的容器

1.容器的基本要求 a.并非所有的数据都可以放进容器当中.各种容器模板对所存数据类型都有一个基本要求——可复制构造.将数据放进容器的过程就是通过数据的复制构造函数在容器内创建数据的一个副本的过程. b.容器中必须有若干与所存数据类型有关的嵌套定义类型. C::value_type 容器所存数据类型 C::reference 容器数据的引用类型 C::const_reference 容器数据的只读引用类型 C::size_type 容器容量类型,通常是一个无符号整数类型 c.除嵌套类型定义外,容器

STL库中简单的list类模板示例和一个小的延时程序

先贴代码: #include "stdafx.h" #include <iostream> #include <list> #include <ctime> using namespace std; void mysleep(int second) { clock_t st; st=clock();//该程序从启动到函数调用占用CPU的时间 while(clock()-st<second*CLOCKS_PER_SEC);//#define CL

STL库中的正态分布函数

在设计抽奖一类程序中,有时会需要一种概率“有较大可能获得一个普通结果,有较小可能获得一个糟糕或极好的结果”,这就可以用正态分布函数来获得这样一个结果. STL中已经提供了一系列随机分布的函数,包括正态分布,泊松分布等 头文件: random 函数: std::normal_distribution<type> distribution( σ,μ ) 其中σ为正态分布的平均数学期望,也就是正态曲线中高峰的x值,μ值越大曲线坡度约缓,反之则越陡,在x轴上, (0,σ * μ) 占据了曲线的大部分空