C++ Map实践

Posted do-your-best

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Map实践相关的知识,希望对你有一定的参考价值。

实践如下:

#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
using namespace std;

int main() 
    cout << "Map 测试" << endl;

    //int i =1;
    string name("jack");
    pair<int, string> pair1(1, name);
    pair<int, string> pair2(2, "老张");

    map<int, string> map1;
    map1.insert(pair1);
    map1.insert(pair1);
    map1.insert(pair2);

    map<int, string>::iterator it;
    for (it = map1.begin(); it != map1.end(); it++) 
        cout << (*it).first << "->" << (*it).second << endl;
    

    pair<string, string> pair21("张三", "张三");
    pair<string, string> pair22("老张", "老张");

    map<string, string> map2;
    map2.insert(pair21);
    map2.insert(pair21);
    map2.insert(pair22);

    map<string, string>::iterator it2;
    for (it2 = map2.begin(); it2 != map2.end(); it2++) 
        cout << (*it2).first << "->" << (*it2).second << endl;
    

    map2.erase("张三");
    cout << "再次遍历" << endl;
    for (it2 = map2.begin(); it2 != map2.end(); it2++) 
        cout << (*it2).first << "->" << (*it2).second << endl;
    

    map2.insert(pair<string, string>("老张2", "老张2"));
    cout << "再次遍历2" << endl;
    for (it2 = map2.begin(); it2 != map2.end(); it2++) 
        cout << (*it2).first << "->" << (*it2).second << endl;
    

    cout << "测试结束" << endl;
    return 0;

 

以上是关于C++ Map实践的主要内容,如果未能解决你的问题,请参考以下文章

Object/Map 何为最佳实践

Golang实践录:map的几个使用示例

Go语言map实践

Go基础编程实践—— Map和数组

Golang实践录:map的几个使用示例

Golang实践录:map的几个使用示例