概念:用来表示具有一一对应关系的一种结构,这种结构中一般存储两个成员变量key和value,key表示键值,value表示与key对应的信息。eg:英汉词典、单词的个数。
💡set s1;
💡set s2( InputIterator first, InputIterator last ) ;
💡set s3(const set& s2) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { set s1; //无参构造 //注意set:排序 + 去重 int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s2(a, a + sizeof(a) / sizeof(int)); //迭代器区间构造 set s3(s2); //拷贝构造 auto it = s2.begin(); while (it != s2.end()) //用迭代器进行遍历,中序遍历,升序序列 { cout << *it << ' '; //迭代器指向空间的值不能被修改,因为key为const T it++; } cout << endl; for (auto& e : s3) //支持迭代器就支持范围for { cout << e << ' '; } cout << endl; return 0; }
💡iterator find(const T& val)const ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set> s1(a, a + sizeof(a) / sizeof(int)); //仿函数为greater,按大于进行比较 auto it1 = s1.begin(); while (it1 != s1.end()) //用迭代器进行遍历,中序遍历,仿函数为greater, 降序序列 { cout << *it1 << ' '; //迭代器指向的空间值不能被修改,因为key为const T it1++; } cout << endl; set::iterator it2 = s1.find(10); //查找 if (it2 != s1.end()) { cout << "找到了" << endl; } else { cout << "找不到" << endl; } return 0; }
💡void erase(iterator pos) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set> s1(a, a + sizeof(a) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; set::iterator pos = s1.find(10); //s1.erase(pos); 错误,删除的值不存在,为无效位置,编译器会崩溃 return 0; }
💡size_t erase(const T& val) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s1(a, a + sizeof(a) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; s1.erase(10); //10不在,编译器不做任何处理 s1.erase(6); for (auto& e : s1) { cout << e << ' '; } cout << endl; return 0; }
💡void erase ( iterator first, iterator last ) ;
💡pair
insert(const T& val) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s1(a, a + sizeof(a) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; pair::iterator, bool> ret1 = s1.insert(10); cout << ret1.second << endl; pair::iterator, bool> ret2 = s1.insert(6); cout << ret2.second << endl; for (auto& e : s1) { cout << e << ' '; } cout << endl; return 0; }
💡iterator insert(iterator pos , const T& val) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s1(a, a + sizeof(a) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; auto pos = s1.find(3); auto it2 = s1.insert(pos, 20); for (auto& e : s1) { cout << e << ' '; } cout << endl; return 0; }
💡void insert(iterator first , iterator last) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a1[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s1(a1, a1 + sizeof(a1) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; int a2[] = { 10, 11, 10, 8, 6 }; s1.insert(a2, a2 + sizeof(a2) / sizeof(int)); for (auto& e : s1) { cout << e << ' '; } cout << endl; return 0; }
💡size_t count( const T& val)const ;
💡iterator lower_bound(const T& val)cosnt;
💡iterator upper_bound(const T& val)cosnt;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a1[] = { 6, 3, 4, 2, 1, 6, 4, 5, 2}; set s1(a1, a1 + sizeof(a1) / sizeof(int)); auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; auto it2 = s1.lower_bound(3); // >=3 cout << *it2 << endl; auto it3 = s1.upper_bound(5); // >5 cout << *it3 << endl; return 0; }
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { int a1[] = { 6, 3, 4, 2, 1, 6, 6, 4, 4, 5, 2}; multiset s1(a1, a1 + sizeof(a1) / sizeof(int)); //只具有 排序 功能 auto it1 = s1.begin(); while (it1 != s1.end()) { cout << *it1 << ' '; it1++; } cout << endl; auto start1 = s1.find(4); //find返回二叉搜索树中序的第一个值为4元素的迭代器 while (start1 != s1.end() && *start1 == 4) { cout << *start1 << ' '; start1++; } cout << endl; auto start2 = s1.lower_bound(4); //lower_bound返回二叉搜索树中序>=4第一个元素的迭代器 auto end2 = s1.upper_bound(5); //upper_bound返回二叉搜索树中序>5第一个元素的迭代器 while (start2 != end2) { cout << *start2 << ' '; start2++; } cout << endl; return 0; }
💡map
s1;
💡map
s2( InputIterator first, InputIterator last ) ;
💡map
s3(const map & s2) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include
💡pair
insert(const value_type& val) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include
💡T2& operatot[ ](const T1& key) ;
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include #include
https://leetcode.cn/problems/top-k-frequent-words/description/
class Solution { public: template class KvCompare{ public: bool operator()(const T& p1, const T& p2) { return p1.second > p2.second || (p1.second == p2.second && p1.first < p2.first); } }; vector topKFrequent(vector& words, int k) { map m; for(auto& e : words) m[e]++; //使用map来统计单词的个数 priority_queue, KvCompare>> pq(m.begin(), m.end()); vector ret; while(k--) { ret.push_back(pq.top()); pq.pop(); } return ret; } };
https://leetcode.cn/problems/intersection-of-two-arrays/description/
/*方法1:nums1、nums2都用set进行排序+去重,在遍历s2, 判断是否s1.count(e)==1; 方法2:sort+unique+erase,nums2[5]={1,1,2,2,3}->nums2{1,2,3,1,2},unique返回值为nums2+3; 方法3:set+数据同步、备份算法,前提都是有序+去重,同时从头开始往后走,值小的++,值相同,同时++, 直到有一个走到了尾就停止*/ class Solution { public: vector intersection(vector& nums1, vector& nums2) { set s1(nums1.begin(), nums1.end()); set s2(nums2.begin(), nums2.end()); vector ret; auto it1 = s1.begin(); //数据同步、备份算法 auto it2 = s2.begin(); while(it1 != s1.end() && it2 != s2.end()) { if(*it1 < *it2) //小的往后走 it1++; else if(*it1 > *it2) it2++; else //相等,同时往后走 { ret.push_back(*it1); it1++; it2++; } } return ret; } };