Can range based for loop work over a range

前端 未结 4 1035
醉梦人生
醉梦人生 2021-01-04 21:04

If I have range (pair of 2 iterators) is there a way to write \"for each\" loop for that uses range, not a raw array or container.

Something like this:



        
4条回答
  •  逝去的感伤
    2021-01-04 21:49

    I don't think it will work like that out of the box as equal_range returns a pair of iterators while, the for cycle over range according to documentation are:

    The begin_expr and end_expr are defined to be either:
    If (__range) is an array, then (__range) and (__range + __bound), where __bound is the array bound
    If (__range) is a class and has either a begin or end member (or both), then begin_expr is __range.begin() and end_expr is __range.end();
    Otherwise, begin(__range) and end(__range), which are found based on argument-dependent lookup rules with std as an associated namespace.
    

    I would say you may define begin and end functions that take the pair of iterators and return first and second one resepectively.

提交回复
热议问题