聊城手机网站制作阿里云网站备案多少天

当前位置: 首页 > news >正文

聊城手机网站制作,阿里云网站备案多少天,网站建设浙江,南安市住房和城乡建设部网站目录 运算符重载 1.概念 2.友元函数运算符重载 3.成员函数运算符重载 4.特殊运算符重载 1.赋值运算符重载 2.类型转换运算符重载 5.注意事项
std::string字符串类#xff1a; 模板与容器 模板 1.函数模板 2.类模板 类内实现 类内声明类外实现 运算符重载 1.概念…目录 运算符重载 1.概念 2.友元函数运算符重载 3.成员函数运算符重载 4.特殊运算符重载 1.赋值运算符重载 2.类型转换运算符重载 5.注意事项  std::string字符串类 模板与容器 模板 1.函数模板 2.类模板 类内实现 类内声明类外实现 运算符重载 1.概念 C中可以把部分的运算符开做成函数此时运算符也可以重载。 运算符预定义的操作只能针对基本数据类型。但是对于自定义类型也需要类似运算操作此时就可以重新定义这些运算符的功能使其支持特定类型完成特定的操作。 可以被重载的运算符         算术运算符、-、、/、%、、–         位操作运算符、|、~、^(位异或)、(左移)、(右移)         逻辑运算符!、、||         比较运算符、、、、、!         赋值运算符、、-、、/、%、、|、^、、         其他运算符[]、()、-、,、new、delete、new[]、delete[] 不被重载的运算符         成员运算符.、指针运算符*、三目运算符 ? : 、sizeof、作用域:: 运算符重载有两种实现方式1.友元函数运算符重载 2.成员函数运算符重载 2.友元函数运算符重载 #include iostream using namespace std;class MyInt { private:int a; public:MyInt(int a){this-aa;}int get_int(){return a;}friend MyInt operator (MyInt i,MyInt i2);friend MyInt operator (MyInt i);//前置friend MyInt operator (MyInt i,int);//后置 }; MyInt operator (MyInt i,MyInt i2) {return i.ai2.a; } MyInt operator (MyInt i) {return i.a; }MyInt operator (MyInt i,int) {return i.a; }int main() {MyInt int1(1);MyInt int2(2);MyInt int3int1int2;coutint1: int1.get_int()endl;coutint2: int2.get_int()endl;coutint3int1int2endl;coutint3: (int3).get_int()endl;coutint3: (int3).get_int()endl;coutint3: int3.get_int()endl;return 0; }3.成员函数运算符重载 成员函数运算符重载相比于友元函数运算符重载最主要的区别在于友元函数的第一个输入参数在成员函数中使用this指针代替因此同样的运算符重载成员函数比友元函数参数少一个。 #include iostream using namespace std;class MyInt { private:int a; public:MyInt(int a){this-aa;}int get_int(){return a;}MyInt operator (MyInt i);MyInt operator ();//前置MyInt operator (int);//后置 }; MyInt MyInt::operator (MyInt i) {return this-ai.a; } MyInt MyInt::operator () {return this-a; }MyInt MyInt::operator (int) {return this-a; }int main() {MyInt int1(1);MyInt int2(2);MyInt int3int1int2;coutint1: int1.get_int()endl;coutint2: int2.get_int()endl;coutint3int1int2endl;coutint3: (int3).get_int()endl;coutint3: (int3).get_int()endl;coutint3: int3.get_int()endl;return 0; }4.特殊运算符重载 1.赋值运算符重载 除了之前学习的无参构造函数、拷贝构造函数、析构函数以外如果程序员不手写编译器还会添加一个赋值运算符重载函数。         赋值运算符重载只能使用成员函数运算符实现。         当类中出现指针类型的成员变量时默认的赋值运算符重载函数类似于默认的浅拷贝构造函数因此需要手动编写解决“浅拷贝”的问题。 #include iostream using namespace std;class MyInt {int a; public:MyInt(int a){this-aa;}int get_int(){return a;}MyInt operator (MyInt i){cout调用赋值运算符重载函数endl;this-ai.a;return *this;} };int main() {MyInt int1(1);MyInt int2(2);coutint1: int1.get_int()endl;coutint2: int2.get_int()endl;int2int1;coutint2: int2.get_int()endl;return 0; }2.类型转换运算符重载 必须使用成员函数运算符重载且格式比较特殊。 #include iostream using namespace std;class MyInt {int a;string bhello; public:MyInt(int a){this-aa;}operator int(){return a;}operator string(){return b;} };int main() {MyInt int1(1);int aint1;string bint1;coutint a aendl;coutstring b bendl;return 0; }5.注意事项  1.重载的运算符限制在C语言中已有的运算符范围不能创建新的运算符。 2.运算符重载本质上也是函数重载但是不支持函数默认值设定。 3.重载之后运算符不能改变运算符的优先级和结合性也不能改变运算符的操作数和语法结构。 4.运算符重载必须基于或包含自定义类型即不能改变基本数据类的运算规则。 5.重载的功能应该与原有的功能相似避免没有目的的滥用运算符重载。 6.一般情况下双目运算符建议使用友元函数运算符重载单目运算符建议使用成员函数运算符重载。 std::string字符串类 字符串对象是一种特殊类型的容器专门设计用于操作字符串。 #include iostream #include string.h using namespace std;int main() {string s;// 判断是否为空couts.empty()endl; //1string s1hello;//隐式调用构造函数couts1endl; //hellostring s2(world);//显式调用构造函数couts2endl; //world//、!、 、 判断cout(s1s2)endl; //0cout(s1!s2)endl; //1cout(s1s2)endl; //0cout(s1s2)endl; //1string s3(s2);//拷贝构造函数couts3endl; //world// 参数1char * 源字符串// 参数2保留的字符串数string s4(ABCDE,3);couts4endl; //ABC// 参数1std::string 源字符串// 参数2不保留的字符数从头开始string s5(s2,3);couts5endl; //ld// 参数1字符数量// 参数2字符内容 charstring s6(5,a);couts6endl; //aaaaacout 原s5 s5 原s6 s6 endl;swap(s5,s6); // 交换cout 交换后s5 s5 交换后s6 s6 endl;string s7s1s2;//字符串连接couts7endl; //helloworlds7.append(你好世界);// 向后追加字符串couts7endl; //helloworld你好世界s7.push_back(a);// 向后追加单个字符couts7endl; //helloworld你好世界a// 插入// 参数1插入的位置// 参数2插入的内容s7.insert(0,123);couts7endl; //123helloworld你好世界a// 删除字符串// 参数1起始位置// 参数2删除的字符数量s7.erase(2,5);couts7endl; //12oworld你好世界a// 替换// 参数1起始位置// 参数2被替换的字符数// 参数3替换的新内容s7.replace(0,3,);couts7endl; //world你好世界as7.clear(); // 清空couts7.length()endl; //0string s8heiheihei;couts8endl; //heiheiheis8ABCDEFG; // 重新赋值couts8endl; //ABCDEFG// 参数1拷贝的目标// 参数2拷贝的字符数量// 参数3拷贝的起始位置// C的string到C语言数组char a[20]{0};s8.copy(a,3,0);coutaendl; //ABC// C string 到 c string 用到了C语言中的strcpy// c_str C的字符串转成C语言的字符数组// c_str 返回一个 const char *char b[20]{0};strcpy(b,s8.c_str());coutbendl; //ABCDEFGreturn 0; }模板与容器 模板 模板可以让类或者函数支持一种通用类型这种通用类型在实际运行的过程中可以使用任何数据类型因此可以写一些与类型无关的代码这种编程方式也被称为“泛型编程”。 通常有两种形式 1.函数模板 2.类模板 1.函数模板 使一个函数支持模板编程可以使函数支持通用数据类型。 #include iostream using namespace std; templatetypename T    // typename也可以是class T add(T a,T b) {return a b; }int main() {string str1 hello;string str2 world;cout add(str1,str1) endl;return 0; } 2.类模板 使一个类支持模板编程可以使一个类支持通用数据类型。 类内实现 #include iostream using namespace std;templateclass T class Test { private:T val; public:Test(T v){val v;}T get_val()const{return val;}void set_val(const T val){this-val val;} };int main() {Testint t1(20);cout t1.get_val() endl;t1.set_val(10);cout t1.get_val() endl;t1.set_val(10.32);cout t1.get_val() endl;   // 数据窄化10Testdouble t2(2.56);cout t2.get_val() endl;t2.set_val(23.12);cout t2.get_val() endl;return 0; } 类内声明类外实现 #include iostream using namespace std;templateclass T class Test { private:T val; public:Test(T v);T get_val()const;void set_val(const T val); };templateclass T TestT::Test(T v) {val v; }templateclass T T TestT::get_val()const {return val; }templateclass T void TestT::set_val(const T val) {this-val val; }int main() {Testint t1(20);cout t1.get_val() endl;t1.set_val(10);cout t1.get_val() endl;t1.set_val(10.32);cout t1.get_val() endl;   // 数据窄化10Testdouble t2(2.56);cout t2.get_val() endl;t2.set_val(23.12);cout t2.get_val() endl;return 0; }