我是C++的新手,我请求帮助来解决问题.
我正在编写一个简单的STL样式函数,它应该返回序列的中间元素(向量,列表等)
这是我的函数,我尝试使用迭代器的概念
template <class It, class T> It middle(It first, It last)
{
while(first!=last){
++first;
--last;
}
return first;
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要,试图调用中间为一个向量的向量(我省略了包括)
int main() {
vector<int>vi;
int x;
cout<<"Enter vector elemets...";
while (cin>>x)
vi.push_back(x);
cout<<endl;
cin.clear();
cout<<"the vector is:"<<endl;
for(int i=0;i<vi.size();++i)
cout<<vi[i]<<" ";
cout<<endl;
vector<int>::iterator first=vi.begin();
vector<int>::iterator last=vi.end();
vector<int>::iterator ii=middle(first,last);
cout<<"The middle element of the vector is: "<<*ii<<endl;
}
Run Code Online (Sandbox Code Playgroud)
使用g ++编译时出现以下错误:
myex21-7.cpp:79: error: no matching function for call to ‘middle(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)’
Run Code Online (Sandbox Code Playgroud)
有人可以给我一些解决方法吗?感谢您对高级潜行的任何帮助
c++ ×1