There are really only two possible solutions. If you do this a lot, over long distances, you would be better off converting your characters to encode a single element using wchar_t (or int32_t , or whatever is most appropriate. This is not a simple copy that will convert every single char to a target type, but true conversion function that recognizes multibyte characters and convert them to a single element.
For random use or shorter sequences, you can write your own functions to advance n bytes. For UTF-8, I use the following:
inline size_t size( Byte ch ) { return byteCountTable[ ch ] ; } template< typename InputIterator > InputIterator succ( InputIterator begin, size_t size, std::random_access_iterator_tag ) { return begin + size ; } template< typename InputIterator > InputIterator succ( InputIterator begin, size_t size, std::input_iterator_tag ) { while ( size != 0 ) { ++ begin ; -- size ; } return begin ; } template< typename InputIterator > InputIterator succ( InputIterator begin, InputIterator end ) { if ( begin != end ) { begin = succ( begin, end, size( *begin ), std::::iterator_traits< InputIterator >::iterator_category() ) ; } return begin ; } template< typename InputIterator > size_t characterCount( InputIterator begin, InputIterator end ) { size_t result = 0 ; while ( begin != end ) { ++ result ; begin = succ( begin, end ) ; } return result ; }
source share