Error: “no match for operator+” , for list iterator

前端 未结 4 1908
情书的邮戳
情书的邮戳 2020-12-11 18:27

For the code below, i am getting the error in title for line

while((*(It2 + code)).exists){


void locatetohashtable(st         


        
相关标签:
4条回答
  • 2020-12-11 18:46

    This is a matter of "Concept".

    A list can only be traversed efficiently forward and backward, thus its iterators model the Bidirectional Iterator concept.

    You can either use std::advance to move the iterator by several positions at once, however it will not be efficient.

    Or you can change to use a vector or deque instead of a list. Since they are Random Access containers, their iterators support addition and subtraction efficiently.

    0 讨论(0)
  • 2020-12-11 18:48

    An iterator for an std::list is bidirectional, so it does not support +(int). The only supported move operations are ++ and --.

    0 讨论(0)
  • 2020-12-11 18:58

    std::list iterators are only bidirectional, not random access, so you can't use operator + to advance them. Use std::next (C++11) or std::advance instead.

    0 讨论(0)
  • 2020-12-11 19:12

    That is because std::list's iterators are bidirectional iterators, so they don't support the addition operation you are trying to perform. In practice, this is because it cannot be implemented as an efficient operation, since lists do not provide random access, so you'd have to step in single increments from initial iterator to the target one. The design decision is to not provide an operation what will be inefficient.

    You can use std::advance or std::next to avoid writing your own increment loops, but under the hood it will increment step by step.

    0 讨论(0)
提交回复
热议问题