c++20引入的三路比较操作符<=>

news2025/5/19 5:11:25

目录

一、简介

二、三向比较的返回类型

2.1 std::strong_ordering

2.2 std::weak_ordering

2.3 std::partial_ordering

三、对基础类型的支持

四、自动生成的比较运算符函数

4.1 std::rel_ops的作用

4.2 使用<=>

五、兼容他旧代码


一、简介

c++20引入了三路比较操作符(或者三向比较)<=>(three-way comparison operator),也叫太空船(spaceship)操作符。

<=>也是一个二元关系运算符,但它不像其他二元比较操作符那样返回类型是布尔类型,而是根据用户指明的三种类型之一:partial_ordering、weak_ordering和strong_ordering,定义于标准库头文件<compare>中,默认为strong_ordering类型。

  • 偏序partial_ordering表达了比较关系中的偏序关系,即给定类的任意两个对象不一定可比较。例如给定一棵对象树,假设父节点比子节点大,<=>得到的结果将为greater,但不是任意两个节点都可比较,此时它们的关系为unordered。对于偏序关系的排序,使用拓扑排序算法将获得正确的结果。
  • 弱序weak_ordering表达了比较关系中的全序关系,即给定类的任意两个对象都能比较,将既不大于也不小于的关系定义为等价(equivalent)关系。假设长方形类按照面积比较就是弱序关系,长宽分别为2和6的矩形与长宽分别为3和4的比较,面积都为12(既不大于也不小于)​,那么它们是等价的,但不相等是因为可以通过长宽区分出来它们不一样。标准库中的std::sort要求关系至少为弱序的才能正确工作。
  • 强序strong_ordering与弱序一样,当对等价关系进行了约束即为相等(equal)关系。考虑正方形类按照面积比较就是强序关系,因为面积一样的正方形无法像长方形那样通过外表能区分出来,即它们是相等的。一些查找算法要求关系为强序才能正确工作。

此外<=>的结果也与字符串比较函数strcmp类似,能够通过正负判断关系:当结果大于0表示大于关系,等于0表示等价、等于关系,小于0表示小于关系。

顾名思义,三向比较就是在形如lhs <=> rhs的表达式中,两个比较的操作数lhs和rhs通过<=>比较可能产生3种结果,该结果可以和0比较,小于0、等于0或者大于0分别对应lhs < rhs、lhs == rhs和lhs > rhs。举例来说:

#include<iostream>

int main(int argc,char* argv[]){
    bool b = 7<=>11 <0;
    std::cout<<std::boolalpha<<b<<std::endl;
    b = 7<=>11 ==0;
    std::cout<<std::boolalpha<<b<<std::endl;
    b = 7<=>11 >0;
    std::cout<<std::boolalpha<<b<<std::endl;
    b = 7<=>7 ==0;
    std::cout<<std::boolalpha<<b<<std::endl;
    return 0;
}

输出:

true
false
false
true

请注意,运算符<=>的返回值只能与0和自身类型来比较,如果同其他数值比较,编译器会报错:

#include<iostream>

int main(int argc,char* argv[]){
    bool b = 7<=>11 <100;//编译失败,<=>的结果不能与除0以外的数值比较
    std::cout<<std::boolalpha<<b<<std::endl;
    return 0;
}


二、三向比较的返回类型

<=>的返回结果并不是一个普通类型,根据标准三向比较会返回3种类型,分别为std::strong_ordering、std::weak_ordering以及std::partial_ordering,而这3种类型又会分为有3~4种最终结果。


2.1 std::strong_ordering

std::strong_ordering类型有3种比较结果,分别为std::strong_ ordering::less、std::strong_ordering::equal以及std::strong_ ordering::greater。表达式lhs <=> rhs分别表示lhs < rhs、lhs == rhs以及lhs > rhs。std::strong_ordering类型的结果强调的是strong的含义,表达的是一种可替换性,简单来说,若lhs == rhs,那么在任何情况下rhs和lhs都可以相互替换,也就是fx(lhs) == fx(rhs)。

