gpt4 book ai didi

c++ - 使用迭代器在 vector C++ 中循环

转载 作者:行者123 更新时间:2023-11-30 01:20:02 24 4
gpt4 key购买 nike

我有这个代码:

std::vector<A>::iterator it;
for(auto it = m_vA.begin(); it != m_vA.end(); it++)

我会出错:

ISO C++ forbids declaration of 'it' with no type
cannot convert '__gnu_cxx::__normal_iterator<A* const*, std::vector<tp::Vehicule*, std::allocator<A*> > >' to 'int' in initialization

如果我删除自动

erreur: no match for 'operator=' in 'it = ((const B*)this)->B::m_vA.std::vector<_Tp, _Alloc>::begin [with _Tp = A*, _Alloc = std::allocator<A*>]()'

B 是我的循环类

谢谢

最佳答案

对于 auto 你似乎没有启用 c++11,如果你启用它,你应该删除这一行:

std::vector<A>::iterator it;

如果您不能使用 c++11 并且在删除 auto 后出现错误,看起来您将此代码放入 const 方法中,因此请将 iterator 替换为 const_iterator:

std::vector<A>::const_iterator it;
for(it = m_vA.begin(); it != m_vA.end(); it++)

如果你在循环后不需要这个迭代器,你也可以把它写成一行:

for(std::vector<A>::const_iterator it = m_vA.begin(); it != m_vA.end(); it++)

关于c++ - 使用迭代器在 vector C++ 中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19961752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com