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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With