zoukankan      html  css  js  c++  java
  • (七)STL关联容器

    序列容器:

      容器: array、vector、list、deque、forward_list

      特点: 存储的都是基本数据类型,intdoublefloatstring等,或者结构体自定义类型

    关联容器:

      容器: map、set、multimap、multiset

      特点: 键值对(key_value)的存储方式

    具体区分:

                                 

    1、map

    #include <iostream>
    #include <map>
    using namespace std;
    
    int main()
    {
    	map<int, string> m;
    	m[2] = "chian";
    	m[4] = "japan";
    	m[6] = "korea";
    
    	// 遍历
    	for (map<int, string> ::iterator it = m.begin(); it != m.end(); ++it) {
    		cout << it->first << " : " << it->second << endl;
    	}
    
    	// 查找
    	cout << m[2] << endl;
    	system("pause");
    	return 0;
    }

    2、multimap

    和 map 容器的区别在于,multimap 容器中可以同时存储多(≥2)个键相同的键值对。

    3、set

    特点: 要求key和value值必须相等, 如{<'a', 'a'>, <'b', 'b'>, <'c', 'c'>}

       存储的各个元素的值必须各不相同

        set容器中,由于key_value是相等的,因此可以对key或value进行排序

       set容器这种特性,以上例子只需要为set提供{'a','b','c'},即可成功存储;

    #include <iostream>
    #include <set>
    using namespace std;
    
    // 创建set容器
    int main()
    {
    	// 调用默认构造函数,创建空set
    	set<string> my1;
    
    	// 直接对齐初始化
    	set<string> my2{ "chian","englis" };
    
    	// 利用双向迭代器遍历
    	for (auto it = my2.begin(); it != my2.end(); ++it) {
    		cout << *it << endl;
    	}
    
    	system("pause");
    	return 0;
    }

    应用

    #include <iostream>
    #include <set>
    using namespace std;
    
    /*
    删除set容器中值为val的元素
    	size_type erase(const value_type& val);
    删除position迭代器指向的元素
    	iterator erase(const_iterator position);
    删除[first,last]区间内的所有元素
    	iteraror erase(const_iteraror firstm const_iterator last);
    */
    
    void PrintShow(set<int> a)
    {
    	cout << "显示: ";
    	for (auto i = a.begin(); i != a.end(); ++i) {
    		cout << *i << " ";
    	}
    	cout << endl << endl;
    }
    
    int main()
    {
    	set<int>my1{ 11,21,32,43,54,65,76 };
    	// size_type erase(const value_type & val);
    	// 返回的值为整数,表示成功删除的元素个数
    	int num = my1.erase(32);
    	cout << "第一种方式:" << num << endl;
    	PrintShow(my1);
    
    	// iterator erase(const_iterator position);
    	// 返回的是迭代器,指向删除元素的下一个元素
    	set<int>::iterator it = my1.erase(my1.begin());
    	cout << "第二种方式:" << *it << endl;
    	PrintShow(my1);
    
    	// iteraror erase(const_iteraror firstm const_iterator last);
    	set<int>::iterator it2 = my1.erase(my1.begin(), --my1.end());
    	cout << "第三种方式:" << *it2 << endl;
    	PrintShow(my1);
    
    	// 清除所有成员
    	my1.clear();
    	PrintShow(my1);
    	system("pause");
    	return 0;
    }

    4、multiset

    multiset与set差别:multiset允许存储多个值相同的元素,而set容器只能存储互不相同的元素;

    创建multiset容器:

      multiset<string> my;  // 空容器

      multiset<string> my{"china","english"};  // 对其进行初始化

    #include <iostream>
    #include <set>
    using namespace std;
    
    void ShowMultiSet(multiset<int> my)
    {
    	for (auto it = my.begin(); it != my.end(); ++it) {
    		cout << *it << " ";
    	}
    	cout << endl;
    }
    
    void ShowSet(set<int> my)
    {
    	for (auto it = my.begin(); it != my.end(); ++it) {
    		cout << *it << " ";
    	}
    }
    int main()
    {
    	multiset<int> my{ 1,2,3,4,5 };
    	my.insert(5);
    	ShowMultiSet(my);
    	
    	// set不能有相同的value
    	set<int> my1{ 1,2,3,4,5 };
    	my1.insert(5);
    	ShowSet(my1);
    
    	system("pause");
    	return 0;
    }
    

      

    做一个优秀的程序媛
  • 相关阅读:
    问题2017S03
    问题2017S02
    高等代数问题1
    无穷积分换元法的严格解释
    线性空间的同构理论
    问题2017S01
    朴素贝叶斯分类
    决策树
    温习MATLAB
    感知机
  • 原文地址:https://www.cnblogs.com/oytt/p/13962905.html
Copyright © 2011-2022 走看看