关联容器

来源:互联网 发布:魔兽争霸3全屏软件 编辑:程序博客网 时间:2024/06/02 20:54

关联容器

map, set, multimap, multiset, 等等; 头文件在map, set 中, unordered_map, unordered_set, 在头文件unordered_map, unordered_set中;

#include <iostream>#include <cstdlib>#include <map>#include <set>#include <string>#include <unordered_map>int main(){    //map : 两个数据, 可以自己定义;    //支持 insert, begin(), end(), erase(), find();    //pair类型可以遍历map, 用first, second;    //当然用插入的顺序是按字典顺序的;    //不会有重复的关键字;    //不允许改变关键字    //允许使用下标操作;    //.count() : 返回真 1, 假 0;    std::map<std::string, int > ma;    std::string str;    int i = 0;    while (std::cin >> str)        ma[str] = i++;    std::map<std::string, int > ma1;    ma1.insert(ma.cbegin(), ma.cend());    for (auto m : ma1)        std::cout << m.first << "  " << m.second << "\n";    auto x = ma.begin();    while (x != ma.end())    {        std::cout << x->first << "  " << x->second << "\n";        x++;    }    std::cout << ma[str] << "\n";    //.count() : 返回真 1, 假 0;    std::cout << ma.count("as") << "\n";    //set : 支持一个数据;    //支持insert, begin(), end(), erase()等;    //不会有重复的关键字;    //不允许改变关键字    //find()要用解引用;    //不允许使用下标操作;    std::set<std::string> se;    se = { "123", "234", "asf" };    std::set<std::string> se1;    se1.insert(se.cbegin(), se.cend());    std::cout << *se.find("234") << "\n";    //unordered_map : 无序的map;  头文件 : unordered_map;    //unordered_set : 无序的set;  头文件 : unordered_set;    std::unordered_map<std::string, int> unma;    std::string str1;    while (std::cin >> str1)        unma[str1]++;    for (auto x : unma)        std::cout << x.first << "   " << x.second << "\n";    //unordered_multimap : 无序可重复的map;    //unordered_multiset : 无序看重复的set;    std::unordered_multimap<std::string, int > unmultimap;    for (int i = 0; i < 5; i++)        unmultimap.insert(ma.begin(), ma.end());    for (auto x : unmultimap)        std::cout << x.first << "\n";    system("pause");    return 0;}
原创粉丝点击