对于基本类型中的int类型,三向比较返回的是std::strong_ordering,例如:

用MSVC编译运行以上代码,会在输出窗口显示class std::strong_ ordering,刻意使用MSVC是因为它的typeid(x).name()可以输出友好可读的类型名称。

对于有复杂结构的类型,std::strong_ordering要求其数据成员和基类的三向比较结果都为std::strong_ordering。例如:

#include<iostream>

struct B{
    int a;
    long b;
    auto operator <=> (const B&) const = default;
};

struct D : B{
    short c;
    auto operator <=> (const D&) const = default;
};

int main(int argc,char* argv[]){
    D x1,x2;
    std::cout<<typeid(decltype(x1 <=> x2)).name()<<std::endl;
}

上面这段代码用MSVC编译运行会输出class std::strong_ordering。

请注意,默认情况下自定义类型是不存在三向比较运算符函数的,需要用户显式默认声明,比如在结构体B和D中声明auto operator <=> (const B&) const = default;和auto operator <=> (const D&)const = default;。

 如果删除基类的<=>运算符,派生类显式定义的<=>将被删除。

如果删除派生类的<=>,保留基类的<=>,还可以运行。

 对结构体B而言,由于int和long的比较结果都是std::strong_ordering,因此结构体B的三向比较结果也是std::strong_ordering。同理,对于结构体D,其基类和成员的比较结果是std::strong_ordering,D的三向比较结果同样是std::strong_ordering。

另外,明确运算符的返回类型,使用std::strong_ ordering替换auto也是没问题的。


2.2 std::weak_ordering

std::weak_ordering类型也有3种比较结果,分别为std::weak_ ordering::less、std::weak_ordering::equivalent以及std::weak_ ordering::greater。std::weak_ordering的含义正好与std::strong_ ordering相对,表达的是不可替换性。即若有lhs == rhs,则rhs和lhs不可以相互替换,也就是fx(lhs) != fx(rhs)。这种情况在基础类型中并没有,但是它常常发生在用户自定义类中,比如一个大小写不敏感的字符串类:

#include <compare>
#include <string>
#include <iostream>

int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}

class CIString {
public:
  CIString(const char *s) : str_(s) {}

  std::weak_ordering operator<=>(const CIString& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换
  }
private:
  std::string str_;
};

int main(int argc,char* argv[])
{
    auto res = 'a'<=>'a';
    std::cout << typeid(res).name()<<std::endl;      //strong_ordering
    std::cout << typeid(res<=>0).name()<<std::endl;  //strong_ordering
    std::cout << typeid( ((std::weak_ordering)res) ).name()<<std::endl; //strong_ordering可以转为weak_ordering
    
    CIString s1{ "HELLO" }, s2{"hello"};
    std::cout << std::boolalpha << (s1 <=> s2 == 0)<<std::endl; // 输出为true
    std::cout << typeid(s1<=>s2).name()<<std::endl;  //weak_ordering
    return 0;
}

 

以上代码实现了一个简单的大小写不敏感的字符串类,它对于s1和s2的比较结果是std::weak_ordering::equivalent,表示两个操作数是等价的,但是它们不是相等的也不能相互替换。当std::weak_ordering和std::strong_ ordering同时出现在基类和数据成员的类型中时,该类型的三向比较结果是std::weak_ordering,例如:

#include <compare>
#include <string>
#include <iostream>

int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}

class CIString {
public:
  CIString(const char *s) : str_(s) {}

  std::weak_ordering operator<=>(const CIString& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换
  }
private:
  std::string str_;
};

struct B{
    int a=0;
    long b=0;
    std::strong_ordering operator <=> (const B&) const = default;
};

struct D : B{
    CIString c{""};
    auto operator <=> (const D&) const = default;
};

int main(int argc,char* argv[])
{
    D w1,w2;
    std::cout << std::boolalpha << (w1 <=> w2 == 0)<<std::endl; // 输出为true
    std::cout << std::boolalpha << (w1 <=> w2 == std::weak_ordering::equivalent)<<std::endl; // 输出为true
    std::cout << typeid(w1<=>w2).name()<<std::endl;  //weak_ordering
    return 0;
}

