tags:

views:

45

answers:

3

Hi.

 int num = 0;
 list<char>::iterator it;
 for(it = binary.const_iterator; it !=binary.end(); ++it) {
  if(*it == '1') {
   abc.push_back(copyoflist.at(num));
  }
  num++;
 }

Here binary is defined as list binary; copyoflist is a char type vector.

I am getting this error: invalid use of 'std::list >::const_iterator' on the line

for(it = binary.const_iterator; it !=binary.end(); ++it)

Am not able to figure out what is going wrong. Can someone help me out ?

+3  A: 

You need:

for (it=binary.begin(); it != binary.end(); ++it)
the_mandrill
+1 for beating me by 6 seconds.
Fred Larson
He beat me by a minute, but only because I stopped to fix the format of the question....
James Curran
@James Curran: Because of FGITW, maybe you need to answer first and then fix the question. 8v) I +1ed your answer too.
Fred Larson
Despite the delay, I think James's answer is a bit better, not only pointing out what to do, but also why the current code is wrong.
Jerry Coffin
+3  A: 

const_iterator is a type, not a property. You would use it like this:

 list<char>::const_iterator it;
 for(it = binary.begin(); it != binary.end(); ++it) 
James Curran
+2  A: 

vector<T>::const_iterator is a type just like vector<T>::iterator. You use either one or the other when you declare the iterator depending on what you need to do in the loop. You always use begin(), end() or the reverse equivalents for the initialization and the conditional.

 int num = 0;
 list<char>::const_iterator it;
 for(it = binary.begin(); it !=binary.end(); ++it) {
  if(*it == '1') {
   abc.push_back(copyoflist.at(num));
  }
  num++;
 }
Cogwheel - Matthew Orlando