我正在尝试编写一个函数来打印常见STL容器(向量,列表等)的表示.我给函数一个模板参数T,例如,它可能代表向量.我在获取类型为T的迭代器时遇到问题
vector<int> v(10, 0);
repr< vector<int> >(v);
Run Code Online (Sandbox Code Playgroud)
...
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
T::iterator i;
for (i = v.begin();
i != v.end()-1;
++i)
{
cout << *i << ", ";
}
cout << *(++i) << ' ';
}
cout << "]\n";
}
Run Code Online (Sandbox Code Playgroud)
...
brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in …Run Code Online (Sandbox Code Playgroud)