Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values when creating a vector, C++

Tags:

c++

vector

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 1s.

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 Rectangles instead of ints:

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 4s 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?

like image 600
Adam Avatar asked Mar 12 '17 04:03

Adam


People also ask

How do you set a value vector?

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.

Does std::vector need to be initialized?

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.

How do you initialize a vector with all values 0?

You can use: std::vector<int> v(100); // 100 is the number of elements. // The elements are initialized with zero values.


2 Answers

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.

like image 59
javaLover Avatar answered Oct 20 '22 03:10

javaLover


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.

like image 3
the_spectator Avatar answered Oct 20 '22 03:10

the_spectator