Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "no match for operator+" , for list iterator

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

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


void locatetohashtable(std::list<Element> elist,
                       int *m,std::list<Element>& table,
                       std::list<std::string>& keylist )
{        
    std::list<Element>::iterator It2=table.begin();
    int i=0;
    int k=0;
    std::list<Element>::iterator It;
    for(It = elist.begin(); It != elist.end(); ++It)
    {
        int code=hash_func(stringIntValue((*It).name),*m,i);
        while((*(It2 + code)).exists){
            i++;
        }
        table.insert(*(It2+i), (*It));
        keylist.insert(keylist.begin(),(*It).name);
        k++;
    }
}

I am not getting the same error for ++It

What is the problem about?

like image 759
merveotesi Avatar asked May 12 '12 13:05

merveotesi


2 Answers

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

like image 100
Travis Gockel Avatar answered Oct 15 '22 08:10

Travis Gockel


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.

like image 30
juanchopanza Avatar answered Oct 15 '22 08:10

juanchopanza