原文地址: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-源码示例的更多相关文章

  1. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  2. vector源码(参考STL源码--侯捷):空间分配导致迭代器失效

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  3. vector源码2(参考STL源码--侯捷):空间分配、push_back

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  4. vector源码1(参考STL源码--侯捷):源码

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...

  5. 个人学习-STL深入学习01-vectory源码研习 // 需要补充

    参考资料: [1]博主:一枚程序员 STL源码剖析--vector https://www.cnblogs.com/sooner/p/3273395.html [2]博主:劲蜡鸡腿堡 vector源码 ...

  6. ArrayList和LinkedList和Vector源码分析

    ArrayList源码: private static final int DEFAULT_CAPACITY = 10;//默认长度 /** * Shared empty array instance ...

  7. 转:【Java集合源码剖析】Vector源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865   Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量 ...

  8. Stack和Vector源码分析

    Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...

  9. Vector源码分析和实例应用

    1.Vector介绍 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. Vector ...

随机推荐

  1. 异常:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/app/userInfoMaint/getProvince.do'

    调试代码时出现异常:java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path '/app/user ...

  2. PHP屏蔽关键字实现方法

    方法一: 思路 用正则去匹配关键字,把关键字用别的字符替换: 1 2 3 $str = "/你大爷|你麻痹|什么玩意|SB|你他妈/";  // 关键字正则字符串  $string ...

  3. 开坑数位dp

    [背景] 在10月3日的dp专练中,压轴的第6题是一道数位dp,于是各种懵逼. 为了填上这个留存已久的坑,蒟蒻chty只能开坑数位dp了. [例题一][HDU2089]不要62 题目大意:给你一个区间 ...

  4. laravel中的模型关联之(一对一)

    一对一 一对一的关联关系就相当于,赞和文章之间的关系,一个用户只能赞一次一个文章,只能和文章发生一次关系, 用户和赞的文章是唯一的一份,这里举得是判断用户是否赞过某个文章 一个参数都是你要获取的模型, ...

  5. 11-vector的使用

    C++ vector用法(详解!!函数,实现) 原创 2016年09月30日 01:13:40 7862 1,简述一下vector的基本操作,它的size,capacity(),clear,rever ...

  6. c语言交换两个变量的值

    有两个变量a 和b,想要交换它们的值 int a,b; 能不能这样操作呢? b=a; a=b; 不能啊,这样操作的意思是把a的值放到b中,然后b中的值已经被覆盖掉了,已经不是b原来的那个值了,所以是没 ...

  7. python解释器的下载和安装

    1.python解释器的下载 python这样的语言,需要一个解释器.而且解释器还有多种语言的实现,我们介绍的是最常用的C语言的实现,称之为Cpython.Python通过在各种操作系统上都有各自的解 ...

  8. JavaWeb中的路径问题

    JavaWEB 开发中的 / 的含义 ①.当前WEB应用的根路径(http://localhost:8080/contextPath/) 请求转发时:request.getRequestDispath ...

  9. New for ASP.NET Web Pages: Conditional attributes

    from:http://www.mikepope.com/blog/AddComment.aspx?blogid=2353 March 01, 2012 The beta release of ASP ...

  10. OM—>AR相关会计科目

    业务会计核算 挑库:           借:发出商品 (递延销货成本) 贷:发出商品 (递延销货成本)   发运:           借:发出商品 (递延销货成本) 贷:库存商品/原材料 (库存估 ...