[Solved] How to pass the values stored in vector container to a new variable?


double position_array = first_cells();

This tries to invoke first_cells (a vector) as a callable (but it doesn’t implement operator(): https://en.cppreference.com/w/cpp/container/vector).

Assuming that the loop variable is there for a reason, why not use it:

double position_array = first_cells[i];

Note also

  • this does a possibly unwanted conversion of float to double

  • if you want bounds-checking, use

      double position_array = first_cells.at(i);
    

The name position_array does suggest some more confusion. Did you actually want the three-value tupled cell instead of just the first value?


Getting first rows instead of first cells:

auto       ds         = open_dataset(argv[1]);
array_2d_t first_rows = row_from_all_frames(ds, 0);

Which you can use:

size_t     nsamples   = first_rows.size();

std::cout << "no. of samples: " << nsamples << "\n";

// main loop
for (size_t i = 1; i < nsamples; ++i) {
    auto position_array = first_rows[i];
    // double position_array = static_cast<double>(std::rand()) / RAND_MAX;
    std::cout << "position arrays: "       //
              << position_array[0] << ", " //
              << position_array[1] << ", " //
              << position_array[2] << std::endl;
    // append data to the correlator, which possibly computes some time
    // correlations
    //corr.sample(position_array);
}

Note that position_array is a sub array view from multi_array, so the interface is consistent with multi_array types.

Prints, on my system:

no. of samples: 75
position arrays: 80.03, 35.42, 4.35
position arrays: 80.19, 35.62, 4.27
position arrays: 79.78, 35.68, 4.13
position arrays: 79.51, 35.93, 4.1
position arrays: 79.44, 35.46, 4.27
position arrays: 79.5, 35.43, 4.38
position arrays: 79.03, 35.72, 4.54
position arrays: 79.12, 35.89, 4.28
position arrays: 79.04, 36.35, 3.99
position arrays: 79.06, 36.16, 4.52
position arrays: 79.22, 35.96, 4.39
position arrays: 79.07, 35.84, 4.28
position arrays: 79.43, 35.09, 4.4
position arrays: 79.38, 35.13, 3.81
position arrays: 78.87, 35.73, 4.54
position arrays: 79.3, 35.82, 4.33
position arrays: 79.38, 35.45, 3.98
position arrays: 79.5, 35.48, 3.88
position arrays: 79.16, 34.93, 4.35
position arrays: 78.86, 35.2, 4.44
position arrays: 79.15, 35.53, 4.08
position arrays: 79.41, 35.67, 3.87
position arrays: 79.83, 35.61, 4.19
position arrays: 79.63, 35.26, 3.86
position arrays: 79.94, 35.42, 4.11
position arrays: 80.32, 35.06, 4.01
position arrays: 79.99, 35.44, 3.97
position arrays: 79.82, 35.31, 4.07
position arrays: 80, 34.97, 4.07
position arrays: 80.22, 35.07, 3.91
position arrays: 80.38, 35.56, 3.92
position arrays: 80.6, 35.14, 4.11
position arrays: 80.57, 34.93, 4.15
position arrays: 80.05, 35.33, 4.46
position arrays: 80.12, 35.21, 4.2
position arrays: 80.12, 35.39, 3.97
position arrays: 80.19, 35.69, 4.18
position arrays: 80.4, 35, 3.96
position arrays: 80.55, 35.39, 4.26
position arrays: 80.52, 34.85, 4.07
position arrays: 80.57, 34.66, 4.04
position arrays: 80.69, 34.64, 4.05
position arrays: 80.94, 34.53, 3.88
position arrays: 81.12, 33.99, 4.22
position arrays: 81.25, 34.02, 4.08
position arrays: 81.68, 33.82, 4.17
position arrays: 81.75, 33.89, 4.35
position arrays: 82.2, 34.24, 4.28
position arrays: 81.83, 34.51, 4.17
position arrays: 82.17, 34.09, 4.35
position arrays: 82.33, 34.32, 4.3
position arrays: 82.65, 34.35, 4.09
position arrays: 82.44, 34.6, 4
position arrays: 82.51, 34.04, 4.41
position arrays: 82.4, 34.45, 4.34
position arrays: 81.89, 34.48, 4.18
position arrays: 81.59, 34.62, 4.14
position arrays: 81.82, 34.22, 4.34
position arrays: 81.43, 33.95, 4.05
position arrays: 81.35, 33.88, 3.9
position arrays: 81.44, 33.85, 4.24
position arrays: 81.48, 33.39, 4.25
position arrays: 81.51, 33.69, 4.16
position arrays: 81.66, 33.49, 4.34
position arrays: 82.1, 33.45, 4.17
position arrays: 82.61, 33.8, 4.07
position arrays: 82.51, 33.96, 4.5
position arrays: 82.36, 34.13, 4.46
position arrays: 82.46, 34.28, 4.19
position arrays: 82.59, 34, 4.17
position arrays: 82.26, 33.92, 4.44
position arrays: 82.2, 34.06, 4.18
position arrays: 82.24, 34.12, 4.29
position arrays: 82.16, 33.39, 3.94

9

solved How to pass the values stored in vector container to a new variable?