关于运营商的问题+模板中随机访问迭代器的重载
我想重载列表类中迭代器的“+”运算符,类似
list<double>::iterator operator+(const list<double>::iterator& it, int n)
这样效果很好。但是,当我尝试将其实现为模板时,就像
template<class T>
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)
我收到错误消息一样,
no match for 'operator+' in 'it + small_index'
无法找出原因......
代码附在下面,
#include<iostream>
#include<list>
using namespace std;
template<class T>
ostream& operator<< (ostream& os, const list<T>& l)
{
typename list<T>::const_iterator i = l.begin();
for (;i!=--l.end();i++)
os<<*i<<";";
os<<*i<<endl;
return os;
}
template<class T> //this is where it goes WRONG.
//If don't use template, delete "typename", T->double, runs well
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)
{
typename list<double>::iterator temp=it;
for(int i=0; i<n; i++)
temp++;
return temp;
}
template <class T>
void small_sort(list<T>& l)
{
int n = l.size();
typename list<T>::iterator it = l.begin();
for(int i=0; i<n-1; i++)
{
//Find index of next smallest value
int small_index = i;
for(int j=i+1; j<n; j++)
{
if(*(it+j)<*(it+small_index)) small_index=j;
}
//Swap next smallest into place
double temp = *(it+i);
*(it+i) = *(it+small_index);
*(it+small_index)=temp;
}
}
int main()
{
list<double> l;
l.push_back(6);
l.push_back(1);
l.push_back(3);
l.push_back(2);
l.push_back(4);
l.push_back(5);
l.push_back(0);
cout<<"=============sort the list=============="<<endl;
small_sort(l);
cout<<l;
return 0;
}
I would like to overload the "+" operator of iterator in list class, something like
list<double>::iterator operator+(const list<double>::iterator& it, int n)
This works well. However, when I try to implement it as template, like
template<class T>
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)
I got error msg like,
no match for 'operator+' in 'it + small_index'
can't figure out the reason...
The code is attached below,
#include<iostream>
#include<list>
using namespace std;
template<class T>
ostream& operator<< (ostream& os, const list<T>& l)
{
typename list<T>::const_iterator i = l.begin();
for (;i!=--l.end();i++)
os<<*i<<";";
os<<*i<<endl;
return os;
}
template<class T> //this is where it goes WRONG.
//If don't use template, delete "typename", T->double, runs well
typename list<T>::iterator operator+(const typename list<T>::iterator& it, int n)
{
typename list<double>::iterator temp=it;
for(int i=0; i<n; i++)
temp++;
return temp;
}
template <class T>
void small_sort(list<T>& l)
{
int n = l.size();
typename list<T>::iterator it = l.begin();
for(int i=0; i<n-1; i++)
{
//Find index of next smallest value
int small_index = i;
for(int j=i+1; j<n; j++)
{
if(*(it+j)<*(it+small_index)) small_index=j;
}
//Swap next smallest into place
double temp = *(it+i);
*(it+i) = *(it+small_index);
*(it+small_index)=temp;
}
}
int main()
{
list<double> l;
l.push_back(6);
l.push_back(1);
l.push_back(3);
l.push_back(2);
l.push_back(4);
l.push_back(5);
l.push_back(0);
cout<<"=============sort the list=============="<<endl;
small_sort(l);
cout<<l;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是这个论证在这种情况下是不可推论的。
当您在那里使用迭代器时,编译器必须生成具有任何给定类型的
list
所有可能的实例,并尝试将内部类型iterator
与您指定的参数相匹配。正在传递,请注意,一旦将模板添加到混合中,所有类型的集合实际上是无限的,因为您可以使用同一模板的实例化无限来实例化模板。我建议您完全避免该问题并使用
std::advance
这是推进迭代器的惯用方法。The problem is that the argument is not deducible in that context.
When you use an iterator there, the compiler would have to generate all possible instantiations of
list
with any given type, and try to match an internal typeiterator
with the argument that you are passing, note that the set of all types is actually infinite once you add templates to the mix, as you can instantiate a template with an instantiation of the same template ad infinitum.I recommend that you avoid the problem altogether and use
std::advance
which is the idiomatic way of advancing an iterator.您应该查看是否可以使用标准算法
std::advance(it, n)
。它在
中定义。它为任何合适的迭代器做“正确的事情”。You should take a look whether the standard algorithm
std::advance(it, n)
could be used. It's defined in<iterator>
. It does "the right thing" for any suitable iterator.