gpt4 book ai didi

C++:如何编写接受迭代器并插入元素的函数?

转载 作者:行者123 更新时间:2023-11-30 02:11:18 25 4
gpt4 key购买 nike

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

我有这个功能将图像分割成帧并将它们存储到图像容器中。我如何修改它以采用迭代器而不是容器,并在该点插入元素?

最佳答案

使用back_inserter:

template<typename OutputIterator>
void BlitSurface::ExtractFrames(OutputIterator it, int frame_width, int frame_height,
int frames_per_row, int frames_per_column,
bool padding) const
{
/* ... other lines unchanged ...*/
for(int i=0; i<surface_count; ++i) {
// "BlitSurface()" sets other members to zero. Alternatively you
// can use boost::value_initialized for that.
BlitSurface bs = BlitSurface();
bs._surface = temp_surf[i];
*it++ = bs;
}
delete [] temp_surf;
}

然后像这样调用它

ExtractFrames(std::back_inserter(container), ...);

关于C++:如何编写接受迭代器并插入元素的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3690859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com