Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Is it possible to instantiate a `vector` without specifying the type?

Tags:

c++

stl

vector

So basic, but hard to search that in Google for me.

I am doing a C++ training course online and the topic is STL; in this case vector.

Is it possible to instantiate a vector without specifying the type?

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector v1(10, 0);
    cout<<"Size: "<<v1.size()<<endl;
    for(unsigned i = 0; i < v1.size(); ++i)
    {
        cout<< v1[i]<<" ";
    }
    cout<<endl;
    return 0;
}

I think that is wrong, but I see that throughout the course and this confuses me.

When use vector<int> v1(10, 0) then it compiles, and that is how it should be I think.

In the course we are using NetBeans, but I don't think there is a config or parameter or anything that can make that happen, is there?

like image 237
Ely Avatar asked Jul 17 '15 16:07

Ely


People also ask

Can you initialize a vector?

How to Initialize a Vector Using a Constructor in C++ We can also initialize vectors in constructors. We can make the values to be a bit dynamic. This way, we don't have to hardcode the vector's items.

Is it possible to initialize any vector with an array in C++?

You can initialize a vector by using an array that has been already defined. You need to pass the elements of the array to the iterator constructor of the vector class. The array of size n is passed to the iterator constructor of the vector class.

Does std::vector need to be initialized?

when you create a vector it gets default initialized, so it's up to you if you want to initialize it with user default values or not. You will always get an empty vector container if you don't initialize it.


2 Answers

C++17 does support instantiation of vectors without type. Please see this article, https://en.cppreference.com/w/cpp/language/class_template_argument_deduction

for more information.

So, for instance writing this will work:

vector v {1, 2, 3};  // instead of vector<int>

if you compile with this "-std=c++17" flag.

like image 92
MSC Avatar answered Oct 22 '22 11:10

MSC


Templates in general

Ignoring the details of std::vector for the moment, it is possible to define a default type for a template parameter of a class template. For example:

template <class T = int>
class foo { 
    T *bar;
};

In such a case, you don't have to specify a type to instantiate that template. At the same time, you do have to include a template parameter list. The trick is that the list can be empty, so you could instantiate this template in any of the following ways:

foo<long> a; // instantiate over long. The `int` default is just ignored
foo<int>  b; // instantiate over int. Still doesn't use default
foo<>     c; // also instantiates over int

std::vector specifically

std::vector does use a default parameter for the type of the allocator, but does not provide a default for the type being stored, so the definition looks something like this:

template <class T, class allocator = std::allocator<T>>
class vector
// ...

So, if you don't specify otherwise, the allocator type for the vector will be an std::allocator instantiated over the same type as you're storing--but you do always have to specify a type you're storing, because no default is provided for that type.

Summary

It is definitely possible to specify defaults for all the parameters to a template, in which case it's possible to instantiate the template without (explicitly) specifying the type at the instantiation--but std::vector has one template parameter for which no default is provided, so to instantiate vector, you must specify a type for that parameter.

like image 35
Jerry Coffin Avatar answered Oct 22 '22 10:10

Jerry Coffin