'std::vector::iterator it;'无法编译
我有这个函数:
template<typename T>
void Inventory::insertItem(std::vector<T>& v, const T& x)
{
std::vector<T>::iterator it; // doesn't compile
for(it=v.begin(); it<v.end(); ++it)
{
if(x <= *it) // if the insertee is alphabetically less than this index
{
v.insert(it, x);
}
}
}
并且 g++ 给出了这些错误:
src/Item.hpp: In member function ‘void
yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’:
src/Item.hpp:186: error: expected ‘;’ before ‘it’
src/Item.hpp:187: error: ‘it’ was not declared in this scope
它一定是简单的东西,但是在盯着它十分钟后我找不到任何错误。还有其他人看到吗?
I've got this function:
template<typename T>
void Inventory::insertItem(std::vector<T>& v, const T& x)
{
std::vector<T>::iterator it; // doesn't compile
for(it=v.begin(); it<v.end(); ++it)
{
if(x <= *it) // if the insertee is alphabetically less than this index
{
v.insert(it, x);
}
}
}
and g++ gives these errors:
src/Item.hpp: In member function ‘void
yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’:
src/Item.hpp:186: error: expected ‘;’ before ‘it’
src/Item.hpp:187: error: ‘it’ was not declared in this scope
it must be something simple, but after ten minutes of staring at it I can't find anything wrong. Anyone else see it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这是一个页面,描述了如何使用 typename 以及为什么使用它这里有必要。
Try this instead:
Here's a page that describes how to use typename and why it's necessary here.
你所做的事情效率很低。使用二分搜索代替:
What you are doing is inefficient. Use a binary search instead: