Search code examples
c++vectoriteratorsliding-window

Cant acess the values from my sliding window?


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?
}

Solution

  • 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.