• STL: set和map的区别、联系、使用


    set是一种关联式容器,其特性如下:

    • set以RBTree作为底层容器
    • 所得元素的只有key(键)没有value(值)
    • 不允许出现键重复
    • 所有的元素都会被自动排序
    • 不能通过迭代器来改变set的值,因为set的值仅有键,键不能被修改

    map和set一样是关联式容器,它们的底层容器都是红黑树,但是,map是存储键值对。

    它的特性如下:

    • map以RBTree作为底层容器
    • 所有元素都是键+值存在
    • 不允许键重复
    • 所有元素是通过键进行自动排序的
    • map的键是不能修改的,但是其键对应的值是可以修改的
    #include <map>  
      
    #include <string>  
      
    #include <set>
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        set<int> iset;
        iset.insert(11);
        iset.insert(12);
        iset.insert(13);
        iset.insert(14);
        for (set<int>::iterator iter = iset.begin(); iter != iset.end(); ++iter)
            cout << *iter << endl;
        
    
        cout << endl;
    
    
        map<int, string> mapStudent;  
        mapStudent.insert(pair<int, string>(1, "student_one"));  
        mapStudent.insert(pair<int, string>(2, "student_two"));  
    
        mapStudent.erase(mapStudent.begin(), mapStudent.end());  // 清空map内的元素
    
        pair<int, string>  Student3(3, "student_three");
        mapStudent.insert(Student3);  
    
    
        map<int, string>::iterator iter;  
      
        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
             cout << "first: "<< iter->first << ",      second:" << iter->second << endl;  
    
        return 0;
    }
     

    编译运行:

    .

    /************* 社会的有色眼光是:博士生、研究生、本科生、车间工人; 重点大学高材生、普通院校、二流院校、野鸡大学; 年薪百万、五十万、五万; 这些都只是帽子,可以失败千百次,但我和社会都觉得,人只要成功一次,就能换一顶帽子,只是社会看不见你之前的失败的帽子。 当然,换帽子决不是最终目的,走好自己的路就行。 杭州.大话西游 *******/
  • 相关阅读:
    Go语言从入门到放弃(结构体常见的tag)
    Go语言从入门到放弃(设置 go get 为国内源)
    AndroidStuidio安装
    ADB常用命令
    win10安装Nodejs
    VsCode配置Go语言插件
    Visual Studio Code使用指南
    Go语言从入门到放弃(四)
    CentOs7.5安装Redis
    InnoDB INFORMATION_SCHEMA FULLTEXT Index Tables
  • 原文地址:https://www.cnblogs.com/happybirthdaytoyou/p/13857170.html
Copyright © 2020-2023  润新知