Consider the following code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// create a vector with 20 0s
std::vector<int> arr(20);
for (int i = 0; i < arr.size(); i++)
std::cout<<arr[i];
return 0;
}
The above code creates a vector of 20 0
's and prints each one. If I change the constructor to arr (20,1)
it creates a vector of 20 1
s.
If I define a class:
class Rectangle {
int width, height;
public:
Rectangle (int,int);
int area () {return (width*height);}
};
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
And create a vector of Rectangle
s instead of int
s:
int main() {
// create a vector with 20 integer elements
std::vector<Rectangle> arr(20, Rectangle(2,2));
for (int i = 0; i < arr.size(); i++)
std::cout<<arr[i].area();
return 0;
}
Twenty 4
s get printed. However, when I try:
std::vector<Rectangle> arr(20);
I get:
prog.cpp: In constructor 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = Rectangle; _Alloc = std::allocator<Rectangle>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = Rectangle; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Rectangle>]':
prog.cpp:19:34: error: no matching function for call to 'Rectangle::Rectangle()'
std::vector<Rectangle> arr(20);
Do I need to define a constructor with no arguments to make this work? And in general, what happens when I don't provide a second argument to the vector
constructor when I'm using non-primitive types?
The syntax for assigning values from an array or list: vectorname. assign(arr, arr + size) Parameters: arr - the array which is to be assigned to a vector size - number of elements from the beginning which has to be assigned.
You will not be able to create a Vector, which is not initialized. The default constructor constructs an empty container with a default-constructed allocator. Here are some examples on how to initialise a Vector if you already know its content or size.
You can use: std::vector<int> v(100); // 100 is the number of elements. // The elements are initialized with zero values.
Do I need to define a constructor with no arguments to make this work?
Yes, see this link : http://en.cppreference.com/w/cpp/container/vector/vector.
Here is the related constructor of std::vector
.
explicit vector( size_type count, const T& value = T(), const Allocator& alloc = Allocator());
Without the second parameter, it is assumed to be T()
via default parameter.T()
will become Rectangle()
in your case.
When you call primitive with std::vector
, it will act similar.
Roughly speaking, it will call default-constructor-syntax on the primitive e.g. int()
, which yield 0.
This ideone demo shows that int()==0
.
A little tinkering with your prameterized constructor can solve the problem. We have to just provide default parameter here.
Rectangle::Rectangle (int a=1, int b=1){
width = a;
height = b;
}
Now, if we call std::vector arr(20);
it will execute properly and give your desired output.
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