Templates and STL

前端 未结 6 1748
情歌与酒
情歌与酒 2021-01-05 18:31

The following code represents a container based on std::vector

template 
struct TList
{
    typedef std::vector  Type;
};


         


        
6条回答
  •  清酒与你
    2021-01-05 18:40

    Is it possible to templatize std::vector and create a general container, something like that?

    No. You would have to templatize the function or object using the container -- you couldn't templatize the container itself.

    For example. consider a typical std::find:

    template
    InputIterator find ( InputIterator first, InputIterator last, const T& value )
    {
        for ( ;first!=last; first++) if ( *first==value ) break;
        return first;
    }
    

    This works for any container, but doesn't need a tempalte with the container at all.

    Also, given that it looks what you're trying to do is make container independent code, you might want to buy or borrow yourself a copy of Scott Meyers' Effective STL and read Item 2: Beware the illusion of container-independent code.

提交回复
热议问题