c++ - What type should I use for a range (of array indices)? -


i need write functions returning ranges within array (you may, if like, assume they're specified starting position , length). can't return tuple, need return other type (and maybe it's idea anyway). now, i'd return that's both (somewhat) standard , not incur performance penalty due vtables, convoluted ctors etc.

the options come mind are:

  1. std::pair<unsigned> (or std::pair<size_t>).
  2. a struct wrapping 2 unsigneds (or size_t's).
  3. rolling own range class, sugar-coating utility methods.

i tried looking proper range class, couldn't find one. it's kind of strange nothing in standard library/stl uses class ranges.

are there better alternatives? , if there aren't, of above recommend?

the main reason why hard answer it's not clear kinds of situations range used. don't know yet exactly.

in situations recommend creating own class, because keeps flexible. can add member functions , nested typedefs needed.

if in end, when use cases clear , member functions , auxiliary functions have been defined, turns out replaced pair, can still replace class typedef.

for inner type i'd recommend iterators on integers, unless want store huge amounts of these ranges (in case space optimization important , 2 integers (offset+length) save space).


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -