自定义STL序列的最小嵌套typedef集合?

时间:2011-01-28 01:04:03

标签: c++ boost stl

应该在符合Sequence概念的自定义STL类中定义的嵌套typedef的最小集合是什么?自定义序列应与以下内容兼容:

  1. std::back_insert_iterator
  2. BOOST_FOREACH
  3. Boost range概念。

2 个答案:

答案 0 :(得分:5)

C ++标准说所有容器必须具有以下typedef(C ++ 03 23.1 /表65):

value_type
reference
const_reference
iterator
const_iterator
difference_type
size_type

可逆容器必须具有以下typedef(C ++ 03 23.1 /表66):

reverse_iterator
const_reverse_iterator

答案 1 :(得分:0)

我想出了这个程序来找到嵌套typedef的最小集合:

#include <iostream>
#include <iterator>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/range/algorithm/sort.hpp>

struct Foo
{
    typedef std::vector<int> Vec;
    typedef Vec::const_reference const_reference;
    typedef Vec::iterator iterator;
    typedef Vec::const_iterator const_iterator;

    iterator begin() {return vec.begin();}
    iterator end() {return vec.end();}
    const_iterator begin() const {return vec.begin();}
    const_iterator end() const {return vec.end();}
    void push_back(const int& n) {vec.push_back(n);}

    Vec vec;
};

int main()
{
    Foo f;
    std::back_insert_iterator<Foo> it(f);
    *it = 2; ++it; *it = 1; ++it;
    boost::sort(f);
    BOOST_FOREACH(int x, f)
    {
        std::cout << x << " ";
    }
}

如您所见,至少需要const_referenceiteratorconst_iterator

我应该提一下,我用gcc 4.4.3和boost 1.43编译了这个。