GVKun编程网logo

Swift 中的 数组排序 和 重载运算符(swift数组操作)

1

对于想了解Swift中的数组排序和重载运算符的读者,本文将提供新的信息,我们将详细介绍swift数组操作,并且为您提供关于C++|重载运算符的有价值信息。本文目录一览:Swift中的数组排序和重载运算

对于想了解Swift 中的 数组排序 和 重载运算符的读者,本文将提供新的信息,我们将详细介绍swift数组操作,并且为您提供关于C ++ |重载运算符<< | std :: map、c – 重载运算符、c – 重载运算符 – ()作为自由函数的意义,而不是成员函数?、c – 重载运算符<< for arrays的有价值信息。

本文目录一览:

Swift 中的 数组排序 和 重载运算符(swift数组操作)

Swift 中的 数组排序 和 重载运算符(swift数组操作)

数组排序

首先拿到一个数组,然后这个是按照数字中Item元素的 name 属性来排序的代码片段:

searchResults.sortInPlace({ result1,result2 in
    return result1.name.localizedStandardCompare(result2.name) == .OrderedAscending
  })

这个使用的方法是sortInPlace,还有另外一个方法是sort

如果使用了sort,那么原来的数组是不会有任何变化的,使用sortInPlace,会在原来的数组上做修改。

还可以简化为:

searchResults.sortInPlace{
    $0.name.localizedStandardCompare($1.name) == .OrderedAscending
  }

第二种写法没有传统的括号 (),使用 $0 来标记两个 $1 数组中的对象可读性很好,这是 Swift 的闭包(Closures)中很好的一个特性。

运算符重载

另外,可以使用运算符重载(operator overloading)把这个做的更好:
在被排序对象的数组对象之外,记住是在Class之外,声明重载算术运算符的方法:

func < (lhs: SearchResult,rhs: SearchResult) -> Bool {
  return lhs.name.localizedStandardCompare(rhs.name) == .OrderedAscending
}

有了这个方法,刚刚的排序就可以写的更短:

searchResults.sortInPlace{$0 < $1}

再短:

searchResults.sortInPlace(<)

End.

C ++ |重载运算符<< | std :: map

C ++ |重载运算符<< | std :: map

如何解决C ++ |重载运算符<< | std :: map

我正在尝试在结构中重载地图的运算符

不存在从“ std :: _ Rb_tree_const_iterator >”到“ std :: _ Rb_tree_i terator >”的合适的 用户定义转换>

ostream& operator<<(ostream& os,const map<int,int>& neighbors)
{
    string res;
    map<int,int>::iterator it = neighbors.begin();
    stringstream ss;

    while (it != neighbors.end())
    {
        ss << "[id: " << it->first << " cost: " << it->second << "] ";
        it++;
    }
    return os << ss;
}

如何正确获得对地图迭代器的引用?我只能使用C ++ 98。

这是我完整的代码

#pragma once

#include <map>
#include <string>
#include <sstream>

using namespace std;

struct LSA
{
    int id;
    int seqNum;
    map <int,int> neighbors;

    friend ostream& operator<<(ostream& os,const LSA& lsa);
    friend ostream& operator<<(ostream& os,int>& neighbors);
};

ostream& operator<<(ostream& os,const LSA& lsa)
{
    return os << "[id: " << lsa.id << " seqNum: " << lsa.seqNum << " (" << lsa.neighbors.size() << " neighbors)";
}

ostream& operator<<(ostream& os,int>::iterator it = neighbors.begin();
    stringstream ss;

    while (it != neighbors.end())
    {
        ss << "[id: " << it->first << " cost: " << it->second << "] ";
        it++;
    }
    return os << ss;
}

解决方法

您有一个const映射,因此begin返回一个const_iterator,而不是iterator。没有定义的operator<<接受stringstream作为第二个参数,因此使用其成员函数str,如下所示

ostream& operator<<(ostream& os,const map<int,int>& neighbors)
{
    string res;
    map<int,int>::const_iterator it = neighbors.cbegin();
    stringstream ss;

    while (it != neighbors.end())
    {
        ss << "[id: " << it->first << " cost: " << it->second << "] ";
        it++;
    }
    return os << ss.str();
}

c – 重载运算符

c – 重载运算符

舱位声明:

class unaryOperators 
{
    public:
        int i;

        unaryOperators (int tempI = 0)
        {
            i = tempI;
        }

        unaryOperators operator++ (int);
        unaryOperators operator++ ();
};

此全局定义是否与重载运算符的后缀或前缀版本相对应?为什么?

