美文网首页
c++二维map操作

c++二维map操作

作者: 一路向后 | 来源:发表于2021-03-21 22:08 被阅读0次

1.源码实现

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
    map<string, map<string, string> > a;
    map<string, string> c;
    map<string, map<string, string> >::iterator p2;
    map<string, string>::iterator p3;
    string temp;
    char num[10];
    int i;

    c.clear();

    //将3个元素插入到map c中
    for(i = 0; i < 3; i++)
    {
        sprintf(num, "%d", i);
        temp = string(num);
        c.insert(make_pair(temp, "string"));
    }

    //将含有3个元素的 map c 插入到 map a中
    a.insert(make_pair(temp, c));

    //删除二维map中的数据
    a["2"].erase("1");

    //修改二维map中的数据
    a["2"]["2"] = "hello";

    c.clear();

    //遍历二维map
    for(p2 = a.begin(); p2 != a.end(); p2++)
    {
        for(p3 = p2->second.begin(); p3 != p2->second.end(); p3++)
        {
            cout << "[" << p2->first << ", " << p3->first << "]: " << p3->second << endl;
        }
    }

    return 0;
}

2.编译源码

$ g++ -o example example.cpp

3.运行及其结果

$ ./example
[2, 0]: string
[2, 2]: hello

相关文章

  • c++二维map操作

    1.源码实现 2.编译源码 3.运行及其结果

  • C++ std::map的插入操作

    std::map的插入操作 map是C++中的映射容器类, 支持key-value的存储方式, 那么在插入时是进行...

  • c++中map的操作

    之前用python,写着很方便,特别是里面有一种数据类型,字典。比如 student_scores = {}stu...

  • go语言学习总结

    1、go语言的map和c++中的map有什么区别? go语言中的map是hash_table,和c++中uno...

  • map

    在其他语言诸如C++/Java中, map一般都以库的方式提供 如C++中的 std::map<> ,Java中的...

  • 实现特定场景下高性能的HashMap

    C++标准库的某些场景下的效率问题 在下面的场景中,C++标准库的unordered_map、map、multis...

  • C++ unordered_map

    hash_map ≈ unordered_map 最初的 C++ 标准库中没有类似 hash_map 的实现,但不...

  • map hash_map(挖坑)

    学习内容来自C++ STL中哈希表 hash_map 未学C++之哈希表的使用 map 使用count,返回的是被...

  • Scala学习第十节:Map 以及常规用法

    创建map 创建可变map 获取map中的值 更新map 其他操作

  • C++大厂面试真题

    C++标准库的map和set有什么区别,如何实现的? map和set都是C++的关联容器,其底层实现都是红黑树。 ...

网友评论

      本文标题:c++二维map操作

      本文链接:https://www.haomeiwen.com/subject/qezocltx.html