C++ C++;:如何编写接受迭代器并插入元素的函数? 模板 void BlitSurface::提取帧(容器和输出、整数帧宽度、整数帧高度、, 每行整数帧,每列整数帧, 布尔填充)常数 { SDL_表面**temp_surf=SDL_Ex_ExtractFrames(_表面、框架宽度、框架高度、每行框架、每列框架、填充); int surface_count=每行帧数*每列帧数; 输出。调整大小(表面计数); 容器::迭代器iter=output.begin(); 对于(int i=0;i_surface=temp_surf[i]; 删除[]临时冲浪; }

C++ C++;:如何编写接受迭代器并插入元素的函数? 模板 void BlitSurface::提取帧(容器和输出、整数帧宽度、整数帧高度、, 每行整数帧,每列整数帧, 布尔填充)常数 { SDL_表面**temp_surf=SDL_Ex_ExtractFrames(_表面、框架宽度、框架高度、每行框架、每列框架、填充); int surface_count=每行帧数*每列帧数; 输出。调整大小(表面计数); 容器::迭代器iter=output.begin(); 对于(int i=0;i_surface=temp_surf[i]; 删除[]临时冲浪; },c++,iterator,containers,C++,Iterator,Containers,我有一个函数,可以将图像分割成帧,并将其存储到图像容器中。我如何修改它以使用迭代器而不是容器,并在该点插入元素?使用back\u inserter: template<class Container> void BlitSurface::ExtractFrames(Container & output, int frame_width, int frame_height, int frames_

我有一个函数,可以将图像分割成帧,并将其存储到图像容器中。我如何修改它以使用迭代器而不是容器,并在该点插入元素?

使用
back\u inserter

template<class Container>
void BlitSurface::ExtractFrames(Container & output, int frame_width, int frame_height,
                                         int frames_per_row, int frames_per_column,
                                         bool padding) const
{
    SDL_Surface ** temp_surf = SDL_Ex_ExtractFrames(_surface, frame_width, frame_height, frames_per_row, frames_per_column, padding);

    int surface_count = frames_per_row * frames_per_column;

    output.resize(surface_count);
    Container::iterator iter = output.begin();

    for(int i=0; i<surface_count; ++i, ++iter)
        iter->_surface = temp_surf[i];

    delete [] temp_surf;
}

使用
背面插入器

template<class Container>
void BlitSurface::ExtractFrames(Container & output, int frame_width, int frame_height,
                                         int frames_per_row, int frames_per_column,
                                         bool padding) const
{
    SDL_Surface ** temp_surf = SDL_Ex_ExtractFrames(_surface, frame_width, frame_height, frames_per_row, frames_per_column, padding);

    int surface_count = frames_per_row * frames_per_column;

    output.resize(surface_count);
    Container::iterator iter = output.begin();

    for(int i=0; i<surface_count; ++i, ++iter)
        iter->_surface = temp_surf[i];

    delete [] temp_surf;
}

每次我看到这样的答案,我就知道我对STL知之甚少。:(我这样做了:ExtractFrames(std::back_inserter(surface_vec),c_width,c_height,16,6,true);--------然后我得到以下错误:“错误C2182:'v':非法使用类型'void'”,“错误C2440:'initializing':无法从'value_type'转换为'int',和其他一些。surface_vec是一种标准:vector@user嗯,我的错。输出迭代器不公开值类型:(@Johannes:我真想知道为什么,你碰巧知道原因吗?@Matthieu,我不知道。但是也许这样他们可以支持“多态”迭代器。流输出迭代器可以像
output\u迭代器(cout)一样创建
然后人们可以做
*it++=“value:”;*it++=42;
。每次我看到这样的答案,我就知道我对STL知之甚少。:(我这样做了:提取帧(std::back_inserter(surface_vec),c_width,c_height,16,6,true)————然后我得到以下错误:“错误C2182:'v':非法使用类型'void',“错误C2440:‘正在初始化’:无法从‘value_type’转换为‘int’和其他一些。surface_vec是std::vector@user嗯,我的错。输出迭代器不公开值类型:(@Johannes:我真想知道为什么,你知道原因吗?@Matthieu,我不知道。但也许这样他们就可以支持“多态性”“迭代器。可以像
输出迭代器(cout)
那样创建流输出迭代器,然后可以执行
*it++=”值:;*it++=42;
ExtractFrames(std::back_inserter(container), ...);