我正在尝试实现一个存储相关索引的列表。
但是我在标题中提到的for (index_itr = (list_size - numberOfEvents - 1)
处收到错误。我在做什么错,以及如何纠正它?
void logPrintEntry(UINT32 index, UINT32 portID, UINT16 numberOfEvents)
{
LOG_ENTRY log;
list <UINT32> list_telnet_indices;
log = (pLOG_ENTRY) &(Data.Log.log[index]);
list <UINT32> ::iterator index_itr;
UINT16 list_size = list_telnet_indices.size();
if( list_size <= numberOfEvents )
{
// print all logs
for ( index_itr = list_telnet_indices.begin();
index_itr != list_telnet_indices.end(); ++index_itr )
{
printDataOnly(log, *index_itr, portID);
}
}
else
{
// print only the last relevant entries
for (index_itr = (list_size - numberOfEvents - 1); //error: assignment to '_List_iterator<unsigned int,unsigned int &,unsigned int *>' from 'int'
index_itr != list_telnet_indices.end(); ++index_itr)
{
printDataOnly(log, *index_itr, portID);
}
}
}
答案 0 :(得分:1)
index_itr
是一个迭代器,list_size - numberOfEvents - 1
是一个整数。那些不兼容。
使用std::advance
设置std::list
#include <iterator> // std::advance
index_itr = list_telnet_indices.begin();
std::advance(index_itr, list_size - numberOfEvents - 1);
for (; index_itr != list_telnet_indices.end(); ++index_itr)
{
...
}