用MSVC编译运行上面这段代码会输出class std::weak_ordering,因为D中的数据成员CIString的三向比较结果为std::weak_ordering。请注意,如果显式声明默认三向比较运算符函数为std::strong_ordering operator <=> (const D&) const = default;,那么一定会遭遇到一个编译错误。


2.3 std::partial_ordering

std::partial_ordering类型有4种比较结果,分别为std::partial_ ordering::less、std::partial_ordering::equivalent、std::partial_ ordering::greater以及std::partial_ordering::unordered。std:: partial_ordering约束力比std::weak_ordering更弱,它可以接受当lhs == rhs时rhs和lhs不能相互替换,同时它还能给出第四个结果std::partial_ordering::unordered,表示进行比较的两个操作数没有关系。比如基础类型中的浮点数:

#include <iostream>

int main(int argc,char* argv[])
{
    std::cout << typeid(decltype(7.7 <=> 11.1)).name();//输出partial_ordering
    return 0;
}

用MSVC编译运行以上代码会输出class std::partial_ordering。之所以会输出class std::partial_ordering而不是std::strong_ordering,是因为浮点的集合中存在一个特殊的NaN,它和其他浮点数值是没关系的:

#include <iostream>

int main(int argc,char* argv[])
{
    std::cout<<std::boolalpha<< ((0.0/0.0 <=> 1.0) == std::partial_ordering::unordered);//输出true
    return 0;
}

这段代码编译输出的结果为true。

当std::weak_ordering和std:: partial_ordering同时出现在基类和数据成员的类型中时,该类型的三向比较结果是std::partial_ordering,例如:

#include <compare>
#include <string>
#include <iostream>
 
int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}
 
class CIString {
public:
  CIString(const char *s) : str_(s) {}
 
  std::weak_ordering operator<=>(const CIString& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换
  }
private:
  std::string str_;
};
 
struct B{
    int a=0;
    long b=0;
    std::strong_ordering operator <=> (const B&) const = default;
};
 
struct D : B{
    CIString c{""};
    float u=0.0;
    auto operator <=> (const D&) const = default;
};
 
int main(int argc,char* argv[])
{
    D w1,w2;
    std::cout << std::boolalpha << (w1 <=> w2 == 0)<<std::endl; // 输出为true
    std::cout << std::boolalpha << (w1 <=> w2 == std::partial_ordering::equivalent)<<std::endl; // 输出为true
    std::cout << typeid(w1<=>w2).name()<<std::endl;  //partial_ordering
    return 0;
}


用MSVC编译运行以上代码会输出class std::partial_ordering,因为D中的数据成员u的三向比较结果为std::partial_ordering,同样,显式声明为其他返回类型也会让编译器报错。在C++20的标准库中有一个模板元函数std::common_comparison_category,它可以帮助我们在一个类型合集中判断出最终三向比较的结果类型,当类型合集中存在不支持三向比较的类型时,该模板元函数返回void。

