Ответ 1
Попробуйте это вместо:
typename std::vector<T>::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
это должно быть что-то простое, но через десять минут, глядя на него, я не могу найти ничего плохого. Кто-нибудь еще видит это?
Попробуйте это вместо:
typename std::vector<T>::iterator it;
Здесь приведена страница, описывающая как использовать имя_файла и почему это необходимо здесь.
То, что вы делаете, неэффективно. Вместо этого используйте двоичный поиск:
#include <algorithm>
template <typename T>
void insertItem(std::vector<T>& v, const T& x)
{
v.insert(std::upper_bound(v.begin(), v.end(), x), x);
}