首页 > 学院 > 开发设计 > 正文

STL中的set&&map

2019-11-08 18:48:37
字体:
来源:转载
供稿:网友

首先看一下set与map的介绍:

set关联式容器,set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。

map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个 可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。

作为STL容器,他们有着相同的操作:插入,删除,查找等。 set与map也是存在着区别的: set的结点是一个数据,set(集合)——包含了经过排序了的数据,这些数据的(value)必须是唯一的。 map的结点是一对数据,map(映射)——经过排序了的二元组的集合,map中的每个元素都是由两个值组成,其中的key(键值,一个map中的键值必须是唯一的)是在排序或搜索时使用,它的值可以在容器中重新获取;而另一个值是该元素关联的数值。比如,除了可以ar[43] = “overripe”这样找到一个数据,map还可以通过ar[“banana”] = “overripe”这样的方法找到一个数据。如果你想获得其中的元素信息,通过输入元素的全名就可以轻松实现。

接下来进一步了解他们的一些操作: 文档中set的插入操作有三种,分别为: 这里写图片描述

其中pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同的。

template <class T1, class T2> struct pair{ typedef T1 first_type; typedef T2 second_type; T1 first; T2 second;}

看下面代码:

#include <iostream>#include <stdlib.h>#include <utility>#include <string>using namespace std;int main() { pair <string, double> PRoduct1("tomatoes", 3.25); pair <string, double> product2; pair <string, double> product3; product2.first = "lightbulbs"; product2.second = 0.99; product3 = make_pair("shoes", 20.0);// cout << "The price of " << product1.first << " is $" << product1.second << "/n"; cout << "The price of " << product2.first << " is $" << product2.second << "/n"; cout << "The price of " << product3.first << " is $" << product3.second << "/n"; system("pause"); return 0;}

运行结果为: 这里写图片描述

三种insert的代码:

#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>#include <stdlib.h>#include <set>using namespace std;int main(){ set<int> myset; set<int>::iterator it; pair<set<int>::iterator, bool> ret; //直接插入 myset.insert(4); myset.insert(1); myset.insert(7); myset.insert(5); ret = myset.insert(1); // 1已经插入过,不能重复插入 if (ret.second == false) { it = ret.first; // "it" 现在所指是1 } //找位置插入 myset.insert(it, 2); myset.insert(it, 8); int a[] = { 5, 15 }; //插入一段数据 myset.insert(a, a + 2);//仅插入了15 cout << "myset contains:"<<endl; for (it = myset.begin(); it != myset.end(); it++) { cout << " " << *it; } cout << endl; system("pause"); return 0;

运行结果为: 这里写图片描述

同样删除操作也对应有三种: 这里写图片描述 测试代码如下:

#include <iostream>#include <set>using namespace std;int main(){ set<int> myset; set<int>::iterator it; //插入数据 for (int i = 1; i < 10; i++) { myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90 } it = myset.begin();//"it"指向10 it++; // "it"指向20 //找到位置删除 myset.erase(it); //找到数据删除 myset.erase(40); //删除一段数据(60-90) it = myset.find(60);//查找位置 myset.erase(it, myset.end()); cout << "myset contains:"; for (it = myset.begin(); it != myset.end(); ++it) cout << " " << *it; cout << endl; system("pause"); return 0;}

运行结果如下: 这里写图片描述

map的应用实例,统计水果出现的次数并排序

#include <algorithm>#include <stdlib.h>#include <map>#include <string>#include <vector>struct Min{ bool Operator()(pair<string, int> p1, pair<string, int> p2) { return p1.second > p2.second; }};void HeapSort(){ vector<string> v1 = { "苹果", "香蕉", "苹果" , "苹果", "苹果", "香蕉" , "苹果", "草莓" }; map<string, int> fruit; //用map统计次数 for (size_t i = 0; i < v1.size(); i++) { fruit[v1[i]]++; } // 用heap排序 vector<pair<string, int>> vec; map<string, int>::iterator it = fruit.begin(); //while (it != fruit.end()) //{ // vec.push_back(pair<string, int>(it->first, it->second)); // it++; //} vec.insert(vec.begin(), fruit.begin(), fruit.end()); make_heap(vec.begin(), vec.end(), Min()); sort_heap(vec.begin(), vec.end(), Min()); int K = 3; for (size_t i = 0; (i < K) && (i < vec.size()); i++) { cout << vec[i].first << "--" << vec[i].second << endl; }}int main(){ HeapSort(); system("pause"); return 0;}

运行结果为: 这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表