c++ - 错误 : assignment to '_List_iterator<unsigned int,unsigned int &,unsigned int *>' from 'int'

标签 c++ list logging stl iterator

我正在尝试实现一个将存储相关索引的列表。 但是,我在标题中提到的 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);
        }
    }
}

最佳答案

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)
{
    ...
}

关于c++ - 错误 : assignment to '_List_iterator<unsigned int,unsigned int &,unsigned int *>' from 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34495690/

相关文章:

java - 如何在 Java 中使用 Collections 对从类中填充的列表进行排序

delphi - FastMM4,如何读取日志文件?

c++ - 并行计算右侧连续的零位(尾随) : an explanation?

c++ - 尝试运行指针排序算法时出现段错误(核心已转储)

C++问题:boost::bind receive other boost::bind

ruby - 如何将 Ruby Sequel 日志记录设置为 DEBUG 级别?

multithreading - 哪个是更好的日志记录方法 - 文件或数据库?

c++ - 首选使用 istrstream 或 strtol/strtod 从字符串中读取数字

c# - 如何删除递归嵌套列表中的重复项

python - 如何将列表中的每一项添加到 Python 中的前一项?