Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const_iterator to iterator C++ Error

I'm trying to iterator through a map object using the following chunk of code:

for(map<int, vector>::iterator table_iter = table.being(); table_iter != table.end(); table_iter++)
{
    ...
}

And I keep getting errors telling me:

conversion from const_iterator to non-scalar type iterator requested

And I can't seem to determine why the iterator would be const vs. not-const, or how to deal with this.

like image 774
Patrick Perini Avatar asked Dec 21 '22 05:12

Patrick Perini


2 Answers

Use map<int, vector>::const_iterator instead which is returned by map::begin.

like image 161
danca Avatar answered Dec 24 '22 02:12

danca


Sounds like table is a const object or reference, in which case begin returns a const_iterator. Change your for-loop to this:

// typedefs make for an easier live, note const_iterator
typedef map<int, vector>::const_iterator iter_type;
for(iter_type table_iter = table.begin(); table_iter != table.end(); table_iter++)
{
    ...
}
like image 42
Xeo Avatar answered Dec 24 '22 01:12

Xeo