c++ - 无法从我的滑动窗口访问值?

标签 c++ vector iterator sliding-window

我目前正在为 vector<double> 实现滑动窗口功能.问题是我似乎无法cout值(value)?当我输出它时,我似乎得到了内存位置,而不是实际值..

我需要处理窗口包含的数据,因此访问这些值会很整洁。

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

最佳答案

每次打印窗口内容时,都需要遍历窗口本身。这可以通过像这样更改 for 循环的内容来完成:

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

请注意,窗口的宽度为 (NUM_SECONDS*SAMPLE_RATE)。这可以存储在像 window_width 或类似的变量中以帮助提高可读性。

关于c++ - 无法从我的滑动窗口访问值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47529161/

相关文章:

c++ - 了解 C++ 中映射和 vector 的 typeid.name() 输出

c++ - C++ 中的 CDBException(错误)处理(VS2010、MFC、Excel/ODBC)

c++ - char* 和 new 的一些错误

c++ - cocos2d-x EditBox的简单例子,报错undefined reference to

c++ - 如何打破这种循环类型定义?

c++ - 实现基于迭代器的 shell 排序

c++ - 访问 std::list 中派生类的属性

c++ - 在数组中插入数据时出错 - 数组下标的无效类型 'int[int]'

mysql - 如何将向量插入到 mysql 表的列中?

c++ - 调试断言失败 - 数组迭代器不兼容