Const_iterator for C ++ Iterator Error

I am trying to execute an iterator through a map object using the following code snippet:

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

And I keep getting error messages:

conversion from const_iterator to a non-scalar request of an iterator of type

And I can’t determine why the iterator will be const vs. not- const , or how to handle it.

+4
source share
4 answers

Instead, use map<int, vector>::const_iterator , which returns map::begin .

+7
source

It looks like table is an object or a const reference, in which case begin returns const_iterator . Change for 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.being(); table_iter != table.end(); table_iter++) { ... } 
+2
source

Well, your question has been partially answered.

"And I can't understand why the iterator will be const vs. not-const or how to handle it." Like others, if your table is defined as a constant, you need to define a constant iterator. The disadvantage is that if the function is defined as const, then the iterator must be const.

I realized that the const function is your problem, not the const table.

+1
source

I assume the table is const &.

0
source

Source: https://habr.com/ru/post/1385041/


All Articles