[转]An STL compliant sorted vector-源码示例
原文地址:http://www.codeproject.com/Articles/3217/An-STL-compliant-sorted-vector
最近在看sorted vectored的一些东西,自己封装了一个sorted vector类型,后来找到了codeproject上的一个源码示例,感觉写的不错,可以借鉴一下。
sorted_vector
adapts a std::vector
to the interface required by std::set/std::multiset
, thereby providing set/multiset and vector functionality (random access) in one container.
/* STL-conforming "sorted vector" container
*
* (C) 2002 Martin Holzherr (holzherr@infobrain.com). All rights reserved.
*
* Permission is granted to use, distribute and modify this code provided that:
* ? this copyright notice appears,
* ?
* The author welcomes any suggestions on the code or reportings of actual
* use of the code. Please send your comments to holzherr@infobrain.com.
*
* The author makes NO WARRANTY or representation, either express or implied,
* with respect to this code, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and
* you, its user, assume the entire risk as to its quality and accuracy.
*
* Created: November 19th, 2002
* Last modified: November 27th, 2002
(changed namespace from std to codeproject;
uses template member functions for MSCVER>=1300) */ #ifndef SORTED_VECTOR_
#define SORTED_VECTOR_
#define VERSION_SORTED_VECTOR_ 0x00010010 #include <algorithm>
#include <vector>
#include <utility>
#include <functional> #pragma pack(push,8)
#pragma warning(push,3) namespace codeproject{
// TEMPLATE CLASS sorted_vector template<class K, bool bNoDuplicates= false,class Pr = std::less<K>, class A = std::allocator<K> >
class sorted_vector {
public:
typedef sorted_vector<K,bNoDuplicates,Pr,A> Myt_;
typedef std::vector<K,A> Cont;
typedef typename Cont::allocator_type allocator_type;
typedef typename Cont::size_type size_type;
typedef typename Cont::difference_type difference_type;
typedef typename Cont::reference reference;
typedef typename Cont::const_reference const_reference;
typedef typename Cont::value_type value_type;
typedef K key_type;
typedef typename Cont::iterator iterator;
typedef typename Cont::const_iterator const_iterator;
typedef Pr key_compare;
typedef Pr value_compare; typedef typename Cont::const_reverse_iterator
const_reverse_iterator;
typedef typename Cont::reverse_iterator reverse_iterator; typedef std::pair<iterator, iterator> Pairii_;
typedef std::pair<const_iterator, const_iterator> Paircc_;
typedef std::pair<iterator, bool> Pairib_;
explicit sorted_vector(const Pr& pred = Pr(),const A& al = A())
:key_compare_(pred),vec_(al){} //#if (_MSC_VER >= 1300) //_MSC_VER 定义编译器的版本,MS VC++
template<class It>
sorted_vector(It first, It beyond,
const Pr& pred = Pr(),const A& al = A())
:key_compare_(pred),vec_(first,beyond,al)
{stable_sort();}
//#else
// sorted_vector(const_iterator first, const_iterator beyond,
// const Pr& pred = Pr(),const A& al = A())
// :key_compare_(pred),vec_(first,beyond,al)
// {stable_sort();}
//#endif sorted_vector(const Myt_& x)
: vec_(x.vec_),key_compare_(x.key_compare_)
{} ~sorted_vector() {}
Myt_& operator=(const Myt_& x) {vec_.operator=(x.vec_);
key_compare_= x.key_compare_;
return *this;}
Myt_& operator=(const Cont& x){vec_.operator=(x);
sort();return *this;} void reserve(size_type n) {vec_.reserve(n);}
iterator begin() {return vec_.begin(); }
const_iterator begin() const {return vec_.begin(); }
iterator end() {return vec_.end();}
const_iterator end() const {return vec_.end();}
reverse_iterator rbegin() {return vec_.rbegin();}
const_reverse_iterator rbegin() const
{return vec_.rbegin();} reverse_iterator rend() {return vec_.rend();}
const_reverse_iterator rend() const
{return vec_.rend();} size_type size() const {return vec_.size();}
size_type max_size() const {return vec_.max_size();}
bool empty() const {return vec_.empty();}
A get_allocator() const {return vec_.get_allocator();}
const_reference at(size_type p) const {return vec_.at(p);}
reference at(size_type p) {return vec_.at(p);}
const_reference operator[](size_type p) const
{return vec_.operator[](p);} reference operator[](size_type p) {return vec_.operator[](p);}
reference front() {return vec_.front();}
const_reference front() const {return vec_.front();}
reference back() {return vec_.back();}
const_reference back() const {return vec_.back();}
void pop_back() {vec_.pop_back();} void assign(const_iterator first, const_iterator beyond)
{vec_.assign(first,beyond);}
void assign(size_type n, const K& x = K())
{vec_.assign(n,x);}
/*insert members*/
Pairib_ insert(const value_type& x)
{
if(bNoDuplicates){
iterator p= lower_bound(x);
if(p==end()||key_compare_(x,*p)){
return Pairib_(InsertImpl_(p,x),true);
}else{
return Pairib_(p,false);
}
}else{
iterator p= upper_bound(x);
return Pairib_(InsertImpl_(p,x),true);
}
}
iterator insert(iterator it, const value_type& x)//it is the hint
{
if(it!=end() ){
if(bNoDuplicates){
if(key_compare_(*it,x)){
if((it+)==end()||KeyCompare_Gt_(*(it+),x)){//use hint
return InsertImpl_(it+,x);
}else if(KeyCompare_Geq_(*(it+),x)){
return end();
}
}
}else{
if( KeyCompare_Leq_(*it,x)
&&((it+)==end()||KeyCompare_Geq_(*(it+),x))){
return InsertImpl_(it+,x);
}
}
}
return insert(x).first;
}
//#if (_MSC_VER >= 1300) //_MSC_VER 定义编译器的版本,MS VC++
template<class It>
void insert(It first, It beyond)
{
size_type n= std::distance(first,beyond);
reserve(size()+n);
for( ;first!=beyond;++first){
insert(*first);
}
}
//#else
// void insert(const_iterator first, const_iterator beyond)
// {
// size_type n= std::distance(first,beyond);
// reserve(size()+n);
// for( ;first!=beyond;++first){
// insert(*first);
// }
// }
//#endif
iterator erase(iterator p) {return vec_.erase(p);}
iterator erase(iterator first, iterator beyond)
{return vec_.erase(first,beyond);}
size_type erase(const K& key)
{
Pairii_ begEnd= equal_range(key);
size_type n= std::distance(begEnd.first,begEnd.second);
erase(begEnd.first,begEnd.second);
return n;
}
void clear() {return vec_.clear();} bool Eq_(const Myt_& x) const
{return (size() == x.size()
&& std::equal(begin(), end(), x.begin())); }
bool Lt_(const Myt_& x) const
{return (std::lexicographical_compare(begin(), end(),
x.begin(), x.end()));}
void swap(Myt_& x)
{vec_.swap(x.vec_);std::swap(key_compare_,x.key_compare_);} friend void swap(Myt_& x, Myt_& Y_)
{x.swap(Y_); } key_compare key_comp() const {return key_compare_; }
value_compare value_comp() const {return (key_comp()); } //针对多维索引的属性值查找,需提供自定义的比较方法。只能查找排序的属性值。
template <typename _Tp, class _Compare>
const_iterator find(const _Tp& k, _Compare cmp) {
const_iterator p = lower_bound(k, cmp);
return (p == end() || cmp(k, *p)) ? end() : p;
} template <typename _Tp, typename _Compare>
iterator lower_bound (const _Tp& val, _Compare cmp) {
return std::lower_bound(begin(), end(), val, cmp);
} iterator find(const K& k)
{ iterator p = lower_bound(k);
return (p==end()||key_compare_(k, *p))? end():p;
}
const_iterator find(const K& k) const
{const_iterator p = lower_bound(k);
return (p==end()||key_compare_(k,*p))?end():p;}
size_type count(const K& k) const
{Paircc_ Ans_ = equal_range(k);
size_type n = std::distance(Ans_.first, Ans_.second);
return (n); }
iterator lower_bound(const K& k)
{return std::lower_bound(begin(), end(), k, key_compare_); }
const_iterator lower_bound(const K& k) const
{return std::lower_bound(begin(), end(), k, key_compare_); }
iterator upper_bound(const K& k)
{return std::upper_bound(begin(), end(), k, key_compare_); }
const_iterator upper_bound(const K& k) const
{return std::upper_bound(begin(), end(), k, key_compare_); }
Pairii_ equal_range(const K& k)
{return std::equal_range(begin(), end(), k, key_compare_); }
Paircc_ equal_range(const K& k) const
{return std::equal_range(begin(), end(), k, key_compare_); } /*functions for use with direct std::vector-access*/
Cont& get_container()
{return vec_;}
void sort()//restore sorted order after low level access
{ std::sort(vec_.begin(),vec_.end(),key_compare_);
if( bNoDuplicates ){
vec_.erase(Unique_(),vec_.end());
}
}
void stable_sort()//restore sorted order after low level access
{ std::stable_sort(vec_.begin(),vec_.end(),key_compare_);
if( bNoDuplicates ){
erase(Unique_(),end());
}
}
protected:
iterator Unique_()
{ iterator front_= vec_.begin(),out_= vec_.end(),end_=vec_.end();
bool bCopy_= false;
for(iterator prev_; (prev_=front_)!=end_ && ++front_!=end_; ){
if( key_compare_(*prev_,*front_)){
if(bCopy_){
*out_= *front_;
out_++;
}
}else{
if(!bCopy_){out_=front_;bCopy_=true;}
}
}
return out_;
}
iterator InsertImpl_(iterator p,const value_type& x)
{return vec_.insert(p,x);}
bool KeyCompare_Leq_(const K& ty0,const K& ty1)
{return !key_compare_(ty1,ty0);}
bool KeyCompare_Geq_(const K& ty0,const K& ty1)
{return !key_compare_(ty0,ty1);}
bool KeyCompare_Gt_(const K& ty0,const K& ty1)
{return key_compare_(ty1,ty0);} key_compare key_compare_;
Cont vec_;
}; template<class K,bool bNoDuplicates,class Pr, class A> inline
bool operator==(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return x.Eq_(Y_); }
template<class K,bool bNoDuplicates,class Pr, class A> inline
bool operator!=(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return !(x == Y_); }
template<class K,bool bNoDuplicates,class Pr, class A> inline
bool operator<(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return x.Lt_(Y_);}
template<class K,bool bNoDuplicates,class Pr,class A> inline
bool operator>(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return Y_ < x; }
template<class K,bool bNoDuplicates,class Pr, class A> inline
bool operator<=(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return !(Y_ < x); }
template<class K, bool bNoDuplicates,class Pr,class A> inline
bool operator>=(const sorted_vector<K, bNoDuplicates,Pr,A>& x,
const sorted_vector<K, bNoDuplicates,Pr,A>& Y_)
{return (!(x < Y_)); }
}
#pragma warning(pop)
#pragma pack(pop)
#elif VERSION_SORTED_VECTOR_ != 0x00010010
#error You have included two sorted_vector.h with different version numbers
#endif
[转]An STL compliant sorted vector-源码示例的更多相关文章
- vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...
- vector源码(参考STL源码--侯捷):空间分配导致迭代器失效
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- vector源码2(参考STL源码--侯捷):空间分配、push_back
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- vector源码1(参考STL源码--侯捷):源码
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- 个人学习-STL深入学习01-vectory源码研习 // 需要补充
参考资料: [1]博主:一枚程序员 STL源码剖析--vector https://www.cnblogs.com/sooner/p/3273395.html [2]博主:劲蜡鸡腿堡 vector源码 ...
- ArrayList和LinkedList和Vector源码分析
ArrayList源码: private static final int DEFAULT_CAPACITY = 10;//默认长度 /** * Shared empty array instance ...
- 转:【Java集合源码剖析】Vector源码剖析
转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865 Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量 ...
- Stack和Vector源码分析
Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...
- Vector源码分析和实例应用
1.Vector介绍 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. Vector ...
随机推荐
- android 标签页<include /> 的使用
在android页面布局设计中,有时候需要用到很多相同的布局设计.如果每个用到该布局的xml里都写那个相同布局的话,会造成语句冗余,而且可读性很差. 为了解决这个问题的话,我们可以把相同布局的代码单独 ...
- 视频x264编码浅析
声明 x264_param_t 结构体变量: x264_param_t params; x264_param_default_preset(¶ms, "ultrafast&q ...
- Redis搭建(三):哨兵模式
一.sentinel介绍 Redis 2.8中提供了“哨兵”工具来实现自动化的系统监控和故障恢复功能. Redis 2.6 版也提供了哨兵工具,但此时的哨兵是1.0版,存在非常多的问题,任何情况下都不 ...
- python调用Go代码
Go 1.5发布了,其中包含了一个特性:可以编译生成动态链接库,经试验,生成的.so文件可以被python加载并调用.下面举个例子: 先写一个go文件main.go: package main imp ...
- std:: lower_bound std:: upper_bound
std:: lower_bound 该函数返回范围内第一个不小于(大于或等于)指定val的值.如果序列中的值都小于val,则返回last.序列应该已经有序! eg: #include <iost ...
- Docker学习笔记_Dockerfile基本知识
Dockerfile由一行行命令语句组成,并支持以#开头的注释行. 1.编写一个Dockerfile文件 创建一个空的Docker工作目录,进入该目录,使用sudo vim Dockerfile指令新 ...
- 1.介绍templates
我们现在要计算int和double类型数据的平方,我们就需要2个函数: #include <iostream> using namespace std; int square(int x) ...
- c# 导入c++ dll
1.类的函数的内联实现 #include "stdafx.h" #include "testdll.h" #include <iostream> # ...
- WEBAPI使用过滤器对API接口进行验证
用户登录控制器:[ActionFilter]自定义过滤器 用户信息:var userData = new JObject(); userData.Add(" ...
- 3.python 发送邮件之smtplib模块
SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址的邮件传输规则. python中对SMTP进行了简单的封装,可以发送纯文本邮件, ...