我被要求写一个swap()方法,以便第一个元素变成第二个,第二个变成第一个。 我有:
Pair<T2,T1> swap() { return Pair<T2,T1>(second, first); }但是这会返回一个新的对象而不是交换,我认为它需要是一个无用的方法来改变它自己的数据成员。 这是可能的,因为T1和T2可能是不同的类类型? 换句话说,我不能简单地设置temp = first,first = second,second = temp,因为它会尝试将它们转换为不同的类型。 我不知道为什么你可能想要有一个模板对象改变其类型的顺序,因为它似乎会造成混乱,但似乎是我被要求做的事情。
编辑:谢谢大家回答! 和我想的一样,交换位置显然没有任何意义,对swap()函数的请求非常模糊。
template< class T1, class T2 > class Pair { T1 first; T2 second; };I'm being asked to write a swap() method so that the first element becomes the second and the second the first. I have:
Pair<T2,T1> swap() { return Pair<T2,T1>(second, first); }But this returns a new object rather than swapping, where I think it needs to be a void method that changes its own data members. Is this possible to do since T1 and T2 are potentially different class types? In other words I can't simply set temp=first, first=second, second=temp because it would try to convert them to different types. I'm not sure why you would potentially want to have a template object that changes order of its types as it seems that would cause confusion but that appears to be what I'm being asked to do.
Edit: Thank you all for answering! Pretty much as I thought, swapping in place obviously does not make any sense, the request for the swap() function was quite ambiguous.
最满意答案
由于T1和T2不必是同一类型,因此不能在原位置换。 Pair<T1,T2>是与Pair<T2,T1>不同的类型。 您必须返回与原始类型不同类型的对象,因此必须是新对象。
我会做的是这样的:
template< class T1, class T2 > Pair<T2,T1> swap(const Pair<T1,T2>& pair) { return Pair<T2,T1>(pair.second, pair.first); }(没有理由使其成为您的Pair模板的成员。)
但是,您可以在T1和T2属于同一类型时添加重载 :
template< class T > Pair<T,T>& swap(Pair<T,T>& pair) { using std::swap; swap(pair.first, pair.second); return pair; }但是,正如丹尼斯在评论中提到的那样,这可能确实令人困惑。
另一个想法是为您的Pair模板定义一个转换构造函数 ,以便可以交换隐式可转换类型:
template< class T1, class T2 > class Pair { T1 first; T2 second; template< class A1, class A2 > Pair(const A1& a1, const A2& a2) : first(a1), second (a2) {} };然后你可以这样交换:
Pair<int,double> p1(42,47.11); Pair<double,int> p2(p1.second,p1.first);但请注意,这也支持其他可能不需要的隐式转换:
Pair<char,float> p3(p1.second, p1.first); // narrowing!You cannot swap in-place, since T1 and T2 need not be of the same type. Pair<T1,T2> is a different type than Pair<T2,T1>. You have to return an object of a different type than the original one, so that has to be a new object.
What I'd do is this:
template< class T1, class T2 > Pair<T2,T1> swap(const Pair<T1,T2>& pair) { return Pair<T2,T1>(pair.second, pair.first); }(There's no reason to make this a member of your Pair template.)
You could, however, add an overload for when T1 and T2 are of the same type:
template< class T > Pair<T,T>& swap(Pair<T,T>& pair) { using std::swap; swap(pair.first, pair.second); return pair; }But this, as Dennis mentioned in his comment, might be indeed very confusing.
Another idea is to define a converting constructor for your Pair template, so that implicitly convertible types can be swapped:
template< class T1, class T2 > class Pair { T1 first; T2 second; template< class A1, class A2 > Pair(const A1& a1, const A2& a2) : first(a1), second (a2) {} };Then you can swap like this:
Pair<int,double> p1(42,47.11); Pair<double,int> p2(p1.second,p1.first);But note that this also supports other, probably unwanted implicit conversions:
Pair<char,float> p3(p1.second, p1.first); // narrowing!更多推荐
发布评论