Yan Zhu
Yan Zhu

Reputation: 4346

Can typedef used for vector in general?

In general, we can do

typedef std::vector<int> container1;
typedef std::vector<char> container2;

But it looks like we can't do something like.

typedef vector container;
container<int> ins;

Is there anyway to achieve this? What I can think of is using macro.

Upvotes: 2

Views: 3034

Answers (1)

perreal
perreal

Reputation: 98068

C++11 aliases allows this:

#include <vector>

template<class T>
using Vec = std::vector<T>;   
Vec<int> v;   // same as std::vector<int> v;

also see this

And in a similar fashion, you can rewrite the typedefs in C++11, as:

using container1 = std::vector<int>;
using container2 = std::vector<char>;

These are exactly same as the typedefs in your question.

Upvotes: 11

Related Questions