Greetings to all! I need to make a two-dimensional vector (table), the rows of which will be filled with one-dimensional vectors. One-dimensional vectors are the representation of a binary number, which at each iteration should change by a certain step. The main problem is to copy the values ​​of the one-dimensional vector into the necessary line of the two-dimensional vector. Tried as follows:

for (int i = 1; i < 25; i++) { for (int j = 0; j < 5; j++) { table[i][j] = to_binary(startvalue)[j]; cout << table[i][j] << " "; } startvalue += step; } 

where to_binary(startvalue) is a function that returns the vector of bits of the translated number. However, nothing is displayed on the screen, and the compiler does not detect any errors. Using debugging was able to establish that the problem lies in the string table[i][j] = to_binary(startvalue)[j]; but failed to solve it. Thank you in advance for all the help!

    1 answer 1

    Well, if I understood you correctly ... You gave too little data, so, here's the code, it gives out a number in binary representation 25 times, successively increasing it by the given step .

     #include <vector> #include <bitset> using std::vector; using std::bitset; vector<int> to_binary(int sv); int main() { vector<vector<int>> table(25); int startvalue(5); int step(4); for (int i = 0; i < table.size(); i++) { auto vect = to_binary(startvalue); for(int j = 0;j<vect.size();j++) { table[i].push_back(vect[j]); std::cout << table[i][j]; } std::cout << std::endl; startvalue += step; } return 0; } vector<int> to_binary(int sv) { bitset<32> num(sv); vector<int> res; for (int i(0); i < num.size(); i++) res.push_back(num[i]); return res; } 

    Everything is given to the console correctly.

    I draw your attention that the bitset works correctly only with integer primitives, for real you need to use something else (you can write the algorithm manually, these are details).

    PS:

    bitset <32> num (sv);

    32 - because the size of an int in a 32-bit architecture (if memory serves me, and I say everything is right) is 4 bytes * 8 bits = 32 bits.