|
-
December 25th, 2022, 09:36 PM
#1
move data from a 2D vector into a 3D vector, three times. the sizes of the 2D vector
I am collecting numerical data into a 2D vector. At the end of every cycle that 2D vector dumps into a 3D vector. The size of the 2D vector varies.
I am getting a vector overload during the use of the 3D vector (only when the rows and columns aren't the same as the previous entry).
I have successfully completed this exact challenge using a changing 1D that goes into a 2D with no errors. So, I am suspicious that my 2D vector is not being cleared properly, using clear(). Perhaps there is an additional built-in function within C++?
After several attempts searching on google and SOF, I am amazed that I can't find someone who has asked how to do this.
It seemes like an extremely common necessity.
Code:
#include <iostream>
#include <vector>
#include <random>
int main()
{
// vector init
std::vector<std::vector<int>> vec_input_ctrl;
std::vector <int> vec_ctrl_values;
std::vector<std::vector<int>> vec_collected_values;
std::vector<std::vector<std::vector<int>>> vec_collected_values_3D;
// random number generatior
std::random_device random_device_01;
std::mt19937 randgen_01(random_device_01());
// variable init
int var_int;
int column_count = 0;
int row_count = 0;
int cycle_count = 5;
// 3D loop
for (int var_cycle = 0; var_cycle < cycle_count; var_cycle++)
{
std::uniform_int_distribution<> columns(10, 20);
column_count = columns(randgen_01);
std::uniform_int_distribution<> rows(3, 10);
row_count = rows(randgen_01);
// verify input
std::cout << "verify input" << std::endl;
for (int var_create_vector_a = 0; var_create_vector_a < column_count; var_create_vector_a++)
{
vec_input_ctrl.emplace_back(column_count);
for (int var_output_vector = 0; var_output_vector < row_count; var_output_vector++)
{
vec_input_ctrl[var_create_vector_a].push_back(0);
std::cout << vec_input_ctrl[var_create_vector_a][var_output_vector] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
// 2D loop
for (int var_create_vector_a = 0; var_create_vector_a < column_count; var_create_vector_a++)
{
// create blank 1D vector
std::cout << "create blank 1D vector" << std::endl;
for (int var_create_vector_b = 0; var_create_vector_b < vec_input_ctrl[var_create_vector_a].size(); var_create_vector_b++)
{
vec_ctrl_values.push_back(vec_input_ctrl[var_create_vector_a][var_create_vector_b]);
std::cout << vec_ctrl_values[var_create_vector_b] << " ";
}
std::cout << std::endl;
// randomly adjust values in 1D vector
std::cout << "randomly adjust values in 1D vector" << std::endl;
for (int var_adjust_vector = 0; var_adjust_vector < row_count; var_adjust_vector++)
{
std::uniform_int_distribution<> rand_amount(0, 9);
var_int = rand_amount(randgen_01);
vec_ctrl_values[var_adjust_vector] = var_int;
std::cout << vec_ctrl_values[var_adjust_vector] << " ";
}
std::cout << std::endl;
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// verify the vector size inside the 2D loop
if (vec_collected_values.size() > 0)
{
std::cout << "vec_collected_values 1D size inside the 2D loop is: " << vec_collected_values.size() << std::endl;
if (vec_collected_values[0].size() > 0) std::cout << "vec_collected_values 2D size inside the 2D loop is: " << vec_collected_values[0].size() << std::endl;
else std::cout << "vec_collected_values 2D size inside the 2D loop is empty" << std::endl;
}
else std::cout << "vec_collected_values 2D size inside the 1D loop is empty" << std::endl;
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// move 1D vector into 2D vector
std::cout << "move 1D vector into 2D vector" << std::endl;
vec_collected_values.emplace_back(row_count);
for (int var_output_vector = 0; var_output_vector < row_count; var_output_vector++)
{
vec_collected_values[var_create_vector_a][var_output_vector] = vec_ctrl_values[var_output_vector];
std::cout << vec_collected_values[var_create_vector_a][var_output_vector] << " ";
}
// resets
vec_ctrl_values.clear();
std::cout << std::endl << std::endl;
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// verify the vector size
if (vec_collected_values.size() > 0)
{
std::cout << "vec_collected_values 1D size after the 2D loop is: " << vec_collected_values.size() << std::endl;
if (vec_collected_values[0].size() > 0) std::cout << "vec_collected_values 2D size after the 2D loop is: " << vec_collected_values[0].size() << std::endl;
else std::cout << "vec_collected_values 2D size after the 2D loop is empty" << std::endl;
}
else std::cout << "vec_collected_values 2D size after the 1D loop is empty" << std::endl;
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// verify 2D vector
std::cout << "verify 2D vector" << std::endl;
for (int var_output_vector_a = 0; var_output_vector_a < column_count; var_output_vector_a++)
{
for (int var_output_vector_b = 0; var_output_vector_b < row_count; var_output_vector_b++)
{
std::cout << vec_collected_values[var_output_vector_a][var_output_vector_b] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
// move 2D vector into 3D vector
std::cout << "move 2D vector into 3D vector" << std::endl;
for (int var_output_vector_a = 0; var_output_vector_a < column_count; var_output_vector_a++)
{
vec_collected_values_3D.emplace_back(column_count, std::vector <int>(row_count));
for (int var_output_vector_b = 0; var_output_vector_b < row_count; var_output_vector_b++)
{
vec_collected_values_3D[var_cycle][var_output_vector_a][var_output_vector_b] = vec_collected_values[var_output_vector_a][var_output_vector_b];
std::cout << vec_collected_values_3D[var_cycle][var_output_vector_a][var_output_vector_b] << " ";
}
std::cout << std::endl;
}
vec_collected_values[row_count].clear();
vec_collected_values.clear();
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// verify the vector size
if (vec_collected_values.size() > 0)
{
std::cout << "vec_collected_values 1D size after.clear() the 2D loop is: " << vec_collected_values.size() << std::endl;
if (vec_collected_values[0].size() > 0) std::cout << "vec_collected_values 2D size after.clear() the 2D loop is: " << vec_collected_values[0].size() << std::endl;
else std::cout << "vec_collected_values 2D size after.clear() the 2D loop is empty" << std::endl;
}
else std::cout << "vec_collected_values 2D size after.clear() the 1D loop is empty" << std::endl;
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
std::cout << std::endl << std::endl;
std::cout << "end of 2D loop #" << var_cycle << std::endl;
std::cout << std::endl << std::endl;
std::cout << "end of 2D loop #" << var_cycle << std::endl;
std::cout << std::endl << std::endl;
std::cout << std::endl << std::endl;
}
// verify 3D vector
std::cout << "verify 3D vector" << std::endl;
for (int var_cycle = 0; var_cycle < cycle_count; var_cycle++)
{
std::cout << "vec_collected_values_3D #" << var_cycle << std::endl;
for (int var_output_vector_a = 0; var_output_vector_a < column_count; var_output_vector_a++)
{
for (int var_output_vector_b = 0; var_output_vector_b < row_count; var_output_vector_b++)
{
std::cout << vec_collected_values_3D[var_cycle][var_output_vector_a][var_output_vector_b] << " ";
}
std::cout << std::endl;
}
vec_collected_values.clear();
std::cout << std::endl << std::endl;
}
return 0;
}
linki
https://godbolt.org/z/G6q5rac5d
Last edited by 2kaud; December 28th, 2022 at 04:28 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|