Minimal set of nested typedef for custom STL sequence?

What is the minimum set of nested typedefs that must be defined in a custom STL class that conforms to the concept of Sequence? The user sequence must be compatible with:

+3
source share
2 answers

The C ++ standard says that all containers should have the following typedefs (C ++ 03 23.1 / Table 65):

value_type
reference
const_reference
iterator
const_iterator
difference_type
size_type

Reversible containers should have the following typedefs (C ++ 03 23.1 / Table 66):

reverse_iterator
const_reverse_iterator
+5
source

, typedefs:

#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_reference, iterator const_iterator.

, gcc 4.4.3 1.43.

0

All Articles