编写自己的 STL 容器

是否有关于如何编写新容器的准则,这些新容器的行为会像任何 STL容器一样?

67063 次浏览

您将需要阅读 C + + 标准关于容器和 C + + 标准对容器实现的要求的章节。

C + + 03标准的相关章节是:

第23.1节货柜规定

C + + 11标准中的相关章节是:

第23.2节货柜规定

C + + 11标准的最终草案是免费提供的 译自: 美国《科学》杂志网站(http://www.open-std.org/jtc1/sc22/wg21/docs/paper/2011/n3242.pdf)原著: http://www.open-std.org/jtc1/sc22/wg21/docs/paper/2011/n3242.pdf

您也可以阅读一些优秀的书籍,这些书籍将帮助您从容器用户的角度理解需求。两本很容易让我印象深刻的好书是:

Scott Meyers & 有效的 STL
C++标准程式库: 尼古拉 · 约瑟蒂尔斯(Nicolai Josutils)

下面是我从23.2.1拼凑起来的一个序列伪容器。注意,iterator_category应该是 std::input_iterator_tagstd::output_iterator_tagstd::forward_iterator_tagstd::bidirectional_iterator_tagstd::random_access_iterator_tag中的一个。还要注意的是,下面的 严格来说比要求更严格,但这是想法。注意,绝大多数的“标准”函数在技术上是可选的,因为迭代器非常棒。

template <class T, class A = std::allocator<T> >
class X {
public:
typedef A allocator_type;
typedef typename A::value_type value_type;
typedef typename A::reference reference;
typedef typename A::const_reference const_reference;
typedef typename A::difference_type difference_type;
typedef typename A::size_type size_type;


class iterator {
public:
typedef typename A::difference_type difference_type;
typedef typename A::value_type value_type;
typedef typename A::reference reference;
typedef typename A::pointer pointer;
typedef std::random_access_iterator_tag iterator_category; //or another tag


iterator();
iterator(const iterator&);
~iterator();


iterator& operator=(const iterator&);
bool operator==(const iterator&) const;
bool operator!=(const iterator&) const;
bool operator<(const iterator&) const; //optional
bool operator>(const iterator&) const; //optional
bool operator<=(const iterator&) const; //optional
bool operator>=(const iterator&) const; //optional


iterator& operator++();
iterator operator++(int); //optional
iterator& operator--(); //optional
iterator operator--(int); //optional
iterator& operator+=(size_type); //optional
iterator operator+(size_type) const; //optional
friend iterator operator+(size_type, const iterator&); //optional
iterator& operator-=(size_type); //optional
iterator operator-(size_type) const; //optional
difference_type operator-(iterator) const; //optional


reference operator*() const;
pointer operator->() const;
reference operator[](size_type) const; //optional
};
class const_iterator {
public:
typedef typename A::difference_type difference_type;
typedef typename A::value_type value_type;
typedef typename const A::reference reference;
typedef typename const A::pointer pointer;
typedef std::random_access_iterator_tag iterator_category; //or another tag


const_iterator ();
const_iterator (const const_iterator&);
const_iterator (const iterator&);
~const_iterator();


const_iterator& operator=(const const_iterator&);
bool operator==(const const_iterator&) const;
bool operator!=(const const_iterator&) const;
bool operator<(const const_iterator&) const; //optional
bool operator>(const const_iterator&) const; //optional
bool operator<=(const const_iterator&) const; //optional
bool operator>=(const const_iterator&) const; //optional


const_iterator& operator++();
const_iterator operator++(int); //optional
const_iterator& operator--(); //optional
const_iterator operator--(int); //optional
const_iterator& operator+=(size_type); //optional
const_iterator operator+(size_type) const; //optional
friend const_iterator operator+(size_type, const const_iterator&); //optional
const_iterator& operator-=(size_type); //optional
const_iterator operator-(size_type) const; //optional
difference_type operator-(const_iterator) const; //optional


reference operator*() const;
pointer operator->() const;
reference operator[](size_type) const; //optional
};


typedef std::reverse_iterator<iterator> reverse_iterator; //optional
typedef std::reverse_iterator<const_iterator> const_reverse_iterator; //optional


X();
X(const X&);
~X();


X& operator=(const X&);
bool operator==(const X&) const;
bool operator!=(const X&) const;
bool operator<(const X&) const; //optional
bool operator>(const X&) const; //optional
bool operator<=(const X&) const; //optional
bool operator>=(const X&) const; //optional


iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
iterator end();
const_iterator end() const;
const_iterator cend() const;
reverse_iterator rbegin(); //optional
const_reverse_iterator rbegin() const; //optional
const_reverse_iterator crbegin() const; //optional
reverse_iterator rend(); //optional
const_reverse_iterator rend() const; //optional
const_reverse_iterator crend() const; //optional


reference front(); //optional
const_reference front() const; //optional
reference back(); //optional
const_reference back() const; //optional
template<class ...Args>
void emplace_front(Args&&...); //optional
template<class ...Args>
void emplace_back(Args&&...); //optional
void push_front(const T&); //optional
void push_front(T&&); //optional
void push_back(const T&); //optional
void push_back(T&&); //optional
void pop_front(); //optional
void pop_back(); //optional
reference operator[](size_type); //optional
const_reference operator[](size_type) const; //optional
reference at(size_type); //optional
const_reference at(size_type) const; //optional


template<class ...Args>
iterator emplace(const_iterator, Args&&...); //optional
iterator insert(const_iterator, const T&); //optional
iterator insert(const_iterator, T&&); //optional
iterator insert(const_iterator, size_type, T&); //optional
template<class iter>
iterator insert(const_iterator, iter, iter); //optional
iterator insert(const_iterator, std::initializer_list<T>); //optional
iterator erase(const_iterator); //optional
iterator erase(const_iterator, const_iterator); //optional
void clear(); //optional
template<class iter>
void assign(iter, iter); //optional
void assign(std::initializer_list<T>); //optional
void assign(size_type, const T&); //optional


void swap(X&);
size_type size() const;
size_type max_size() const;
bool empty() const;


A get_allocator() const; //optional
};
template <class T, class A = std::allocator<T> >
void swap(X<T,A>&, X<T,A>&); //optional

此外,每当我制作一个容器时,我都或多或少地使用类进行测试,如下所示:

#include <cassert>
struct verify;
class tester {
friend verify;
static int livecount;
const tester* self;
public:
tester() :self(this) {++livecount;}
tester(const tester&) :self(this) {++livecount;}
~tester() {assert(self==this);--livecount;}
tester& operator=(const tester& b) {
assert(self==this && b.self == &b);
return *this;
}
void cfunction() const {assert(self==this);}
void mfunction() {assert(self==this);}
};
int tester::livecount=0;
struct verify {
~verify() {assert(tester::livecount==0);}
}verifier;

制作 tester对象的容器,并在测试容器时调用每个对象的 function()。不要创建任何全局 tester对象。如果您的容器在任何地方作弊,这个 tester类将 assert,您将知道您在某个地方意外作弊。

下面是一个非常简单的伪向量实现,它基本上是 std::vector的包装器,有自己的(但是实际的)迭代器,它模仿 STL 迭代器。同样,迭代器非常简单,跳过了许多概念,比如 const_iterator、有效性检查等等。

代码可以立即运行。

#include <iostream>
#include <string>
#include <vector>


template<typename T>
struct It
{
std::vector<T>& vec_;
int pointer_;


It(std::vector<T>& vec) : vec_{vec}, pointer_{0} {}


It(std::vector<T>& vec, int size) : vec_{vec}, pointer_{size} {}


bool operator!=(const It<T>& other) const
{
return !(*this == other);
}


bool operator==(const It<T>& other) const
{
return pointer_ == other.pointer_;
}


It& operator++()
{
++pointer_;
return *this;
}


T& operator*() const
{
return vec_.at(pointer_);
}
};


template<typename T>
struct Vector
{
std::vector<T> vec_;


void push_back(T item)
{
vec_.push_back(item);
};


It<T> begin()
{
return It<T>(vec_);
}


It<T> end()
{
return It<T>(vec_, vec_.size());
}
};


int main()
{
Vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);


bool first = true;
for (It<int> it = vec.begin(); it != vec.end(); ++it)
{
if (first) //modify container once while iterating
{
vec.push_back(4);
first = false;
}


std::cout << *it << '\n'; //print it
(*it)++;                  //change it
}


for (It<int> it = vec.begin(); it != vec.end(); ++it)
{
std::cout << *it << '\n'; //should see changed value
}
}