Skip to content

Instantly share code, notes, and snippets.

@jonnyyu
Created October 11, 2022 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnyyu/bbc987d2c0a6e7520f4d99b0996e14bf to your computer and use it in GitHub Desktop.
Save jonnyyu/bbc987d2c0a6e7520f4d99b0996e14bf to your computer and use it in GitHub Desktop.
C++ iterate by chunk
#pragma once
namespace utils
{
template<class T>
void enumByChunk(const typename T::const_iterator& cbegin, const typename T::const_iterator& cend, const size_t chunkSize,
const function<void(const typename T::const_iterator& sub_begin, const typename T::const_iterator& sub_end)>& func)
{
auto it = cbegin;
while (it < cend)
{
auto next = (size_t)std::distance(it, cend) >= chunkSize ? it + chunkSize : cend;
func(it, next);
it = next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment