Search code examples
c++vectortypedef

Assign another name/alias for std::vector


I am trying to give std::vector a different name like MyVector, so I did following typedef

typedef std::vector<float> MyVector<float>;

However, visual studio complains on MyVector that "MyVector is not a template"

How do I assign std::vector another name?

I have may MyVector in my code which is essentially std::vector, so I just want to equal std::vector with MyVector so I don't have to change all the MyVector into std::vector.


Solution

  • What you want is an alias template, like this:

    template <typename T>
    using MyVector = std::vector<T>;
    

    That will allow you to use it like this:

    MyVector<float> vec = stuff;
    

    Where vec will be a std::vector<float>.