简体   繁体   English

为 std::vector 分配另一个名称/别名

[英]Assign another name/alias for std::vector

I am trying to give std::vector a different name like MyVector , so I did following typedef我想给std::vector一个不同的名字,比如MyVector ,所以我按照 typedef

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

However, visual studio complains on MyVector that "MyVector is not a template"但是,Visual Studio 在MyVector上抱怨“MyVector 不是模板”

How do I assign std::vector another name?如何为std::vector分配另一个名称?

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 .我的代码中可能有MyVector ,它本质上是std::vector ,所以我只想将std::vectorMyVector相等,所以我不必将所有MyVector更改为std::vector

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> .其中vec将是std::vector<float>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM