昆明专业网站建设的公司网站怎么建立视频
2026/3/13 18:30:37 网站建设 项目流程
昆明专业网站建设的公司,网站怎么建立视频,平面设计师接单app,网站建设内容是经营项目吗map系列的使⽤ map和multimap参考⽂档 参考文档 map类的介绍 map的声明如下#xff0c;Key就是map底层关键字的类型#xff0c;T是map底层value的类型#xff0c;set默认要求Key⽀持⼩于⽐较#xff0c;如果不⽀持或者需要的话可以⾃⾏实现仿函数传给第⼆个模版参数Key就是map底层关键字的类型T是map底层value的类型set默认要求Key⽀持⼩于⽐较如果不⽀持或者需要的话可以⾃⾏实现仿函数传给第⼆个模版参数map底层存储数据的内存是从空间配置器申请的。⼀般情况下我们都不需要传后两个模版参数。map底层是⽤红⿊树实现增删查改效率是O(logN) 迭代器遍历是⾛的中序所以是按key有序顺序遍历的。template class Key, // map::key_type class T, // map::mapped_type class Compare lessKey, // map::key_compare class Alloc allocatorpairconst Key,T //map::allocator_type class map;pair类型介绍map底层的红⿊树节点中的数据使⽤pairKey, T存储键值对数据。typedef pairconst Key, T value_type; template class T1, class T2 struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair(): first(T1()), second(T2()) {} pair(const T1 a, const T2 b): first(a), second(b) {} templateclass U, class V pair (const pairU,V pr): first(pr.first), second(pr.second) {} }; template class T1,class T2 inline pairT1,T2 make_pair (T1 x, T2 y) { return ( pairT1,T2(x,y) ); }map的构造map的构造我们关注以下⼏个接⼝即可。map的⽀持正向和反向迭代遍历遍历默认按key的升序顺序因为底层是⼆叉搜索树迭代器遍历⾛的中序⽀持迭代器就意味着⽀持范围formap⽀持修改value数据不⽀持修改key数据修改关键字数据破坏了底层搜索树的结构。// empty (1) ⽆参默认构造 explicit map (const key_compare comp key_compare(), const allocator_type alloc allocator_type()); // range (2) 迭代器区间构造 template class InputIterator map (InputIterator first, InputIterator last, const key_compare comp key_compare(), const allocator_type allocator_type()); // copy (3) 拷⻉构造 map (const map x); // initializer list (5) initializer 列表构造 map (initializer_listvalue_type il, const key_compare comp key_compare(), const allocator_type alloc allocator_type()); // 迭代器是⼀个双向迭代器 iterator - a bidirectional iterator to const value_type // 正向迭代器 iterator begin(); iterator end(); // 反向迭代器 reverse_iterator rbegin(); reverse_iterator rend();map的增删查map的增删查关注以下⼏个接⼝即可map增接⼝插⼊的pair键值对数据跟set所有不同但是查和删的接⼝只⽤关键字key跟set是完全类似的不过find返回iterator不仅仅可以确认key在不在还找到key映射的value同时通过迭代还可以修改valueMember types key_type - The first template parameter (Key) mapped_type - The second template parameter (T) value_type - pairconst key_type,mapped_type // 单个数据插⼊如果已经key存在则插⼊失败,key存在相等value不相等也会插⼊失败 pairiterator,bool insert (const value_type val); // 列表插⼊已经在容器中存在的值不会插⼊ void insert (initializer_listvalue_type il); // 迭代器区间插⼊已经在容器中存在的值不会插⼊ template class InputIterator void insert (InputIterator first, InputIterator last); // 查找k返回k所在的迭代器没有找到返回end() iterator find (const key_type k); // 查找k返回k的个数 size_type count (const key_type k) const; // 删除⼀个迭代器位置的值 iterator erase (const_iterator position); // 删除kk存在返回0存在返回1 size_type erase (const key_type k); // 删除⼀段迭代器区间的值 iterator erase (const_iterator first, const_iterator last); // 返回⼤于等k位置的迭代器 iterator lower_bound (const key_type k); // 返回⼤于k位置的迭代器 const_iterator lower_bound (const key_type k) const;map的数据修改map⽀持修改mapped_type数据不⽀持修改key数据修改关键字数据破坏了底层搜索树的结构。map第⼀个⽀持修改的⽅式时通过迭代器迭代器遍历时或者find返回key所在的iterator修改map还有⼀个⾮常重要的修改接⼝operator[]但是operator[]不仅仅⽀持修改还⽀持插⼊数据和查找数据所以他是⼀个多功能复合接⼝需要注意从内部实现⻆度map这⾥把我们传统说的value值给的是T类型typedef为mapped_type。⽽value_type是红⿊树结点中存储的pair键值对值。⽇常使⽤我们还是习惯将这⾥的T映射值叫做value。Member types key_type - The first template parameter (Key) mapped_type - The second template parameter (T) value_type - pairconst key_type,mapped_type // 查找k返回k所在的迭代器没有找到返回end()如果找到了通过iterator可以修改key对应的mapped_type值 iterator find (const key_type k); // ⽂档中对insert返回值的说明 // The single element versions (1) return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed. // insert插⼊⼀个pairkey, T对象 // 1、如果key已经在map中插⼊失败则返回⼀个pairiterator,bool对象返回pair对象first是key所在结点的迭代器second是false // 2、如果key不在在map中插⼊成功则返回⼀个pairiterator,bool对象返回pair对象first是新插⼊key所在结点的迭代器second是true // 也就是说⽆论插⼊成功还是失败返回pairiterator,bool对象的first都会指向key所在的迭代器 // 那么也就意味着insert插⼊失败时充当了查找的功能正是因为这⼀点insert可以⽤来实现operator[] // 需要注意的是这⾥有两个pair不要混淆了⼀个是map底层红⿊树节点中存的pairkey, T另⼀个是insert返回值pairiterator,bool pairiterator,bool insert (const value_type val); mapped_type operator[] (const key_type k); // operator的内部实现 mapped_type operator[] (const key_type k) { // 1、如果k不在map中insert会插⼊k和mapped_type默认值同时[]返回结点中存储mapped_type值的引⽤那么我们可以通过引⽤修改返映射值。所以[]具备了插⼊修改功能 // 2、如果k在map中insert会插⼊失败但是insert返回pair对象的first是指向key结点的迭代器返回值同时[]返回结点中存储mapped_type值的引⽤所以[]具备了查找修改的功能 pairiterator, bool ret insert({ k, mapped_type() }); iterator it ret.first; return it-second; }构造遍历及增删查使⽤样例#includeiostream #includemap using namespace std; int main() { // initializer_list构造及迭代遍历 mapstring, string dict { {left, 左边}, {right, 右边},{insert, 插⼊},{ string, 字符串 } }; //mapstring, string::iterator it dict.begin(); auto it dict.begin(); while (it ! dict.end()) { //cout (*it).first :(*it).second endl; // map的迭代基本都使⽤operator-,这⾥省略了⼀个- // 第⼀个-是迭代器运算符重载返回pair*第⼆个箭头是结构指针解引⽤取pair数据 //cout it.operator-()-first : it.operator-()-second endl; cout it-first : it-second endl; it; } cout endl; // insert插⼊pair对象的4种⽅式对⽐之下最后⼀种最⽅便 pairstring, string kv1(first, 第⼀个); dict.insert(kv1); dict.insert(pairstring, string(second, 第⼆个)); dict.insert(make_pair(sort, 排序)); dict.insert({ auto, ⾃动的 }); // left已经存在插⼊失败 dict.insert({ left, 左边剩余 }); // 范围for遍历 for (const auto e : dict) { cout e.first : e.second endl; } cout endl; string str; while (cin str) { auto ret dict.find(str); if (ret ! dict.end()) { cout - ret-second endl; } else { cout ⽆此单词请重新输⼊ endl; } } // erase等接⼝跟set完全类似这⾥就不演⽰讲解了 return 0; }map的迭代器和功能样例#includeiostream #includemap #includestring using namespace std; int main() { // 利⽤find和iterator修改功能统计⽔果出现的次数 string arr[] { 苹果, 西⽠, 苹果, 西⽠, 苹果, 苹果, 西⽠, 苹果, ⾹蕉, 苹果, ⾹蕉 }; mapstring, int countMap; for (const auto str : arr) { // 先查找⽔果在不在map中 // 1、不在说明⽔果第⼀次出现则插⼊{⽔果, 1} // 2、在则查找到的节点中⽔果对应的次数 auto ret countMap.find(str); if (ret countMap.end()) { countMap.insert({ str, 1 }); } else { ret-second; } } for (const auto e : countMap) { cout e.first : e.second endl; } cout endl; for (auto e : arr) { pairmapstring, int::iterator, bool ret; ret countMap.insert(make_pair(e, 1)); // 已经存在了 if (ret.second false) { ret.first-second; } } return 0; } #includeiostream #includemap #includestring using namespace std; int main() { // 利⽤[]插⼊修改功能巧妙实现统计⽔果出现的次数 string arr[] { 苹果, 西⽠, 苹果, 西⽠, 苹果, 苹果, 西⽠, 苹果, ⾹蕉, 苹果, ⾹蕉 }; mapstring, int countMap; for (const auto str : arr) { // []先查找⽔果在不在map中 // 1、不在说明⽔果第⼀次出现则插⼊{⽔果, 0}同时返回次数的引⽤⼀下就变成1次了 // 2、在则返回⽔果对应的次数 countMap[str]; } for (const auto e : countMap) { cout e.first : e.second endl; } cout endl; return 0; }//V operator[](const K key) //{ // pairiterator, bool ret insert(make_pair(key, V())); // return ret.first-second; //}#includeiostream #includemap #includestring using namespace std; int main() { mapstring, string dict; dict.insert(make_pair(sort, 排序)); // key不存在-插⼊ {insert, string()} dict[insert]; // 插⼊修改 dict[left] 左边; // 修改 dict[left] 左边、剩余; // key存在-查找 cout dict[left] endl; return 0; }multimap和map的差异multimap和map的使⽤基本完全类似主要区别点在于multimap⽀持关键值key冗余那么insert/find/count/erase都围绕着⽀持关键值key冗余有所差异这⾥跟set和multiset完全⼀样⽐如find时有多个key返回中序第⼀个。其次就是multimap不⽀持[]因为⽀持key冗余[]就只能⽀持插⼊了不能⽀持修改。138. 随机链表的复制 - 力扣LeetCode数据结构初阶阶段为了控制随机指针我们将拷⻉结点链接在原节点的后⾯解决后⾯拷⻉节点还得解下来链接⾮常⿇烦。这⾥我们直接让{原结点,拷⻉结点}建⽴映射关系放到map中控制随机指针会⾮常简单⽅便这⾥体现了map在解决⼀些问题时的价值完全是降维打击。class Solution { public: Node* copyRandomList(Node* head) { mapNode*, Node* nodeMap; Node* copyhead nullptr,*copytail nullptr; Node* cur head; while(cur) { if(copytail nullptr) { copyhead copytail new Node(cur-val); } else { copytail-next new Node(cur-val); copytail copytail-next; } // 原节点和拷⻉节点map kv存储 nodeMap[cur] copytail; cur cur-next; } // 处理random cur head; Node* copy copyhead; while(cur) { if(cur-random nullptr) { copy-random nullptr; } else { copy-random nodeMap[cur-random]; } cur cur-next; copy copy-next; } return copyhead; } };692. 前K个高频单词 - 力扣LeetCode本题⽬我们利⽤map统计出次数以后返回的答案应该按单词出现频率由⾼到低排序有⼀个特殊要求如果不同的单词有相同出现频率按字典顺序排序。解决思路1⽤排序找前k个单词因为map中已经对key单词排序过也就意味着遍历map时次数相同的单词字典序⼩的在前⾯字典序⼤的在后⾯。那么我们将数据放到vector中⽤⼀个稳定的排序就可以实现上⾯特殊要求但是sort底层是快排是不稳定的所以我们要⽤stable_sort他是稳定的。class Solution { public: struct Compare { bool operator()(const pairstring, int x, const pairstring, int y) const { // 是降序是升序 return x.second y.second; } }; vectorstring topKFrequent(vectorstring words, int k) { mapstring, int countMap; for(auto e : words) { countMap[e]; } vectorpairstring, int v(countMap.begin(), countMap.end()); // 仿函数控制降序 stable_sort(v.begin(), v.end(), Compare()); //sort(v.begin(), v.end(), Compare()); // 取前k个 vectorstring strV; for(int i 0; i k; i) { strV.push_back(v[i].first); } return strV; } };解决思路2将map统计出的次数的数据放到vector中排序或者放到priority_queue中来选出前k个。利⽤仿函数强⾏控制次数相等的字典序⼩的在前⾯。class Solution { public: struct Compare { bool operator()(const pairstring, int x, const pairstring, int y) const { return x.second y.second || (x.second y.second x.first y.first);; } }; vectorstring topKFrequent(vectorstring words, int k) { mapstring, int countMap; for(auto e : words) { countMap[e]; } vectorpairstring, int v(countMap.begin(), countMap.end()); // 仿函数控制降序,仿函数控制次数相等字典序⼩的在前⾯ sort(v.begin(), v.end(), Compare()); // 取前k个 vectorstring strV; for(int i 0; i k; i) { strV.push_back(v[i].first); } return strV; } };class Solution { public: struct Compare { bool operator()(const pairstring, int x, const pairstring, int y) const { // 要注意优先级队列底层是反的⼤堆要实现⼩于⽐较所以这⾥次数相等想要字典序⼩的在前⾯要⽐较字典序⼤的为真 return x.second y.second || (x.second y.second x.first y.first); } }; vectorstring topKFrequent(vectorstring words, int k) { mapstring, int countMap; for(auto e : words) { countMap[e]; } // 将map中的单词次数放到priority_queue中仿函数控制⼤堆次数相同按照字典序规则排序 priority_queuepairstring, int, vectorpairstring, int, Compare p(countMap.begin(), countMap.end()); vectorstring strV; for(int i 0; i k; i) { strV.push_back(p.top().first); p.pop(); } return strV; } };

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询