c++ - 无法遍历模板列表 C++

标签 c++ list templates iterator

我不熟悉在 C++ 中使用模板,我正在尝试遍历模板列表。

这是我的代码

template <typename T>
void pleaseWork(const list<T>& aList, list<T>& list1, list<T>& list2) 
{
typename list<T>::iterator i;
int n = 0;
for(i = aList.begin(); i != aList.end(); ++i) {
         //Do something crazy
         n++;
}

}

int main()
{
    list<int> lista;
    list<int> list1;
    list<int> list2;
    for (int i = 0; i < 10; i++) {
        lista.push_back(i*2);
    }

    pleaseWork(lista, list1, list2);

return 0; 
}

编译时出现以下错误: 错误:i = ((const std::list<int, std::allocator<int> >*)aList)->std::list<_Tp, _Alloc>::begin 中的“operator=”不匹配_Tp = int, _Alloc = std::allocator’

谢谢你的帮助

最佳答案

aList 是对const 的引用,因此begin() 将返回一个const_iterator

如果您的编译器支持,请使用 auto

关于c++ - 无法遍历模板列表 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7962544/

相关文章:

C++ : Get a child member from the Father class

c++ - 具有结构组件的类构造函数

C++通过PID在客户端强制退出时停止其他程序

c++ - 为什么 std::filesystem::file_size 位于留给实现的目录上?

python - 用Python创建列表

python : How to remove rows which has nan values for dict inside list

arrays - 为什么在 F# 中处理数组比列表更快

c++ - std::move 模板参数是如何推导出来的?

c++ - 创建一个包含可变类型的元组

c++ - 关于自定义分配器和 STL 的模板声明、别名和特化说明