1
votes

I've written a lot of code using std::vector<T> and std::vector<T>::iterator. Now I've decided to replace the vector container with a circular buffer from boost, namely boost::circular_buffer<T>.

Of course now the compiler will complain for every function that uses std::... where I'm passing the boost::... counterpart. Do I have to rewrite all functions now? I'm asking since the container from boost works exactly the same. Also the Boost documentation states the following:

The circular_buffer is a STL compliant container. It is a kind of sequence similar to std::list or std::deque.

What does the "STL compliant" part mean? Is it referring to what I would like to do (interchangability) or is it simply a mental note for programmers, that the containers work the same way in boost as in STL?

EDIT: To give an example

class Item{ };

class Queue{
private:
    std::vector<Item*> item_vector; // Want to replace only this... 
    std::vector<Item*>::iterator current_position;  // ...and this

public:
    Item* get_current_item() const {
        return *current_position;
    } 

    std::vector<Item*> get_item_vector(){
        return item_vector;
    }
};
1
What sort of functions are you taking about when you say "now the compiler will complain"?user1084944
possible duplicate of stl compliant containerUmNyobe

1 Answers

2
votes

Do I have to rewrite all functions now?

If your functions specifically use vector and its iterator types, then yes, you will have to change them to use different types.

If they are templates, designed to work with any sufficiently compatible container and iterator types, then they should work without change.

What does the "STL compliant" part mean?

It usually means it follows the specification for an iterable sequence defined by the C++ standard library (which was influenced by the ancient STL library, whose name some people loosely use to refer to some or all of the modern standard library).

For example, it has begin() and end() member functions returning an iterator type; and the iterator types can be incremented with ++ and dereferenced with *.

This means that an algorithm implemented as a template, for example:

template <typename InputIterator, typename T>
InputIterator find(InputIterator begin, InputIterator end, T const & value) {
    for (InputIterator it = begin; it != end; ++it) {
        if (*it == value) {
            return it;
        }
    }
    return end;
}

will work for any iterator type that supports these operations. While a non-generic function

void find(some_iterator begin, some_iterator end, some_type t);

will only work for a single specific iterator type, and will have to be changed or duplicated to support others.