开发者

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.cons开发者_如何学Got_iterator; it !=binary.end(); ++it)

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


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) 


You need:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