Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between doing vector<vector<T...>> and vector<vector<T>...>

Tags:

c++

c++11

I saw code like this earlier:

using A = std::vector<std::vector<T>...>

where T is a variadic list of template arguments. I wanted to know what the difference is between putting the parameter pack at the end of the last angle bracket and the first. For example:

using B = std::vector<std::vector<T...>>;

Both of these two compile fine but I don't know what the difference is.

Can someone explain? Thanks.

like image 724
Me myself and I Avatar asked May 05 '13 16:05

Me myself and I


People also ask

What does vector int v [] means?

vector<int> v[] is an array of vectors. That is, it is an array which contains vectors as its elements. So, you cannot change the size of the array part, but we can add to its elements which is vector.

What does vector Int A N means in C++?

std::vector<int> v(N) declares a variable v with the type of std::vector<int> and initializes the vector's size to hold N ints which are default initialized, meaning in this case that their initial value is undefined.

What is Push_back?

push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.

How do I get the size of a vector array in C++?

To get the size of a C++ Vector, you can use size() function on the vector. size() function returns the number of elements in the vector.

What is the difference between a vector and a co-vector?

Vectors and co-vectors are usually “columns of numbers” or “lines of numbers,” respectively. In short, a vector will always be a one-dimensional tensor; if you have a one-dimensional tensor, it will surely be either a vector or co-vector. Two-dimensional tensors are known as matrices.

What is a vector in physics?

A Vector is a specific type of Tensor. In particular, it is a One Dimensional Tensor. A Vector gives you a magnitude and a Direction.

What is the difference between a vector and a matrix?

A vector is always a one-dimensional tensor, and a one-dimensional tensor is always either a vector or a co-vector. Matrix is the name given to two-dimensional tensors. Vector is a one-dimensional array of numbers, often known as a matrix, where m or n = 1.

What is the difference between a tensor and a vector?

A tensor is a generalization of a vector (not a matrix, exactly). A vector is a tuple that obeys the correct transformation laws - for example, if you perform a rotation represented by matrix R, the new vector V' = RV. A tensor is a generalization of this to more dimensions. It takes one copy of R for each rank of the tensor.


1 Answers

In a pack expansion the pattern that precedes the ... is repeated for each element of the pack, so vector<T>... means expand into vector<T1>, vector<T2>, vector<T3> whereas vector<T...> means expand into vector<T1, T2, T3>

If the parameter pack only has one element they're the same, but consider if the parameter pack has two elements, it should be obvious that

std::vector<std::vector<T1>, std::vector<T2>>

and

std::vector<std::vector<T1, T2>>

are not the same. The first one will not compile, the second template parameter for std::vector must be an allocator type, not a vector. The second one will compile if T2 is an allocator type.

like image 63
Jonathan Wakely Avatar answered Sep 24 '22 16:09

Jonathan Wakely