use a content of one vector into other vector

I introduced the random generator binary vector like this:

vector<int> b;
srand(time(0));
for (int i = 0; i < 11; i++)
b.push_back(i);
for (int i = 0; i < b.size(); i++)
{
b[i] = rand() % 2;
std::cout << b[i] % 2 << " ";
}




and I want to use these binary numbers in the following vector (use these generated 11 numbers as a size of the following vector) :



vector<int> d(11);
for (i = 11; i>0; i--)
d[i] = b[i];
std::cin >> d[i];
while ((11 + r + 1)> pow(2, r))
r++;
std::cout << d[i];
std::cout << " The number of (r): " << r ;
Last edited on
What exactly is your question? Note that you aren't copying every element.
for (i = 11; i>0; i--)
This is not reaching i == 0, the first element of the vectors. And i == 11 is not a valid index. Array/vector indices start at 0 and go to size-1.

You could do something like this
1
2
for (i = 11; i>0; i--)
    d[i-1] = b[i-1];


Please use code tags. That means wrapping the code parts of your posts with [code] and [/code]
You can directly assign one vector to another:

vector<int> d = b;

Note that for a vector/array that contains 11 elements the last valid index is 10 because the first index is 0. Thus i = 11 is out of bounds. It needs to be i = 10.
Registered users can post here. Sign in or register to post.