Setting vectors sieze

hello, how do i set the size of vector in the class, that later i could assing value 0 to it using constructor, Its a task from the book:

The class should provide a constructor that accepts no arguments that initializes the BigUnsignedInt;

Now i am getting the error because my vector has 0 size and i get out of scope by assing 0 to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 main.cp
#include <iostream>
#include "bigUnisigednInt.h"

int main()
{
	BigUnisigedInt vec;
	std::cout << vec.fullNumber();


	system("pause");
	return 0;
}


BigUnsisiginedInt.h----------------------------
#pragma once
#include <vector>
class BigUnisigedInt
{
private:
	std::vector<int>vector;
public:
	//constructor
	BigUnisigedInt()
	{
		vector[0] = 0;
	}

	//method
	int fullNumber()
	{
		int size = 0;
		int num = 0;
		int i = 0;
		int lenght = vector.size();
		while (i < lenght)
		{
			std::cout << i << std::endl;
			num = num + vector[i];
			if (i < lenght - 1)
			{
				num = num * 10;
			}
			i++;
		}

		return num;
	}
};
Last edited on
You can use the resize function.
 
vector.resize(1);
This will set the size to 1 and initialize the one new elements to 0.

If you want to explicitly specify which value you want to give to the new elements you can pass the value as a second argument.
 
vector.resize(1, 0);

If you just want to add an element to the end of the vector the simplest is usually to use push_back.
 
vector.push_back(0);
This adds a new element with value 0 to the end of the vector.

Since you know the size when you create the vector you might want to construct the vector with that size.

You can do this using the member initializer list.
1
2
3
4
BigUnisigedInt()
:	vector(1)
{
}
This works the same as resize. You can pass a second argument if you want the element's initial value to be something other than 0.

You can also use the brace-initialization syntax.
1
2
3
4
BigUnisigedInt()
:	vector{0}
{
}
If you want the vector to have more elements you can just list them between { } with commas in between (e.g. vector{0, 1, 8} creates a vector with three elements having the values 0, 1 and 8).

It is also possible to use this kind of initialization directly when defining the member variable.
 
std::vector<int> vector{0};
This might be suitable if you always want the vector to have at least one element. If you add more constructors you don't need to repeat the same initialization in each of them.
Last edited on
Hi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BigUnisigedInt
{
private:
	std::vector<int>vector;
public:
	//constructor
	BigUnisigedInt(): vector(10) // vector with size 10 
	{
             vector[0] = 0; // assign just the first element to zero
         }
        BigUnisigedInt(): vector(10, 0) // vector with size 10 and all elements are 0 
	{}
        BigUnisigedInt(): vector(8, 5) // vector with size 8 and all elements are 5 
	{}
};
Thanks, fixed it, used resize function in the constructor to get result.
Modification of existing object is not initialization.
You should strongly consider the member initializer list syntax.

C++11 did add convenience syntax for default values:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>

struct Foo {
    std::vector<int> v {6};
};

int main() {
    Foo bar;
    for ( auto man : bar.v ) {
        std::cout << "Number " << man << '\n';
    }
}
Note that you probably can't return the actual number from fullNumber(). The point of a BigUnsignedInt class is usually that it lets you represent an integer that's too big to fit within the built-in types.

I prefer to use for loops whenever possible to express the "loopiness" logic. That way the body of the loop only deals with what you're doing to each item. It makes loops much clearer. In other words, you want a for loop that says
1
2
3
for (each item) {
   // do something with the item
}

In your case:
1
2
3
4
for (i=0; i< vector.size(); ++i) {
	std::cout << i << std::endl;  // did you mean vector[i] instead of [i]?
	num = num*10 + vector[i];
}

Topic archived. No new replies allowed.