再次强调一下,std::strong_ordering、std::weak_ordering和`std::partial_ordering`只能与`0`和类型自身比较。深究其原因,是这3个类只实现了参数类型为自身类型和`nullptr_t的比较运算符函数。


三、对基础类型的支持

  • 3.1.对两个算术类型的操作数进行一般算术转换,然后进行比较。其中整型的比较结果为std::strong_ordering,浮点型的比较结果为std::partial_ordering。例如7 <=> 11.1中,整型7会转换为浮点类型,然后再进行比较,最终结果为std::partial_ordering类型。
  • 3.2.对于无作用域枚举类型和整型操作数,枚举类型会转换为整型再进行比较,无作用域枚举类型无法与浮点类型比较:
enum color {
  red
};

auto r = red <=> 11;   //编译成功
auto r = red <=> 11.1; //编译失败
  • 3.3.对两个相同枚举类型的操作数比较结果,如果枚举类型不同,则无法编译。
  • 3.4.对于其中一个操作数为bool类型的情况,另一个操作数必须也是bool类型,否则无法编译。比较结果为std::strong_ordering。
  • 3.5.不支持作比较的两个操作数为数组的情况,会导致编译出错,例如:
int arr1[5];
int arr2[5];
auto r = arr1 <=> arr2; // 编译失败
  • 3.6.对于其中一个操作数为指针类型的情况,需要另一个操作数是同样类型的指针,或者是可以转换为相同类型的指针,比如数组到指针的转换、派生类指针到基类指针的转换等,最终比较结果为std::strong_ordering:
char arr1[5];
char arr2[5];
char* ptr = arr2;
auto r = ptr <=> arr1;

上面的代码可以编译成功,若将代码中的arr1改写为int arr1[5],则无法编译,因为int [5]无法转换为char *。如果将char * ptr = arr2;修改为void * ptr = arr2;,代码就可以编译成功了。


四、自动生成的比较运算符函数

4.1 std::rel_ops的作用

标准库中提供了一个名为std::rel_ops的命名空间,在用户自定义类型已经提供了==运算符函数和<运算符函数的情况下,帮助用户实现其他4种运算符函数,包括!=、>、<=和>=。

 代码:

#include <compare>
#include <string>
#include <iostream>
#include <utility>
 
int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}
 
class CIString2 {
public:
  CIString2(const char* s) : str_(s) {}

  bool operator < (const CIString2& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) < 0;
  }
  
  bool operator== (const CIString2& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) == 0;
  }
private:
  std::string str_;
};


int main(int argc,char* argv[])
{
    using namespace std::rel_ops;
    CIString2 s1( "hello" ), s2( "world" );
    bool r = true;
    r = s1 == s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 != s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 > s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 >= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 < s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 <= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    return 0;
}

输出:

false
true
false
false
true
true

4.2 使用<=>

不过因为C++20标准有了三向比较运算符的关系,所以不推荐上面这种做法了。C++20标准规定,如果用户为自定义类型声明了三向比较运算符,那么编译器会为其自动生成<、>、<=和>=这4种运算符函数。对于CIString我们可以直接使用这4种运算符函数:

#include <compare>
#include <string>
#include <iostream>
#include <utility>
 
int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}
 
class CIString {
public:
  CIString(const char *s) : str_(s) {}
  
//   bool operator== (const CIString& b) const {
//       return ci_compare(str_.c_str(), b.str_.c_str()) == 0;
//   }
 
  std::weak_ordering operator<=>(const CIString& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换
  }
private:
  std::string str_;
};

int main(int argc,char* argv[])
{
    CIString s1( "hello" ), s2( "world" );
    bool r = true;
    // r = s1 == s2;
    // std::cout<<std::boolalpha<<r<<std::endl;
    // r = s1 != s2;
    // std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 > s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 >= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 < s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 <= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    return 0;
}

输出

false
false
true
true

那么这里就会产生一个疑问,很明显三向比较运算符能表达两个操作数是相等或者等价的含义,为什么标准只允许自动生成4种运算符函数,却不能自动生成==和=!这两个运算符函数呢?实际上这里存在一个严重的性能问题。在C++20标准拟定三向比较的早期,是允许通过三向比较自动生成6个比较运算符函数的,而三向比较的结果类型也不是3种而是5种,多出来的两种分别是std::strong_ equality和std::weak_equality。但是在提案文档p1190中提出了一个严重的性能问题。简单来说,假设有一个结构体:

struct S {
    std::vector<std::string> names;
    auto operator<=>(const S &) const = default;
};

它的三向比较运算符的默认实现这样的:

template<typename T>
std::strong_ordering operator<=>(const std::vector<T>& lhs, const std::vector<T> & rhs) 
{
    size_t min_size = min(lhs.size(), rhs.size());
    for (size_t i = 0; i != min_size; ++i) {
        if (auto const cmp = std::compare_3way(lhs[i], rhs[i]); cmp != 0) {
            return cmp;
        }
    }
    return lhs.size() <=> rhs.size();
}


这个实现对于<和>这样的运算符函数没有问题,因为需要比较容器中的每个元素。但是==运算符就显得十分低效,对于==运算符高效的做法是先比较容器中的元素数量是否相等,如果元素数量不同,则直接返回false:

template<typename T>
bool operator==(const std::vector<T>& lhs, const std::vector<T>& rhs)
{
    const size_t size = lhs.size();
    if (size != rhs.size()) {
        return false;
    }

    for (size_t i = 0; i != size; ++i) {
        if (lhs[i] != rhs[i]) {
            return false;
        }
    }
    return true;
}

想象一下,如果标准允许用三向比较的算法自动生成==运算符函数会发生什么事情,很多旧代码升级编译环境后会发现运行效率下降了,尤其是在容器中元素数量众多且每个元素数据量庞大的情况下。很少有程序员会注意到三向比较算法的细节,导致这个性能问题难以排查。基于这种考虑,C++委员会修改了原来的三向比较提案,规定声明三向比较运算符函数只能够自动生成4种比较运算符函数。由于不需要负责判断是否相等,因此std::strong_equality和std::weak_ equality也退出了历史舞台。对于==和!=两种比较运算符函数,只需要多声明一个==运算符函数,!=运算符函数会根据前者自动生成:

#include <compare>
#include <string>
#include <iostream>
#include <utility>
 
int ci_compare(const char* s1, const char* s2)
{
  while (tolower(*s1) == tolower(*s2++)) {
       if (*s1++ == '\0') {
            return 0;
       }
  }
  return tolower(*s1) - tolower(*--s2);
}
 
class CIString {
public:
  CIString(const char *s) : str_(s) {}
  
  bool operator== (const CIString& b) const {
      return ci_compare(str_.c_str(), b.str_.c_str()) == 0;
  }
 
  std::weak_ordering operator<=>(const CIString& b) const {
       return ci_compare(str_.c_str(), b.str_.c_str()) <=> 0;//strong_ordering返回为weak_ordering类型,实际上发生了类型转换
  }
private:
  std::string str_;
};

int main(int argc,char* argv[])
{
    CIString s1( "hello" ), s2( "world" );
    bool r = true;
    r = s1 == s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 != s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 > s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 >= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 < s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    r = s1 <= s2;
    std::cout<<std::boolalpha<<r<<std::endl;
    return 0;
}
false
true
false
false
true
true

五、兼容他旧代码

现在C++20标准已经推荐使用<=>和==运算符自动生成其他比较运算符函数,而使用<、==以及std::rel_ops生成其他比较运算符函数则会因为std::rel_ops已经不被推荐使用而被编译器警告。那么对于老代码,我们是否需要去实现一套<=>和==运算符函数呢?其实大可不必,C++委员会在裁决这项修改的时候已经考虑到老代码的维护成本,所以做了兼容性处理,即在用户自定义类型中,实现了<、==运算符函数的数据成员类型,在该类型的三向比较中将自动生成合适的比较代码。比如:

#include <iostream>
 
struct Legacy {
  int n=0;
  bool operator==(const Legacy& rhs) const
  {
       return n == rhs.n;
  }
  bool operator<(const Legacy& rhs) const
  {
       return n < rhs.n;
  }
};

struct TreeWay {
  Legacy m;
  std::strong_ordering operator<=>(const TreeWay &) const = default;
};

int main(int argc,char* argv[])
{
    TreeWay t1, t2;
    bool r = t1 < t2;
    std::cout<<std::boolalpha<<r<<std::endl;
    return 0;
}

在上面的代码中,结构体TreeWay的三向比较操作会调用结构体Legacy中的<和==运算符来完成,其代码类似于:

struct TreeWay {
  Legacy m;
  std::strong_ordering operator<=>(const TreeWay& rhs) const {
       if (m < rhs.m) return std::strong_ordering::less;
       if (m == rhs.m) return std::strong_ordering::equal;
       return std::strong_ordering::greater;
  }
};

需要注意的是,这里operator<=>必须显式声明返回类型为std::strong_ ordering,使用auto是无法通过编译的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2377945.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Cursor开发酒店管理系统

目录&#xff1a; 1、后端代码初始化2、使用Cursor打开spingboot项目3、前端代码初始化4、切换其他大模型5、Curosr无限续杯 1、后端代码初始化 找一个目录&#xff0c;使用idea在这个目录下新建springboot的项目。 2、使用Cursor打开spingboot项目 在根目录下新建.cursor文件…

图像对比度调整(局域拉普拉斯滤波)

一、背景介绍 之前刷对比度相关调整算法&#xff0c;找到效果不错&#xff0c;使用局域拉普拉斯做图像对比度调整&#xff0c;尝试复现和整理了下相关代码。 二、实现流程 1、基本原理 对输入图像进行高斯金字塔拆分&#xff0c;对每层的每个像素都针对性处理&#xff0c;生产…

如何在本地打包 StarRocks 发行版

字数 615&#xff0c;阅读大约需 4 分钟 最近我们在使用 StarRocks 的时候碰到了一些小问题&#xff1a; • 重启物化视图的时候会导致视图全量刷新&#xff0c;大量消耗资源。- 修复 PR&#xff1a;https://github.com/StarRocks/starrocks/pull/57371• excluded_refresh_tab…

git使用的DLL错误

安装好git windows客户端打开git bash提示 Error: Could not fork child process: Resource temporarily unavailable (-1). DLL rebasing may be required; see ‘rebaseall / rebase –help’. 提示 MINGW64的DLL链接有问题&#xff0c;其实是Windows的安全中心限制了&…

区块链blog1__合作与信任

&#x1f342;我们的世界 &#x1f33f;不是孤立的&#xff0c;而是网络化的 如果是单独孤立的系统&#xff0c;无需共识&#xff0c;而我们的社会是网络结构&#xff0c;即结点间不是孤立的 &#x1f33f;网络化的原因 而目前并未发现这样的理想孤立系统&#xff0c;即现实中…

从数据包到可靠性:UDP/TCP协议的工作原理分析

之前我们已经使用udp/tcp的相关接口写了一些简单的客户端与服务端代码。也了解了协议是什么&#xff0c;包括自定义协议和知名协议比如http/https和ssh等。现在我们再回到传输层&#xff0c;对udp和tcp这两传输层巨头协议做更深一步的分析。 一.UDP UDP相关内容很简单&#xf…

【CanMV K230】AI_CUBE1.4

《k230-AI 最近小伙伴有做模型的需求。所以我重新捡起来了。正好把之前没测过的测一下。 这次我们用的是全新版本。AICUBE1.4.dotnet环境9.0 注意AICUBE训练模型对硬件有所要求。最好使用独立显卡。 有小伙伴说集显也可以。emmmm可以试试哈 集显显存2G很勉强了。 我们依然用…

vscode 默认环境路径

目录 1.下面放在项目根目录上&#xff1a; 2.settings.json内容&#xff1a; 自定义conda环境断点调试 启动默认参数&#xff1a; 1.下面放在项目根目录上&#xff1a; .vscode/settings.json 2.settings.json内容&#xff1a; {"python.analysis.extraPaths"…

支付宝授权登录

支付宝授权登录 一、场景 支付宝小程序登录&#xff0c;获取用户userId 二、注册支付宝开发者账号 1、支付宝开放平台 2、点击右上角–控制台&#xff0c;创建小程序 3、按照步骤完善信息&#xff0c;生成密钥时会用到的工具 4、生成的密钥&#xff0c;要保管好&#xff…

Fabric 服务端插件开发简述与聊天事件监听转发

原文链接&#xff1a;Fabric 服务端插件开发简述与聊天事件监听转发 < Ping通途说 0. 引言 以前写过Spigot的插件&#xff0c;非常简单&#xff0c;仅需调用官方封装好的Event类即可。但Fabric这边在开发时由于官方文档和现有互联网资料来看&#xff0c;可能会具有一定的误…

电商物流管理优化:从网络重构到成本管控的全链路解析

大家好&#xff0c;我是沛哥儿。作为电商行业&#xff0c;我始终认为物流是电商体验的“最后一公里”&#xff0c;更是成本控制的核心战场。随着行业竞争加剧&#xff0c;如何通过物流网络优化实现降本增效&#xff0c;已成为电商企业的必修课。本文将从物流网络的各个环节切入…

Unity:延迟执行函数:Invoke()

目录 Unity 中的 Invoke() 方法详解 什么是 Invoke()&#xff1f; 基本使用方法 使用要点 延伸功能 ❗️Invoke 的局限与注意事项 在Unity中&#xff0c;延迟执行函数是游戏逻辑中常见的需求&#xff0c;比如&#xff1a; 延迟切换场景 延迟播放音效或动画 给玩家时间…

移植RTOS,发现任务栈溢出怎么办?

目录 1、硬件检测方法 2、软件检测方法 3、预防堆栈溢出 4、处理堆栈溢出 在嵌入式系统中&#xff0c;RTOS通过管理多个任务来满足严格的时序要求。任务堆栈管理是RTOS开发中的关键环节&#xff0c;尤其是在将RTOS移植到新硬件平台时。堆栈溢出是嵌入式开发中常见的错误&am…

【设计模式】- 结构型模式

代理模式 给目标对象提供一个代理以控制对该对象的访问。外界如果需要访问目标对象&#xff0c;需要去访问代理对象。 分类&#xff1a; 静态代理&#xff1a;代理类在编译时期生成动态代理&#xff1a;代理类在java运行时生成 JDK代理CGLib代理 【主要角色】&#xff1a; 抽…

数据服务共享平台方案

该文档聚焦数据服务共享平台方案,指出传统大数据管理存在数据定义不统一、开发困难、共享不足等挑战,提出通过自服务大数据平台实现数据 “采、存、管、用” 全流程优化,涵盖数据资产管理、自助数据准备、服务开发与共享、全链路监控等功能,并通过国家电网、东方航空、政府…

skywalking使用教程

skywalking使用教程 一、介绍 skywalking 1.1 概念 skywalking是分布式系统的应用程序性能监视工具&#xff0c;专为微服务、云原生架构和基于容器&#xff08;Docker、K8s、Mesos&#xff09;架构而设计。SkyWalking 是观察性分析平台和应用性能管理系统&#xff0c;提供分布…

C 语 言 - - - 简 易 通 讯 录

C 语 言 - - - 简 易 通 讯 录 代 码 全 貌 与 功 能 介 绍通 讯 录 的 功 能 说 明通 讯 录 效 果 展 示代 码 详 解contact.hcontact.ctest.c 总 结 &#x1f4bb;作 者 简 介&#xff1a;曾 与 你 一 样 迷 茫&#xff0c;现 以 经 验 助 你 入 门 C 语 言 &#x1f4a1;个 …

机器学习知识自然语言处理入门

一、引言&#xff1a;当文字遇上数学 —— 自然语言的数字化革命 在自然语言处理&#xff08;NLP&#xff09;的世界里&#xff0c;计算机要理解人类语言&#xff0c;首先需要将文字转化为数学向量。早期的 One-Hot 编码如同给每个词语分配一个唯一的 “房间号”&#xff0c;例…

MySQL数据库——支持远程IP访问的设置方法总结

【系列专栏】&#xff1a;博主结合工作实践输出的&#xff0c;解决实际问题的专栏&#xff0c;朋友们看过来&#xff01; 《项目案例分享》 《极客DIY开源分享》 《嵌入式通用开发实战》 《C语言开发基础总结》 《从0到1学习嵌入式Linux开发》 《QT开发实战》 《Android开发实…

Pageassist安装(ollama+deepseek-r1)

page-assist网站&#xff1a;https://github.com/n4ze3m/page-assist 首先电脑配置node.js&#xff0c;管理员打开命令窗口输入下面命令下载bun npm install -g buncd 到你想要安装page-assist的地方&#xff08;推荐桌面&#xff09; 输入下列命令 git clone https://gith…