unaryOperators operator++ (unaryOperators &one)
{   
    return one; 
}

解决方法

unaryOperators& operator++ (unaryOperators &one)
              ^^

是非成员前缀一元增量运算符.

非成员后缀一元增量运算符将另外的int作为策略强制参数.

unaryOperators operator++ (unaryOperators &one,int)

参考:

C 03标准13.5.7增量和减量[over.inc]

The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters,or a non-member function with one parameter of class or enumeration type,it defines the prefix increment operator ++ for objects of that type. If the function is a member function with one parameter (which shall be of type int) or a non-member function with two parameters (the second of which shall be of type int),it defines the postfix increment operator ++ for objects of that type. When the postfix increment is called as a result of using the ++ operator,the int argument will have value zero.125)

[Example:
class X {
   public:
      X& operator++(); // prefix ++a
      X operator++(int); // postfix a++
};
class Y { };
Y& operator++(Y&); // prefix ++b
Y operator++(Y&,int); // postfix b++

void f(X a,Y b) {
++a; // a.operator++();
a++; // a.operator++(0);
++b; // operator++(b);
b++; // operator++(b,0);
a.operator++(); // explicit call: like ++a;
a.operator++(0); // explicit call: like a++;
operator++(b); //explicit call: like ++b;
operator++(b,0); // explicit call: like b++;
}
—end example]

c – 重载运算符 – ()作为自由函数的意义,而不是成员函数?

c – 重载运算符 – ()作为自由函数的意义,而不是成员函数?

我正在阅读 the C++ FAQ.在那里,我发现操作符重载使用的指南中有一点:

If you provide constructive operators,they should allow promotion of the left-hand operand (at least in the case where the class has a single-parameter ctor that is not marked with the explicit keyword). For example,if your class Fraction supports promotion from int to Fraction (via the non-explicit ctor Fraction::Fraction(int)),and if you allow x – y for two Fraction objects,you should also allow 42 – y. In practice that simply means that your operator-() should not be a member function of Fraction. Typically you will make it a friend,if for no other reason than to force it into the public: part of the class,but even if it is not a friend,it should not be a member.

为什么作者写的是operator-()不应该是成员函数?

如果我将operator-()作为成员函数,还有什么后果呢?

解决方法

这里是作为成员函数的运算符的分数:
class Fraction{    Fraction(int){...}    Fraction operator -( Fraction const& right ) const { ... }};

有了它,这是有效的代码:

Fraction x;Fraction y = x - 42;

其等效于x.operator-(Fraction(42));但这不是:

Fraction z = 42 - x;

因为42没有成员函数运算符(当然,它甚至不是一个类).

但是,如果您将运算符声明为自由函数,则转换操作将适用于其两个参数.所以这

Fraction z = 42 - x;

变成这样

Fraction z = Fraction(42) - x;

这相当于运算符 – (分数(42),x)).

c – 重载运算符<< for arrays

c – 重载运算符<< for arrays

我想重载operator<<对于任意数组,使得代码cout<< my_arr会起作用.首先,我试图重载operator<<的第二个参数.在const T(& arr)[N]上,其中T和N是模板参数.但是测试代码会发现副作用:const char []也匹配类型规范,新的重载与流类中定义的重载冲突.示例代码:
#include <cstddef>
#include <iostream>

template<typename T,std::size_t N>
std::ostream& operator<<(std::ostream& os,const T (&arr)[N])
{
    /* do stuff */
    return os;
}

int main()
{
    std::cout << "noooo\n"; /* Fails: ambiguous overload */
}

这样的阵列打印运算符是否仍然可以实现?

解决方法

当然:
template<typename T,std::size_t N>
typename std::enable_if<!std::is_same<T,char>::value,std::ostream&>::type
operator<<(std::ostream& os,const T (&arr)[N])
{
    // ...
}

这将使用SFINAE在T为char时禁用您的重载.

对于C 03,Boost有enable_ifis_same.或者只是自己滚动:

template<class T,class U> struct is_same { 
    enum { value = false }; 
};
template<class T> struct is_same<T,T> { 
    enum { value = true }; 
};

template<bool,class T> struct enable_if {};
template<class T> struct enable_if<true,T> { 
    typedef T type; 
};

关于Swift 中的 数组排序 和 重载运算符swift数组操作的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于C ++ |重载运算符<< | std :: map、c – 重载运算符、c – 重载运算符 – ()作为自由函数的意义,而不是成员函数?、c – 重载运算符<< for arrays等相关知识的信息别忘了在本站进行查找喔。

本文标签: