【发布时间】:2011-05-06 04:59:42
【问题描述】:
我正在我的 C++ 大学课程中学习如何使用向量。我遇到了一个问题,使我无法使用带有向量的迭代器。这是我的源代码:
template <class T>
void HuffMan<T>::search_freq(T temp) {
//checks for frequency
if(objects.empty()){
objects.push_back(temp);
return;
}
vector<T>::iterator it = objects.begin();
while(it != objects.end()) {
if(*it == temp)
cout<<"added aready\n";
else
objects.push_back(temp);
//this is where the error occurs
//I cannot call 'it++' for some reason
it++;
}
}
此代码始终返回一个运行时错误,显示“向量迭代器不可递增”。我试图将 while 循环更改为 for 循环,但我认为这与错误无关。
信息:我的矢量对象声明如下:
vector<T> objects;
谁能帮我指出这个错误?
谢谢, 是的
【问题讨论】:
-
while循环是否包含在
HuffMan<T>类的方法中?