Line 35: Quote from a STL book: ContType c(beg,end) Creates a container and initializes it with copies of all elements of [beg,end)
Did I get it wrong ?
About reserve, what else could I use to change vector size in such a way that it can run properly ?
The size of the vector is way too large. Factorial 69 is in the order of 1E500 which is bigger than a double, but you are using int which only goes to 2.147 billion.
2) How to initialize the vector in such a way, that it wont crash (runtime error because of lack of space, or idk) ?
3) To find out how many different numbers are there in the vector, should I use the function unique_copy, so that I erase the duplicates, and then compare each element with the following, and increase the counter ?
I'm confused. The size of my vector is too small, or too large ?
I am saying that you are using an int to hold the factorial, the max size that this will hold is 2.147 billion. I am not sure what factorial will produce a number this size, but i do know that factorial 69 is in the order of 1E500, which is bigger than what a double can hold. Your vector holds 10001 ints, so even if you were using doubles 69 would be too much.
It is important to realise what maximums different data types can hold.
Use resize instead of reserve to set the size of the vector.
Line 35: Quote from a STL book: ContType c(beg,end) Creates a container and initializes it with copies of all elements of [beg,end)
Did I get it wrong ?
The book is talking about iterators. Integers are not iterators so the constructor that is being used is the second constructor on this page http://www.cplusplus.com/reference/stl/vector/vector/ (the book was talking about the third one).
I did search on google, and all the links (that are related to my search) are from www.cplusplus.com reference. I did check the reference before coming on the forums, and that's why I'm here. I don't understand how to initialise the vector.
1 2 3 4
v.resize() // not good - that's what I understand.
v.size() // doesn't help
v.capacity() // doesn't help
v.reserve() // doesn't help
If you are going to use ints, then set a limit of say 20, this will be the size of the vector and the limit for calculating the factorial. Once you have it working, then look at changing the limit to a larger value.
To do this you really need to use doubles, (because it grows so quickly). Using unsigned long long doesn't help much. Doubles go to a max of 1.7E308, so be careful that your factorial answer doesn't exceed this.
// constructing vectors
#include <iostream>
#include <vector>
usingnamespace std;
int main ()
{
unsignedint i;
// constructors used in the same order as described above:
vector<int> first; // empty vector of ints
vector<int> second (4,100); // four ints with value 100
vector<int> third (second.begin(),second.end()); // iterating through second
vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
cout << "The contents of fifth are:";
for (i=0; i < fifth.size(); i++)
cout << " " << fifth[i];
cout << endl;
return 0;
}