[英]C++: How do I write a function that accepts an iterator and inserts elements?
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;
}
I have this function splits an image up into frames and stores them into a container of images. 我有这个功能将图像分割成帧并将它们存储到图像容器中。 How would I modify it to take an iterator instead of a container, and insert the elements at that point?
我如何修改它来取一个迭代器而不是一个容器,并在那一点插入元素?
Use back_inserter
: 使用
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;
}
Then call it like 然后把它称为
ExtractFrames(std::back_inserter(container), ...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.