我有这个函数:
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
这一定是某些简单的问题,但是我盯着它看了十分钟也没发现什么问题。有其他人看到了吗?
it < v.end()
-- 它可以与vector
一起使用,但不能与list
和其他容器一起使用。您应该始终使用it != v.end()
来确保没有问题。 - Peter Alexander