Gaurav Kalra
Gaurav Kalra

Reputation: 281

Invalid use of List Iterator in c++

int num = 0; list::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 ?

Upvotes: 0

Views: 1409

Answers (3)

Cogwheel
Cogwheel

Reputation: 23217

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++;
 }

Upvotes: 2

James Curran
James Curran

Reputation: 103485

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) 

Upvotes: 3

the_mandrill
the_mandrill

Reputation: 30832

You need:

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

Upvotes: 3

Related Questions