美文网首页
c++ map使用示例

c++ map使用示例

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

1.源码实现

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
    map<int, string> mapStudent;
    map<int, string>::iterator it;

    /*插入数据*/
    mapStudent.insert(pair<int, string>(1, "student one"));
    mapStudent.insert(pair<int, string>(2, "student two"));
    mapStudent.insert(pair<int, string>(3, "student three"));

    cout << "2: " << mapStudent[2] << endl;

    /*遍历数据*/
    for(it=mapStudent.begin(); it!=mapStudent.end(); it++)
    {
        cout << it->first << ": " << it->second << endl;
    }

    /*清空数据*/
    mapStudent.clear();

    /*遍历数据*/
    for(it=mapStudent.begin(); it!=mapStudent.end(); it++)
    {
        cout << it->first << ": " << it->second << endl;
    }

    return 0;
}

2.编译源码

$ g++ -o example example.cpp

3.运行及其结果

$ ./example
2: student two
1: student one
2: student two
3: student three

相关文章

  • c++ map使用示例

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

  • map的用法

    Map概念 Map 对象以键值对保存数据。 map相对object的优点 map使用示例 数组的map方法 map...

  • map hash_map(挖坑)

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

  • C++调用lua方式

    目标 使用C++调用lua接口 示例 lua代码(test.lua) C++调用示例(lua_test.cpp) ...

  • C++ vector、iterator、map的使用代码示例

  • go语言陷阱之九:map的容量

    当一个map变量被创建后,你可以指定map的容量,但是不可以在map上使用cap()方法 代码示例:package...

  • map字典

    golang的map实现并不是像c++一样使用红黑树,而是使用了hashmap,用数组来实现。 map 是字典的概...

  • 第七周 - 20180521

    C++中map/unodered_map选择和比较 在写LycorisNet的过程中,底层的数据结构大量使用了ma...

  • C++ grpc使用示例

    核心工作 在一个 .proto 文件内定义服务. 用 protocol buffer 编译器生成服务器和客户端代码...

  • C++ allocator使用示例

    动态内存管理 之前我们讲述过动态内存的开辟,可以通过new, malloc,以及alloc等方式,本文通过介绍al...

网友评论

      本文标题:c++ map使用示例

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