zoukankan      html  css  js  c++  java
  • map添加数据

    map添加数据

    1     //添加数据 六种方式
    2     mp.insert({0,"map zero"});//使用{}
    3     mp.insert(pair<int,string>(1,"map one"));//使用pair
    4     mp.insert(make_pair(2,"map two"));//使用make_pair
    5     mp.insert(make_pair<int,string>(3,"map three"));
    6     mp.insert(map<int,string>::value_type(4,"map four"));//使用value_type
    7     mp[5]="map five";//直接用键,值 赋值

    测试代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 void show(map<int,string>& mp){
     4     map<int,string>::iterator iter=mp.begin();
     5     while(iter!=mp.end()){
     6         cout<<iter->first<<" "<<iter->second<<endl;
     7         iter++;
     8     }
     9 }
    10 int main()
    11 {
    12     //先构造 map
    13     map<int,string> mp;//构造一个map
    14 
    15     //添加数据 六种方式
    16     mp.insert({0,"map zero"});//使用{}
    17     mp.insert(pair<int,string>(1,"map one"));//使用pair
    18     mp.insert(make_pair(2,"map two"));//使用make_pair
    19     mp.insert(make_pair<int,string>(3,"map three"));
    20     mp.insert(map<int,string>::value_type(4,"map four"));//使用value_type
    21     mp[5]="map five";//直接用键,值 赋值
    22 
    23 
    24 
    25     show(mp);//输出map
    26     return 0;
    27 }

    运行结果:

    1 0 map zero
    2 1 map one
    3 2 map two
    4 3 map three
    5 4 map four
    6 5 map five
  • 相关阅读:
    梦断代码阅读笔记一
    进度一
    LOJ#6031. 「雅礼集训 2017 Day1」字符串
    cf700E. Cool Slogans
    BZOJ1014: [JSOI2008]火星人prefix
    BZOJ2716: [Violet 3]天使玩偶
    cf1080F. Katya and Segments Sets
    BZOJ1354: [Baltic2005]Bus Trip
    灭绝树题集
    How Many Substrings?
  • 原文地址:https://www.cnblogs.com/NirobertEinteson/p/11967581.html
Copyright © 2011-2022 走看看