C++:map用法及元素的默认值

C++:map用法

一、map基本用法

键值对

第一个参数为键的类型,第二个参数为值的类型。

  • 源代码
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
    map<int,string> ::iterator iter;  //迭代器iterator:变量iter的数据类型是由map<int,string>定义的iterator类型
    map<int,string> myMap;

//添加数据
    myMap[1] = "one";
    myMap[2] = "two";
    myMap[3] = "three";

//遍历map
    iter = myMap.begin();             //指向map首对元素
    cout<<"myMap:"<<endl;
    for (iter; iter != myMap.end(); iter++) {  //myMap.end()指向map最后一对元素的后一对元素
        cout << (*iter).first << " " << (*iter).second << "\n";
    }
    cout<<endl;

//构造map
    map<int, string> myMap2(myMap.begin(), myMap.end());//用myMap指定范围内的元素对,构造myMap2
    map<int, string> myMap3(myMap);//用myMap构造myMap2

    iter=myMap2.begin();
    iter++;
    cout<<"myMap2: "<<(*iter).first<<" " << (*iter).second<<endl<<endl;

    iter=myMap3.begin();
    iter++;
    iter++;
    cout<<"myMap3: "<<(*iter).first<<" " << (*iter).second<<endl;

    return 0;
}

  • 运行结果:

1491599-20191206164824582-154268151.png

二、map元素的默认值

当map内元素值为int类型或常量时,默认值为0。

当为String类型时,默认值不明,不显示

  1. map内元素值为int类型
#include <iostream>
#include <map>
using namespace std;

int main(){
    map<int,int> table;

    table[1]=1;

    cout<<table[0]<<endl;
    cout<<table[1]<<endl;
    return 0;

}

  • 运行结果:

1491599-20191203203543956-2034852188.png

  1. map内元素值为常量类型
#include <iostream>
#include <map>
using namespace std;

enum Symbols { //第一个枚举元素默认值为0,后续元素递增+1。
    // 终结符号 Terminal symbols:TS
    TS_I,           // i
    TS_PLUS,        // +
    TS_MULTIPLY,    // *
    TS_L_PARENS,    // (
    TS_R_PARENS,    // )
    TS_EOS,         // #
    TS_INVALID,     // 非法字符

    // 非终结符号 Non-terminal symbols:NS
    NS_E,           // E
    NS_T,           // T
    NS_F            // F
};

int main(){
    map<int,enum Symbols> table;

    table[1]=TS_PLUS;

    cout<<table[0]<<endl;
    cout<<table[1]<<endl;

    return 0;

}

  • 运行结果:

1491599-20191203204536674-559687789.png

  1. map内元素值为string类型
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    map<int,string> table;

    table[1]="abc";

    cout<<table[0]<<endl;
    cout<<table[1]<<endl;
    return 0;

}

  • 运行结果:

1491599-20191203204811949-471549651.png

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::mapC++ 标准库中的关联容器,它提供了一种键值对的映射关系,并且会按照键的字典序自动排序。可以使用该容器实现字典、统计词频等功能。 使用 std::map 需要包含头文件 <map>,其中定义了该容器的类模板。定义一个 std::map 可以使用以下语法: ```c++ std::map<Key, Value> map_name; ``` 其中,Key 和 Value 分别指定了键和值的类型。例如,定义一个键为字符串,值为整数的 std::map 可以使用以下语句: ```c++ std::map<std::string, int> word_count; ``` 使用 std::map 插入元素可以使用 insert() 函数,删除元素可以使用 erase() 函数。查询元素可以使用 [] 运算符或者 find() 函数。例如,插入一个键值对可以使用以下语句: ```c++ word_count.insert(std::pair<std::string, int>("hello", 1)); ``` 使用 [] 运算符可以直接访问一个键对应的值,例如: ```c++ int count = word_count["hello"]; ``` 如果键不存在,则会自动插入一个新的键值对,值为默认值。使用 find() 函数可以查找一个键对应的迭代器,例如: ```c++ std::map<std::string, int>::iterator it = word_count.find("hello"); if (it != word_count.end()) { int count = it->second; } ``` 以上就是 std::map 的基本用法。需要注意的是,std::map 内部实现使用红黑树,插入、删除、查询的时间复杂度都是 O(log n),但是相比于 std::unordered_map,std::map 具有自动排序的特性,因此更适合于需要按照键的顺序访问元素的场合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值