简体   繁体   English

在C ++中迭代向量

[英]Iterating a Vector in C++

So based on other examples I've found, I'm led to believe that this would be the proper code to iterate over m_vect: 因此,根据我发现的其他示例,我被认为是对m_vect进行迭代的正确代码:

for(vector<T>::iterator it = m_vect.begin(); it != m_vect.end(); ++it)

However, upon attempting to compile, I get the following error on that line: 但是,在尝试编译时,在该行上出现以下错误:

heap.h:167:6: error: need ‘typename’ before ‘std::vector<T>::iterator’ because ‘std::vector<T>’ is a dependent scope

Like I said, I copied and adapted the line from another piece of code, so I'm really not sure what I'm doing right and wrong. 就像我说的那样,我从另一段代码复制并改写了该行,所以我真的不确定自己在做对与错。 Any insight? 有见识吗?

To clarify, this is in a template function, and I have declared 'template '. 为了澄清,这是在模板函数中,我已经声明了'template'。 m_vect is of type vector. m_vect是向量类型。 Aaaand I don't know how to display less than and greater than properly... 我不知道如何显示小于和大于正确的显示...

Thankfully in C++11 you can just have the compiler figure it out. 幸运的是,在C ++ 11中,您可以让编译器解决该问题。 The compiler already knows what m_vect is, so you can tell it: 编译器已经知道m_vect是什么,因此您可以告诉它:

for (auto it= m_vect.begin(); ( it != m_vect.end()) ; ++ it ) { }

But wait, there's more. 但是,等等,还有更多。 In c++11 you can even just iterate over everything in m_vect 在c ++ 11中,您甚至可以遍历m_vect所有m_vect

for (auto it : m_vect ) { }

Can you tell that I think iterating in C++03 was insane and I never saw anybody do it in real life, and in C++11 it is a thousand times better? 您能告诉我,我认为在C ++ 03中进行迭代是疯狂的,并且在现实生活中我从未见过有人这样做,而在C ++ 11中,迭代效果要好上千倍?

This appears to be in a template function, and vector<T>::iterator is a type which is dependent on T , which is a template parameter. 这似乎是在模板函数中,而vector<T>::iterator是一种依赖于T的类型, T是模板参数。 Since templates can be specialized with unique definitions for any different type, the compiler has no way of being sure, until the template is actually instantiated, whether vector<T>::iterator is a type, or a static member. 由于可以使用任何不同类型的唯一定义来专门化模板,因此,在模板实际实例化之前,编译器无法确定vector<T>::iterator是类型还是静态成员。 If it is a static member, this does not make any sense, syntactically: 如果它是静态成员,那么从语法上讲这没有任何意义:

for(vector<T>::iterator it = m_vect.begin(); it != m_vect.end(); ++it)

What you need to do is tell the compliler that vector<T>::iterator is a type. 您需要做的就是告诉编译vector<T>::iterator是一种类型。 Use typename for that: 为此使用typename

for(typename vector<T>::iterator it = m_vect.begin(); it != m_vect.end(); ++it)

If you read your error, you can see that this is exactly what your compiler is telling you. 如果您读取了错误,则可以看到这正是编译器告诉您的。

T needs to be an actual type. T必须是实际类型。 It should be the type of whatever is stored in the vector. 它应该是向量中存储的任何类型。 Your for loop looks correct. 您的for循环看起来正确。 You declared m_vect somewhere. m_vect某处声明了m_vect The iterator needs the same time as whatever you declared m_vect (plus ::iterator of course). 迭代器需要与您声明的m_vect相同的时间(当然m_vect加上::iterator )。

This is something compilers are enforcing better now. 这是编译器现在正在执行的更好的东西。

When you are using something derived from a template, you have to flag if it's being used as a type by adding typename . 当使用从模板派生的东西时,必须通过添加typename来标记它是否被用作类型。 Then if the compiler knows that (for some insane reason) vector<T>::iterator is a method, it can immediately flag the error instead of heading into undefined behavior. 然后,如果编译器知道(出于某种疯狂的原因) vector<T>::iterator是一种方法,则它可以立即标记错误而不是进入未定义的行为。

So just add typename like the message says: 因此,只需添加typename如消息所示:

for(typename vector<T>::iterator it = m_vect.begin(); it != m_vect.end(); ++it)

If you have amodern enough compiler, you can replace vector<T>::iterator with auto . 如果您有足够现代的编译器,则可以将auto vector<T>::iterator替换为auto

Else, find the correct type to replace the T with for your vector. 否则,找到正确的类型以将T替换为您的向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM