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

目录

一、简介

二、三向比较的返回类型

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_orderingstd::cout << typeid(res<=>0).name()<<std::endl;  //strong_orderingstd::cout << typeid( ((std::weak_ordering)res) ).name()<<std::endl; //strong_ordering可以转为weak_orderingCIString s1{ "HELLO" }, s2{"hello"};std::cout << std::boolalpha << (s1 <=> s2 == 0)<<std::endl; // 输出为truestd::cout << typeid(s1<=>s2).name()<<std::endl;  //weak_orderingreturn 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; // 输出为truestd::cout << std::boolalpha << (w1 <=> w2 == std::weak_ordering::equivalent)<<std::endl; // 输出为truestd::cout << typeid(w1<=>w2).name()<<std::endl;  //weak_orderingreturn 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_orderingreturn 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);//输出truereturn 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; // 输出为truestd::cout << std::boolalpha << (w1 <=> w2 == std::partial_ordering::equivalent)<<std::endl; // 输出为truestd::cout << typeid(w1<=>w2).name()<<std::endl;  //partial_orderingreturn 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.mzph.cn/news/906475.shtml

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

相关文章

计算机网络相关面试题

一、HTTP1.1和HTTP2的区别 HTTP/1&#xff08;主要指 HTTP/1.1&#xff09;和 HTTP/2 是 Web 协议发展中的两个重要版本&#xff0c;二者在性能、协议机制和功能特性上有显著差异。以下从多个维度对比分析&#xff0c;并结合具体案例说明&#xff1a; 一、连接与请求处理方式 1…

图论算法精解(Java 实现):从基础到高频面试题

一、图的基础表示方法 1.1 邻接矩阵&#xff08;Adjacency Matrix&#xff09; 邻接矩阵是表示图的一种直观方式&#xff0c;它使用一个二维数组来存储节点之间的连接关系。对于一个有 n 个节点的图&#xff0c;邻接矩阵是一个 nn 的矩阵&#xff0c;其中 matrix [i][j] 表示…

江科大TIM定时器hal库实现

定时器相关hal库函数 hal库的定时器函数相比于标准库&#xff0c;多了很多的中断回调函数&#xff0c;同时对于定时器的初始化也改成使用句柄一次性顺带连带DMA等功能一起初始化了 typedef struct {uint32_t Prescaler; /*定时器的预分频值*/uint32_t CounterMode; …

CentOS 10:启动telnet服务

参考&#xff0c; 鳥哥私房菜 - 第七章、網路安全與主機基本防護&#xff1a;限制埠口, 網路升級與 SELinux 7.3.3 埠口与服务的启动/关闭及开机时状态设定 我们知道系统的 Telnet 服务通常是以 super daemon 来控管的&#xff0c;请您启动您系统的 telnet 试看看。 1 要启动 …

Taro 安全区域

目录 一、问题描述 二、问题解决 1、顶部刘海区 2、底部小黑条 一、问题描述 安全区域主要是为了避免刘海屏或底部栏遮挡&#xff0c;而造成的不良显示效果。 本次将针对以下两点进行考量&#xff1a; 1、顶部刘海屏区 2、苹果X底部小黑条 二、问题解决 通过Taro.getS…

【Java微服务组件】分布式协调P1-数据共享中心简单设计与实现

欢迎来到啾啾的博客&#x1f431;。 记录学习点滴。分享工作思考和实用技巧&#xff0c;偶尔也分享一些杂谈&#x1f4ac;。 欢迎评论交流&#xff0c;感谢您的阅读&#x1f604;。 目录 引言设计一个共享数据中心选择数据模型键值对设计 数据可靠性设计持久化快照 &#xff08…

在SpringBoot项目中,使用单元测试@Test

1.引入依赖 <!--单元测试Test的依赖--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>3.2.1</version> </dependency> 2.在src/test/java目录…

在Java中,将Object对象转换为具体实体类对象

在Java中&#xff0c;将Object对象转换为具体实体类对象可以通过以下几种方法实现&#xff1a; 1‌.使用instanceof关键字进行类型检查和转换‌&#xff1a; 首先&#xff0c;使用instanceof关键字检查Object对象是否为目标实体类的类型。 如果是&#xff0c;则进行强制类型…

JAVA学习-练习试用Java实现“音频文件的读取与写入 :使用Java音频库处理音频数据”

问题&#xff1a; java语言编辑&#xff0c;实现音频文件的读取与写入 &#xff1a;使用Java音频库处理音频数据。 解答思路&#xff1a; 在Java中处理音频文件通常需要使用第三方库&#xff0c;例如javax.sound.sampled包&#xff0c;它提供了处理音频文件的基本功能。以下是一…

Flink架构概览,Flink DataStream API 的使用,FlinkCDC的使用

一、Flink与其他组件的协同 Flink 是一个分布式、高性能、始终可用、准确一次&#xff08;Exactly-Once&#xff09;语义的流处理引擎&#xff0c;广泛应用于大数据实时处理场景中。它与 Hadoop 生态系统中的组件可以深度集成&#xff0c;形成完整的大数据处理链路。下面我们从…

linux 查看java的安装路径

一、验证Java安装状态 java -version正常安装会显示版本信息&#xff1a; openjdk version "1.8.0_65" OpenJDK Runtime Environment (build 1.8.0_65-b17) OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)二、检查环境变量配置 若已配置JAVA_HOME&#…

2025-5-21 个人笔记篇matlab小笔记和clang基础使用(简单记录)

个人笔记篇 再不记录就找不到了&#xff0c;之前学的一点基础&#xff0c;看看就行,请不要提问,因为很久了>_<(至少我看来是这样的) matlab小笔记 % 开绘制(新建) figure % 设置绘制标题 title(标题); % 设置绘制的X轴Lable xlabel(x); % 设置绘制的y轴Lable ylabel(cos…

前端JavaScript-嵌套事件

点击 如果在多层嵌套中&#xff0c;对每层都设置事件监视器&#xff0c;试试看 <!DOCTYPE html> <html lang"cn"> <body><div id"container"><button>点我&#xff01;</button></div><pre id"output…

网感驱动下开源AI大模型AI智能名片S2B2C商城小程序源码的实践路径研究

摘要&#xff1a;在数字化浪潮中&#xff0c;网感已成为内容创作者与商业运营者必备的核心能力。本文以开源AI大模型、AI智能名片及S2B2C商城小程序源码为技术载体&#xff0c;通过解析网感培养与用户需求洞察的内在关联&#xff0c;提出"数据驱动-场景适配-价值重构"…

AG-UI:重构AI代理与前端交互的下一代协议标准

目录 技术演进背景与核心价值协议架构与技术原理深度解析核心功能与标准化事件体系典型应用场景与实战案例开发者生态与集成指南行业影响与未来展望1. 技术演进背景与核心价值 1.1 AI交互的三大痛点 当前AI应用生态面临三大核心挑战: 交互碎片化:LangGraph、CrewAI等框架各…

游戏引擎学习第301天:使用精灵边界进行排序

回顾并为今天的内容做准备 昨天&#xff0c;我们解决了一些关于排序的问题&#xff0c;这对我们清理长期存在的Z轴排序问题很有帮助。这个问题我们一直想在开始常规游戏代码之前解决。虽然不确定是否完全解决了问题&#xff0c;但我们提出了一个看起来合理的排序标准。 有两点…

Ajax快速入门教程

输入java时&#xff0c;页面并没有刷新但是下面自动联想出了跟java有关的东西&#xff0c;像这种就叫异步交互 它不会妨碍你的输入&#xff0c;同时还能够同步进行对于java相关联想词的推送 发送异步请求需要借助工具axios 引入axios&#xff0c;可以直接在scripts中引入 get和…

Anti Spy安卓版:智能防护,守护手机安全

Anti Spy安卓版是一款专为安卓设备设计的智能防护应用&#xff0c;旨在帮助用户实时防护手机安全&#xff0c;抵御间谍软件、恶意软件和其他潜在威胁。它基于人工智能和启发式搜索方法的引擎&#xff0c;能够检测并阻止已知和未知的间谍软件、后门程序、账单欺诈、短信欺诈、电…

超低延迟音视频直播技术的未来发展与创新

引言 音视频直播技术正在深刻改变着我们的生活和工作方式&#xff0c;尤其是在教育、医疗、安防、娱乐等行业。无论是全球性的体育赛事、远程医疗、在线教育&#xff0c;还是智慧安防、智能家居等应用场景&#xff0c;都离不开音视频技术的支持。为了应对越来越高的需求&#x…

系统架构设计(十二):统一过程模型(RUP)

简介 RUP 是由 IBM Rational 公司提出的一种 面向对象的软件工程过程模型&#xff0c;以 UML 为建模语言&#xff0c;是一种 以用例为驱动、以架构为中心、迭代式、增量开发的过程模型。 三大特征 特征说明以用例为驱动&#xff08;Use Case Driven&#xff09;需求分析和测…