2

I am currently implementing a sliding window functionality for a vector<double>. Problem is I cant seem to cout the values? When I output it i seem to get memeory location, rather than the actual values..

I need to process the data which the window consist of, so having acess to the values would be neat.

typedef double SAMPLE;
std::vector<std::vector<SAMPLES> > loaded_files;
//Some init
std::vector<SAMPLE>::iterator it;
for (it = loaded_file.samples[0].begin() ; (it + (NUM_SECONDS*SAMPLE_RATE)) != loaded_file.samples[0].end(); ++it)
{
     auto window = *it;
     std::cout << "printing the window!" << std::endl;
     std::cout << &(window) << std::endl; // prints out memory location?
}
Lamda
  • 914
  • 3
  • 13
  • 39

2 Answers2

1

Each time you print the window contents, you need to iterate over the window itself. This can be done by changing the contents of your for loop like so:

typedef double SAMPLE;
std::vector<<SAMPLES>> loaded_files;
//Some init
std::vector<SAMPLE>::iterator it;
for (it = loaded_file.samples[0].begin(); (it + (NUM_SECONDS*SAMPLE_RATE)) != loaded_file.samples[0].end(); ++it)
{
    std::cout << "printing the window!" << std::endl;
    std::vector<SAMPLE>::iterator wit; // window iterator
    for (wit = it; wit != it + (NUM_SECONDS*SAMPLE_RATE); ++wit)
    {
        std::cout << *wit << ',';
    }
    std::cout << std::endl;
}

Note that the width of the window is (NUM_SECONDS*SAMPLE_RATE). This could be stored in a variable like window_widthor similar to help with readability.

scrpy
  • 985
  • 6
  • 23
  • what is `(it + (NUM_SECONDS*SAMPLE_RATE))` exactly.. when talking about readability.. ? – Lamda Nov 28 '17 at 13:22
  • It points to (one past) the end of the sliding window of width `window_width = (NUM_SECONDS*SAMPLE_RATE)`. So in the inner loop, in order to iterate over the window, the window iterator `wit` must start at the current position of `it`, and stop at `it + window_width`. – scrpy Nov 28 '17 at 13:30
0

It is not clear what window object is holding. But in your below statement you are providing the address of window. Hence it is printing the address instead of value.

std::cout << &(window) << std::endl; // prints out memory location? YES

Try with below statement:-

std::cout << window << std::endl; // prints out value here
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17