Effective STL 23 Consider replacing associative container with sorted vector

本文主要是介绍Effective STL 23 Consider replacing associative container with sorted vector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

If we choose an associative container, we’ll almost certainly be using a balanced binary tree. Such a tree would be made up of tree nodes. each holding not only a Widget, but also a pointer to the node’s left child, a pointer to its right child, and a pointer to its parent.
In contrast, there is no overhead when we store a Widget in a vector. The vector itself has overhead, of course, and there may be empty (reserved) space at the end of vector.

using a sorted vector instead of a set

vector<Widget> vw;// lots of insertions, few lookups
... // multiset, stable_sort                
sort(vw.begin(), vw.end());     // object for value to look up
Widget w;               
... // lookup via binary_search             
if (binary_search(vw.begin(), vw.end(), w)...// lookup via lower_bound   
vector<Widget>::iterator i = lower_bound(vw.begin(), vw.end(). w);
if (i != vw.end() && !(w < *i))...// lookup via equal_range
pair<vector<Widget>::iterator, vector<Widget>::iterator> range = equal_range(vw.begin(), vw.end(), w);
if(range.fist != range.second)... // start Recorganize phase
...                         

using a sorted vector instead of a map

typedef pair<string, int> Data;class DataCompare {
public:bool operator()(const Data& lhs, const Data& rhs) const {return keyLess(lhs.first, lhs.second);}bool operator()(const Data& lhs, const Data::first_type& k) const {return keyLess(lhs.first, k);}bool operator()(const Data::first_type& k, const Data& rhs) const {return keyLess(k, rhs.first);}
private:bool keyLess(const Data::first_type& k1, const Data::second_type& k2) const {return k1 < k2;}
};vector<Data> vd;
...
sort(vd.begin(), vd.end(), DataCompare());string s;
...
if (binary_search(vd,begin(), vd.end(), s, DataCompare())..// lookup via lower_bound
// !DataCompare(lhs, k) 
// when it is true, we get i;
vector<Data>::iterator i = lower_bound(vd.begin(), vd.end(), s, DataCompare()); 
if (i != vd.end() && !DataCompare()(s, *i))...// lookup via equal_range
pair<vector<Data>::iterator, vector<Data>::iterator> range = equal_range(vb.begin(), vb.end(), s, DataCompare());
if (rang.first != rang.second) ...

这篇关于Effective STL 23 Consider replacing associative container with sorted vector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/237969

相关文章

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

STL经典案例(四)——实验室预约综合管理系统(项目涉及知识点很全面,内容有点多,耐心看完会有收获的!)

项目干货满满,内容有点过多,看起来可能会有点卡。系统提示读完超过俩小时,建议分多篇发布,我觉得分篇就不完整了,失去了这个项目的灵魂 一、需求分析 高校实验室预约管理系统包括三种不同身份:管理员、实验室教师、学生 管理员:给学生和实验室教师创建账号并分发 实验室教师:审核学生的预约申请 学生:申请使用实验室 高校实验室包括:超景深实验室(可容纳10人)、大数据实验室(可容纳20人)、物联网实验

C++ STL 适配器

系列文章目录 模板特例化,偏特化,左右值引用 https://blog.csdn.net/surfaceyan/article/details/126794013 C++ STL 关联容器 https://blog.csdn.net/surfaceyan/article/details/127414434 C++ STL 序列式容器(二) https://blog.csdn.net/surfac

模拟实现vector中的常见接口

insert void insert(iterator pos, const T& x){if (_finish == _endofstorage){int n = pos - _start;size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;reserve(newcapacity);pos = _start + n;//防止迭代

C++ STL关联容器Set与集合论入门

1. 简介 Set(集合)属于关联式容器,也是STL中最实用的容器,关联式容器依据特定的排序准则,自动为其元素排序。Set集合的底层使用一颗红黑树,其属于一种非线性的数据结构,每一次插入数据都会自动进行排序,注意,不是需要排序时再排序,而是每一次插入数据的时候其都会自动进行排序。因此,Set中的元素总是顺序的。 Set的性质有:数据自动进行排序且数据唯一,是一种集合元素,允许进行数学上的集合相

stl的sort和手写快排的运行效率哪个比较高?

STL的sort必然要比你自己写的快排要快,因为你自己手写一个这么复杂的sort,那就太闲了。STL的sort是尽量让复杂度维持在O(N log N)的,因此就有了各种的Hybrid sort algorithm。 题主你提到的先quicksort到一定深度之后就转为heapsort,这种是introsort。 每种STL实现使用的算法各有不同,GNU Standard C++ Lib

华为23年笔试题

消息传输 题目描述 在给定的 m x n (1 <= m, n <= 1000) 网格地图 grid 中,分布着一些信号塔,用于区域间通信。 每个单元格可以有以下三种状态:  值 0 代表空地,无法传递信号;  值 1 代表信号塔 A,在收到消息后,信号塔 A 可以在 1ms 后将信号发送给上下左右四个方向的信号塔; 值 2 代表信号塔 B,在收到消息后,信号塔 B 可以在 2ms

STL set整理

#include<set>#include<cstdio>#include<iterator>#include<iostream>#include<algorithm>using namespace std;//set 集合的操作//multisetset<int>Set1;set<int>Set2;set<int>Set3;/*begin() 返回指向第一个元素的迭代器

【C++STL(十四)】一个哈希桶简单模拟实现unordered_map/set

目录 前言 一、改造哈希桶 改造结点类 改造主体  模板参数改造  迭代器(重点) 改造完整哈希桶 unordered_map模拟实现 unordered_set模拟实现 前言 前面的文章都有说unordered_map/set的底层结构就是哈希表,严格来说是哈希桶,那么接下来我们就尝试使用同一个哈希桶来模拟实现一下。整体的逻辑和一棵红黑树封装map/set类似,所以