#include<iostream> using namespace std; //int main() //{ // cout << "Hello Newcoder!" << endl; // system("pause"); // return 0; //} //#define Day 7 //int main() //{ // cout << "一周有:" << Day << "天" << endl; // const int month = 12;// // //month=24;//const修饰的变量也成为常量,不可修改 // cout << "一年有:" << month << "个月" << endl; // return 0; //} #include<windows.h> //int main() //{ // short num = 10; // double d = 3.1415926; // cout << "num="<<num << endl; // //Sleep(1000); // cout << sizeof(num) <<"为short的内存空间" << endl; // //Sleep(1000); // cout << d << endl; // int f1 = 3e2;//3*10^2; // cout << "f1=" << f1 << endl; // float f2 = 3e-2;//3*0.1^2; // cout << "f2=" << f2 << endl; // return 0; //} //int main() //{ // char ch = 'a'; // cout << ch << endl; // cout << sizeof(ch) << endl; // cout << (int)ch << endl;//字符对应的AscⅡ码值 a-97 A-65 // cout << "\"gidetimothy\"" << endl;//转义字符 // cout << "\\" << endl; // //常用转义字符 \n 换行符 \\反斜杠 \t水平制表符 // cout << "aaaa\thelloworld" << endl; // cout << "aa\thelloworld" << endl; // cout<<"aaaaaaa\thelloworld" << endl; // // return 0; //} #include<string> //int main() //{ // char str[] = "hello world"; // cout << str << endl; // string s = "hello nowcoder"; // cout << s << endl; // // return 0; //} //int main() //{ // //bool 布尔类型-占一个字节-bool类型只有两个值 1/0 // bool flag = true; // cout << true << endl; // flag = false; // cout << flag << endl; // cout << sizeof(bool) << endl;//1 // return 0; //} //int main() //{ // /*float a; // cin >> a; // cout << "浮点型a=" << a << endl;*/ // // /*string str ; // cin >> str; // cout << "字符串str=" << str << endl;*/ // // bool flag = false; // cin >> flag; // cout << "布尔类型的值=" << flag << endl;//布尔类型只要为非0的值都为1 // // return 0; //} //猜数字 #include<ctime> //int main() //{ // while (1) // { // cout << "猜数字 游戏开始!" << endl; // //添加一个随机数的种子 利用当前系统时间生成随机数,防止每次随机数都一样 // srand((unsigned int)time(NULL)); // //系统生成一个随机数 // int num; // num = rand() % 100 + 1; //rand()%100 生成0-99的随机数 // // //玩家进行猜测 // // while (true) // { // int val = 0; // cin >> val; // // //判断玩家的猜测 // if (val > num) // { // cout << "猜大了" << endl; // } // else if (val < num) // { // cout << "猜小了" << endl;//猜错 提示过大过小 重新返回第二步 // } // else // { // cout << "恭喜你 猜对了!";//猜对 退出游戏 // break; // } // } // Sleep(2000); // system("cls"); // } // return 0; //} //打印水仙花数 //int main() //{ // int num = 100; // do // { // int a, b, c; // a = num % 10; // b = num / 10 % 10; // c = num / 100; // if (pow(a, 3) + pow(b, 3) + pow(c, 3) == num) // { // cout << num << endl; // } // num++; // } while (num <1000); // // return 0; //} //int main() //{ // for (int i = 1; i <= 100; i++) // { // if (i == 7 || i % 7 == 0 || i / 10 == 7||i%10==7) // { // cout << "敲桌子" << endl; // } // else // { // cout << i << endl; // } // } // return 0; //} //打印9*9乘法表 //int main() //{ // for (int i = 1; i <= 9; i++) // { // for (int j = 1; j <= i; j++) // { // cout << i << "*" << j << "=" << i * j <<"\t"; // } // cout << endl; // } // return 0; //} //打印数组中的最大元素 // 选择排序 //int main() //{ // int arr[5] = { 300,350,200,400,250 }; // int max = 0; // for (int i = 0; i < 5; i++) // { // /*if (arr[i] > max) // { // max = arr[i]; // }*/ // max = arr[i] > max ? arr[i] : max; // } // cout << max << endl; // return 0; //} //数组逆置 //int main() //{ // int arr[5] = { 1,3,2,5,4 }; // int start = 0; // int end = sizeof(arr) / sizeof(arr[0])-1; // // while (start < end) // { // int tmp = arr[start]; // arr[start] = arr[end]; // arr[end] = tmp; // // start++; // end--; // } // for (int i = 0; i < 5; i++) // { // cout << arr[i] << endl; // } // return 0; //} //冒泡排序 //int main() //{ // int arr[] = { 4,2,8,0,5,7,1,3,9 }; // int sz = sizeof(arr) / sizeof(arr[0]); // for (int i = 0; i < sz-1; i++) // { // for (int j = 0; j < sz-i-1; j++) // { // if (arr[j] > arr[j+1]) // { // int tmp = arr[j+1]; // arr[j+1] = arr[j]; // arr[j] = tmp; // } // } // } // for (int i = 0; i < sz; i++) // { // cout << arr[i] << endl; // } // return 0; //} //qsort快速排序 //int cmp(const void* a, const void* b) //{ // // return (*(int*)a - *(int*)b); //} //int main() //{ // int arr[] = { 4,2,8,0,5,7,1,3,9 }; // int sz = sizeof(arr) / sizeof(arr[0]); // qsort(arr, sz, sizeof(int), cmp); // for (int i = 0; i < sz; i++) // { // cout << arr[i] << endl; // } // return 0; //} //二维数组-考试成绩的统计 //int main() //{ // int score[3][3] = { 100,100,100,90,50,100,60,70,80 }; // for (int i = 0; i < 3; i++) // { // int sum = 0; // for (int j = 0; j < 3; j++) // { // sum += score[i][j]; // } // cout << sum << endl; // } // return 0; //} //实现加法函数 //int Add(int a, int b);//函数的声明(可以有多次,定义只能有一次) //int main() //{ // int num1 = 10; // int num2 = 30; // int c=Add(num1, num2);//实参-当调用函数时,实参的值传递给形参 // cout << c << endl; // return 0; //} 定义 //int Add(int x, int y)//形参 //{ // int sum = x + y; // return sum; //} //值传递 //void swap(int num1, int num2) //{ // int tmp = num2; // num2 = num1; // num1 = tmp;//形参的变化无法改变实参 //} // //int main() //{ // int a = 10; // int b = 20; // swap(a, b);//无法交换 // cout << a <<" "<< b << endl; // return 0; //} //函数的分文件编写 // 函数的声明 //void swap(int a, int b); 函数的定义 //void swap(int a, int b) //{ // int tmp = a; // a = b; // b = tmp; // cout << "a=" << a << endl; // cout << "b=" << b << endl; //} //1.创建.h后缀名的头文件 //2.创建.cpp后缀名的源文件 //3.在头文件中写函数的声明 //4.在源文件中写函数的定义 //#include"swap.h" //int main() //{ // int a = 10; // int b = 20; // swap(a, b); // return 0; //} //指针 //int main() //{ // //空指针 // /*int* p = NULL;*/ // //空指针是不可以进行访问的 // // //野指针 // //int* p = (int*)0x1100; // //cout << *p << endl;//访问野指针报错 // // //const修饰指针 // //常量指针 // int a = 10; // const int *p1 = &a;//指针的指向可以修改 指针指向的值不可以改 // *p = 20;//错 // p = 20;//对 // // //指针常量 // int b = 10; // int* const p2 = &b;//指针的指向不可以改,指针的值可以改 // *p = 20;//对 // p = &c;//错 // // //const既修饰指针又修饰常量 // int c = 10; // const int* const p3 = &c;//指针的指向和指针的值都不可以改变 // *p3 = 20;//错 // p3 = &c;//错 // // // return 0; //} //int main() //{ // int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; // int* p = arr; // for (int i = 0; i < 10; i++) // { // cout << *p << endl; // p++; // } // return 0; //} //指针和函数 //void swap(int *p1, int *p2)//地址传递可以修改实参的值 //{ // int tmp = *p1; // *p1 = *p2; // *p2 = tmp; //} //int main() //{ // int a = 10; // int b = 20; // swap(&a, &b); // cout << a << " " << b << endl; // return 0; //} //指针 数组 函数 //冒泡排序 //void bubbleSort(int* arr, int len) //{ // for (int i = 0; i < len; i++) // { // for (int j = 0; j < len - i - 1; j++) // { // if (arr[j] > arr[j+1]) // { // int tmp = arr[j + 1]; // arr[j + 1] = arr[j]; // arr[j] = tmp; // } // } // } //} 打印数组 //void printArray(int* arr, int len) //{ // for (int i = 0; i < len; i++) // { // cout << arr[i] << endl; // } //} //int main() //{ // int arr[10] = { 4,2,5,6,8,0,1,3,9,7 }; // int len = sizeof(arr) / sizeof(arr[0]); // bubbleSort(arr, len); // printArray(arr, len); // // return 0; //} //结构体 /*struct student { string name; int age; int score; }s3; int main() { student s1; s1.age = 18; s1.name = "Gide"; s1.score = 100; cout << "姓名:" << s1.name<< " " << "年龄:" << s1.age <<" " << "分数:" << s1.score << endl; s3.name = "timothy"; cout << "name=" << s3.name << endl; return 0; }*/
//结构体数组 //struct student //{ // string name; // int age; // int score; //}; // //int main() //{ // struct student stuArray[3] ={{"张三",18,100},{"李四",20,99},{"王五",17,98}}; // stuArray[2].name = "Gide"; // stuArray[2].age = 80; // stuArray[2].score = 60; // // for (int i = 0; i < 3; i++) // { // cout << "姓名:" << stuArray[i].name<<" " << "年龄:" << stuArray[i].age<<" " << "分数:" << stuArray[i].score << endl; // } // return 0; //} //结构体指针 //struct student //{ // string name; // int age; // int score; //}; // //int main() //{ // student s = { "张三",18,100 }; // //通过指针指向结构体变量 // student* p = &s; // cout << "姓名:" << p->name << " " << "年龄:" << p->age << " " << "分数:" << p->score << endl; // // return 0; //} //结构体嵌套结构体 //struct student //{ // string name; // int age; // int score; //}; // //struct teacher //{ // int id; // string name; // int age; // struct student stu; //}; // //int main() //{ // teacher t; // t.id = 12345; // t.name = "王老师"; // t.age = 40; // t.stu.name = "小王"; // t.stu.age = 20; // t.stu.score = 60; // cout << "老师姓名:" << t.name << " " << "老师编号:" << t.id // << " " << "学生姓名: " << t.stu.name << " " << "学生成绩:" // << t.stu.score << endl; // return 0; //} //结构体做函数参数 //struct student //{ // string name; // int age; // int score; //}; 值传递 //void printStudent1(struct student s) //{ // cout << "姓名: " << s.name << " " << "年龄: " << s.age << " " << "分数:" << s.score << endl; //} 地址传递 //void printStudent2(struct student *p) //{ // cout << "姓名:" << p->name << " " << "年龄:" << p->age // << " " << "分数:" << p->score << endl; //} //int main() //{ // student s; // s.name = "张三"; // s.age = 19; // s.score = 100; // //printStudent1(s); // printStudent2(&s); // return 0; //} //结构体中const使用场景 // struct student //{ // string name; // int age; // int score; //}; // // void printStudent(const student *p) // { // //p->age = 150;//加入const后不可修改 // cout << "姓名:" << p->name << " " << "年龄:" << p->age // << " " << "分数:" << p->score << endl; // } // int main() // { // student s = { "张三",15,70 }; // printStudent(&s); // // return 0; // } //结构体案例 //struct student //{ // string sName; // int score; //}; //struct teacher //{ // string tName; // struct student sArray[5]; //}; //void allocateSpace(struct teacher tArray[], int len) //{ // string nameSeed = "ABCDE"; // for (int i = 0; i < len; i++) // { // tArray[i].tName = "Teacher_"; // tArray[i].tName += nameSeed[i]; // // for (int j = 0; j < 5; j++) // { // tArray[i].sArray[j].sName = "Student_"; // tArray[i].sArray[j].sName += nameSeed[j]; // // int random = rand() % 61 + 40;//40-100的随机数 // tArray[i].sArray[j].score = random; // } // } //} //void printinfo(struct teacher tArray[], int len) //{ // for (int i = 0; i < len; i++) // { // cout << "老师姓名:" << tArray[i].tName<<endl; // for (int j = 0; j < 5; j++) // { // cout << "\t学生姓名:" << tArray[i].sArray[j].sName << "考试分数:" << tArray[i].sArray[j].score << endl; // } // } //} //int main() //{ // //随机数种子 // srand((unsigned int)time(NULL)); // teacher tArray[3]; // int len = sizeof(tArray) / sizeof(tArray[0]); // allocateSpace(tArray, len); // printinfo(tArray, len); // // return 0; //} //结构体案例2 //struct hero //{ // string name; // int age; // string sex; //}; //void bubbleSort( struct hero hArray[], int len) //{ // for (int i = 0; i < len; i++) // { // for (int j = 0; j < len - i - 1; j++) // { // if (hArray[j].age> hArray[j + 1].age) // { // struct hero tmp = hArray[j + 1]; // hArray[j + 1] = hArray[j]; // hArray[j] = tmp; // } // // } // } //} //void printfhero(struct hero hArray[], int len) //{ // for (int i = 0; i < len; i++) // { // cout << "姓名: " << hArray[i].name <<" " << "年龄:" << hArray[i].age<<" " << "性别:" // << hArray[i].sex << endl; // } //} //int main() //{ // hero hArray[5] = // { // {"刘备",23,"男"}, // {"关羽",22,"男"}, // {"张飞",20,"男"}, // {"赵云",21,"男"}, // {"貂蝉",19,"女"} // }; // int len = sizeof(hArray) / sizeof(hArray[0]); // bubbleSort( hArray, len); // printfhero(hArray,len); // return 0; //} //通讯录管理系统 //#define MAX 1000 //void showMenu() //{ // cout << "**********************" << endl; // cout << "*****1.添加联系人*****" << endl; // cout << "*****2.显示联系人*****" << endl; // cout << "*****3.删除联系人*****" << endl; // cout << "*****4.查找联系人*****" << endl; // cout << "*****5.修改联系人*****" << endl; // cout << "*****6.清空联系人*****" << endl; // cout << "*****0.退出通讯录*****" << endl; // cout << "**********************" << endl; //} // //struct person //{ // string m_name; // int m_age; // int m_sex; // string m_phone; // string m_address; //}; // //struct addressbooks //{ // struct person pArray[MAX]; // int size; //}; // //void addPerson(addressbooks* abs) //{ // if (abs->size == MAX) // { // cout << "通讯录已满,无法添加" << endl; // return; // } // else // { // string name; // cout << "请输入要添加的姓名:" << endl; // cin >> name; // abs->pArray[abs->size].m_name = name; // // int sex=0; // cout << "1---男" << endl; // cout << "2---女" << endl; // while (true) // { // cin >> sex; // if (sex == 1 || sex == 2) // { // abs->pArray[abs->size].m_sex = sex; // break; // } // cout << "输入错误,请重新输入" << endl; // } // // int age; // cout << "请输入年龄" << endl; // cin >> age; // abs->pArray[abs->size].m_age = age; // // string phone; // cout << "请输入电话" << endl; // cin >> phone; // abs->pArray[abs->size].m_phone = phone; // // string address; // cout << "请输入地址" << endl; // cin >> address; // abs->pArray[abs->size].m_address = address; // // abs->size++; // cout << "添加成功!" << endl; // system("pause"); // system("cls"); // } //} // //void showPerson(addressbooks* abs) //{ // if (abs->size == 0) // { // cout << "通讯录为空" << endl; // } // else // { // for (int i = 0; i < abs->size; i++) // { // cout << "姓名:" << abs->pArray[i].m_name << "\t"; // cout << "性别:" << (abs->pArray[i].m_sex == 1 ? "男" : "女") << "\t"; // cout << "年龄:" << abs->pArray[i].m_age << "\t"; // cout << "电话:" << abs->pArray[i].m_phone << "\t"; // cout << "地址:" << abs->pArray[i].m_address << endl; // } // } // system("pause"); // system("cls"); //} // //int isExist(addressbooks* abs, string name) //{ // for (int i = 0; i < abs->size; i++) // { // if (abs->pArray[i].m_name == name) // { // return i; // } // } // return -1; //} //void deletePerson(addressbooks* abs) //{ // cout << "请输入需要删除的姓名:" << endl; // string name; // cin >> name; // int ret = isExist(abs, name); // if (ret == -1) // { // cout << "查无此人" << endl; // } // else // { // cout << "找到此人" << endl; // for (int i = ret; i < abs->size; i++) // { // abs->pArray[i] = abs->pArray[i + 1]; // } // abs->size--; // cout << "删除成功" << endl; // } // system("pause"); // system("cls"); //} //void findPerson(addressbooks* abs) //{ // cout << "请输入要查找的联系人" << endl; // string name; // cin >> name; // int ret = isExist(abs, name); // if (ret == -1) // { // cout << "查无此人" << endl; // } // else // { // cout << "查询到此人的信息" << endl; // cout << "姓名:" << abs->pArray[ret].m_name << "\t"; // cout << "性别:" << abs->pArray[ret].m_sex << "\t"; // cout << "年龄:" << abs->pArray[ret].m_age << "\t"; // cout << "电话:" << abs->pArray[ret].m_phone << "\t"; // cout << "地址" << abs->pArray[ret].m_address << endl; // } // system("pause"); // system("cls"); //} // //void modifyPerson(addressbooks* abs) //{ // cout << "请输入要修改的联系人姓名:" << endl; // string name; // cin >> name; // int ret = isExist(abs, name); // if (ret == -1) // { // cout << "查无此人!" << endl; // } // else // { // string name; // cout << "请输入姓名:" << endl; // cin >> name; // abs->pArray[ret].m_name = name; // // cout << "1---男" << endl; // cout << "2---女" << endl; // int sex=0; // while (true) // { // cin >> sex; // if (sex == 1 || sex == 2) // { // abs->pArray[ret].m_sex = sex; // break; // } // cout << "输入错误,请重新输入!" << endl; // } // // cout << "请输入年龄:" << endl; // int age; // cin >> age; // abs->pArray[ret].m_age = age; // // cout << "请输入电话" << endl; // string phone; // cin >> phone; // abs->pArray[ret].m_phone = phone; // // cout << "请输入地址" << endl; // string address; // cin >> address; // abs->pArray[ret].m_address = address; // } // system("pause"); // system("cls"); //} //void cleanPerson(addressbooks* abs) //{ // cout << "请问确定要清空通讯录" << endl; // cout << endl; // cout << "**1.确定****2.取消*****" << endl; // int select = 0; //here: // cin >> select; // switch (select) // { // case 1: // abs->size = 0; // cout << "通讯录已清空" << endl; // break; // case 2: // cout << "已取消" << endl; // break; // default: // cout << "输入错误,请重新输入!" << endl; // goto here; // } // system("pause"); // system("cls"); //} //int main() //{ // addressbooks abs; // abs.size = 0; // // while (true) // { // showMenu(); // int select; // cin >> select; // switch (select) // { // case 1: // addPerson(&abs); // break; // case 2: // showPerson(&abs); // break; // case 3: // deletePerson(&abs); // break; // case 4: // findPerson(&abs); // break; // case 5: // modifyPerson(&abs); // break; // case 6: // cleanPerson(&abs); // break; // case 0: // cout << "欢迎下次使用通讯录!" << endl; // return 0; // break; // default: // cout << "输入错误,请重新输入!" << endl; // break; // } // } // system("cls"); // return 0; //}
//全局变量、局部变量、静态变量 //int main() //{ // static int a = 10;//静态变量 // // return 0; //} //堆区开辟数据 //int* func() //{ // //利用new关键字 可以将数据开辟到堆区 // //指针 本质也是局部变量,放在栈上,指针保存的数据放在堆区 // int* p = new int(10); // return p; //} //int main() //{ // int* p = func(); // cout << *p << endl; // // return 0; //} //1.new的基本语法 //int* func() //{ // //在堆区创建整型数据 // //new返回是 该数据类型的指针 // int* p = new int(10); // return p; //} //void test01() //{ // int* p = func(); // cout << *p << endl; // cout << *p<< endl; // //堆区的数据由程序员开辟释放 // //释放堆区的数据用delete // delete p; // //cout << *p << endl;//内存已经被释放,再次访问就是非法操作 //} //void test02() //{ // //创建10个整型数组,在堆区 // int *arr=new int[10];//数组有10个元素 // for (int i = 0; i < 10; i++) // { // arr[i] = i + 100;//给10个元素赋值 100-109 // } // for (int i = 0; i < 10; i++) // { // cout << arr[i] << endl; // } // //释放堆区数组 // //释放数组的时候要加[] // delete[]arr; //} //int main() //{ // test01(); // test02(); // return 0; //} //引用 //int main() //{ // int a = 10; // //创建引用 // int &b = a;//b=a 1.引用必须直接初始化 int &b;-错误 // cout << a << endl; // cout << b << endl; // int c = 20; // b = c;//2.引用一旦初始化后,不可改变 // //b = 30; // cout << a<< endl; // cout <<b<< endl; // cout << c << endl; // // return 0; //} //引用做函数参数 // 1.值传递 //void swap01(int a, int b) //{ // int tmp = a; // a = b; // b = tmp; // cout << "a=" << a << endl; // cout << "b=" << b << endl; //} // //2.地址传递 //void swap02(int* a, int* b) //{ // int tmp = *a; // *a = *b; // *b = tmp; //} // //3.引用传递 //void swap03(int &a, int &b) //{ // int tmp = a; // a = b; // b = tmp; //} //int main() //{ // int a = 10; // int b = 20; // //swap01(a, b); // //swap02(&a, &b); // // swap03(a, b); // cout << "a=" << a << endl; // cout << "b=" << b << endl; // return 0; //} //引用做函数的返回值 //int& test01()//不要返回局部变量引用 //{ // int a = 10;//局部变量存放在四区中的栈区 // return a; //} // //int& test02()//函数的调用可以作为左值 //{ // static int a = 10;//静态变量存放在全局区、全局区上的数据在数据结束后系统释放 // return a; //} //int main() //{ // int& ref = test01(); // cout << "ref=" << ref << endl;//第一次结果正确,因为编译器做了保留 // cout << "ref=" << ref << endl;//第二次结果错误,因为a的内存已经释放 // // int& ref2 = test02(); // // cout << "ref2="<<ref2 << endl; // cout << "ref2=" << ref2 << endl; // // test02() = 1000; // cout << "ref2=" << ref2 << endl; // cout << "ref2=" << ref2 << endl; // // return 0; //} //引用的本质-就是一个指针常量 //void func(int &ref) //{ // ref = 100;//ref是引用,转换为*ref=100 //} // //int main() //{ // int a = 10; // // int& ref = a;//自动转换为int* const ref=&a;指针常量是指指针指向不可改 // ref = 20;//内部发现ref是引用,自动转换为*ref=20; // cout << "a:" << a << endl; // cout << "ref:" << ref << endl; // func(a);//引用一旦初始化后不可改变 // return 0; //} //常量引用-const //void showValue(const int &val) //{ // //val = 1000; // cout << "val=" << val << endl; //} //int main() //{ // //常量使用 // //使用场景:用来修饰形参,防止误操作 // //int a = 10;\ // //int &ref=10;//引用本身需要一个合法的内存空间,因此这行错误 // //const int& ref = 10;//加上const之后,编译器将代码修改 int tmp=10; const int& ref=tmp; // //ref = 20;//加入const之后变为可读,不可以修改 // int a = 100; // showValue(a); // cout << "a=" << a << endl; // return 0; //} //函数的默认参数 //int func(int a,int b=20,int c=30) //{ // return a + b + c; //} //int func2(int a, int b = 10; int c)//某个位置有默认参数,后面的都需要有默认值 //{ // return a + b + c; //} //如果函数的声明有默认参数,函数的实现就不能有默认参数 //int func2(int a=10, int b=10); //int func2(int a, int b) //{ // return a + b; //} // //int main() //{ // cout << func(10,30) << endl;//如果自己传入了数据,就用自己的。如果没传就用默认值 // cout<<func2(10, 20)<<endl; // return 0; //} //函数的占位参数 //void func(int a, int)//占位参数 //{ // cout << "this is func" << endl; //} //int main() //{ // func(10 ); // return 0; //} //函数重载 //1.同一个作用域下 //2.函数名称相同 //3.函数参数类型不同或个数不同或顺序不同 //void func() //{ // cout << "func的调用" << endl; //} //void func(int a) //{ // cout << "func的调用!" << endl; //} //void func(double a) //{ // cout << "func的调用!!" << endl; //} //void func(double a,int b) //{ // cout << "func的调用!!!" << endl; //} //int main() //{ // func(); // func(10); // func(10.00); // func(10.00,10); // // return 0; //} //函数重载的注意事项 //1.引用作为重载的条件 //void func(int& a) //{ // cout << "func(int &a)调用" << endl; //} //void func(const int& a) //{ // cout << "func(const int &a)调用" << endl; //} //int main() //{ // int a = 10; // func(a);//调用无const // func(20);//调用有const // return 0; //} //2.函数重载碰到默认参数 //void func2(int a,int b=20) //{ // cout << "func(int a,int b)调用" << endl; //} //void func2(int a) //{ // cout << "func(int a)调用" << endl; //} //int main() //{ // int a = 10; // int b = 20; // func2(a,b); // //func2(20);//当函数重载碰上默认参数,出现二义性 // return 0; //} //C++面向对象三大特性:封装、继承、多态 //封装 //设计一个圆类,求圆的周长 //const double PI = 3.14; class代表设计一个类,类后面紧跟着的就是类名称 //class Circle //{ // //访问权限 // // public: //公共权限 // //属性 // //半径 // int m_r; // //行为 // //获取圆的周长 // double calculateZC() // { // return 2 * PI * m_r; // } //}; //int main() //{ // //通过圆类创建一个具体的圆(对象) // //实例化(通过一个类创建一个对象的过程) // Circle c1; // //给圆对象的属性进行赋值 // c1.m_r = 10; // //2*PI*10=62.8 // cout << "圆的周长为:" << c1.calculateZC() << endl; // return 0; // } //class Student //{ // //类中的属性和行为统一称为成员 //private: // //属性 // string m_name; // int m_id; // //行为 //public: // void showStudent() // { // cout << "name=" << m_name << endl; // cout << "id=" << m_id << endl; // } // void setName(string name) // { // m_name = name; // } // void setID(int id) // { // m_id = id; // } //}; // //int main() //{ // Student s; // s.setName ("Gide"); // s.setID(123456); // s.showStudent(); //} //访问权限 三种 //公共权限public 成员 类内可以访问 类外可以访问 //保护权限protected 成员 类内可以访问 类外不可以访问 //私有权限private 成员 类内可以访问 类外不可以访问 //class Person //{ // //public: // //公共权限 // string m_name; // //protected://保护权限 // string m_car; // //private: // //私有权限 // int m_password; //public: // void func() // { // m_name = "张三";//类内所有权限均可以访问 // m_car = "拖拉机"; // m_password = 123456; // } //}; // //int main() //{ // Person p1; // p1.m_name = "李四"; // p1.m_car = "奔驰";//保护权限内容,在类外访问不到 // p1.m_password = "211212";//私有权限内容,在类外访问不到 // // return 0; //} //class和struct的区别 //访问权限不同 //struct默认权限为公有 //class默认权限为私有 //class C1 //{ // int m_A;//默认权限为私有 //}; //struct C2 //{ // int m_A;//默认权限为公有 //}; //int main() //{ // C1 c1; // c1.m_A = 100;//class里默认权限是私有 不可访问 // C2 c2; // c2.m_A = 100;// struct里默认权限是公有 可访问 // return 0; //} //成员属性设为私有 //自己控制读写权限 //class Person //{ //public: // //写姓名 // void setName(string name) // { // m_name = name; // } // //获取姓名 // string getName() // { // return m_name; // } // //写年龄 // void setage(int age) // { // if (age < 0 || age>150) // { // cout << "输入有误" << endl; // return; // } // m_age = age; // } // //获取年龄 // int getage() // { // return m_age; // } // //写情人 // void setlover(string lover) // { // m_lover = lover; // } //private: // //姓名 可读可写 // string m_name; // //年龄 可读可写 0-150 // int m_age; // //情人 只写 // string m_lover; //}; //int main() //{ // Person p; // p.setName("张三"); // cout << "姓名为:" << p.getName() << endl; // p.setage(100); // cout << "年龄为:" << p.getage() << endl; // p.setlover("李四"); // // return 0; //} //练习案例1 //class Cube //{ //public: // void setL(int l) // { // m_l = l; // } // int getL() // { // return m_l; // } // void setH(int h) // { // m_h = h; // } // int getH() // { // return m_h; // } // void setW(int w) // { // m_w = w; // } // int getW() // { // return m_w; // } // int calculateS() // { // return 2 * m_l * m_w + 2 * m_l * m_h + 2 * m_w * 2 * m_h; // } // int calculateV() // { // return m_l * m_w * m_h; // } // //利用成员函数判断两个立方体是否相等 // bool isSameByClass(Cube &c) // { // if (m_l == c.getL() && m_w == c.getW() && m_h == c.getH()) // { // return true; // } // return false; // } //private: // int m_h; // int m_l; // int m_w; //}; 利用全局函数判断两个立方体是否相等 //bool isSame(Cube c1, Cube c2) //{ // if (c1.getH() == c2.getH() && c1.getL() == c2.getL() && c1.getW ()== c2.getW()) // { // return true; // } // return false; //} //int main() //{ // Cube c1; // c1.setL (10); // c1.setH (10); // c1.setW(10); // cout << "c1的面积的为:" << c1.calculateS() << endl; // cout << "c1的体积为:" << c1.calculateV() << endl; // // Cube c2; // c2.setL(10); // c2.setH(10); // c2.setW(10); // // //利用全局函数判断 // bool ret = isSame(c1, c2); // if (ret) // { // cout << "c1和c2相等" << endl; // } // else // { // cout << "c1和c2不相等" << endl; // } // //利用成员函数判断 // bool ret2 = c1.isSameByClass(c2); // if (ret2) // { // cout << "c1和c2相等!" << endl; // } // else // { // cout << "c1和c2不相等!" << endl; // } // return 0; //} //练习案例2 #include"point.h" #include"Circle.h" //class Point //{ //public: // void setX(int x) // { // m_x = x; // } // int getX() // { // return m_x; // } // void setY(int y) // { // m_y = y; // } // int getY() // { // return m_y; // } //private: // int m_x; // int m_y; //}; //class Circle //{ //public: // void setR(int r) // { // m_r = r; // } // int getR() // { // return m_r; // } // void setCenter(Point center) // { // m_center = center; // } // Point getCenter()//在类中可以让另一个类中作为本来类的成员 // { // return m_center; // } //private: // int m_r; // Point m_center; //}; //void isIncircle(Circle& c, Point& p) //{ // int distance=(c.getCenter().getX() - p.getX())*(c.getCenter().getX() - p.getX()) + // (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());//(x-x1)^2+(y-y1)^2 // // int rDistance = c.getR() * c.getR();//r^2 // // if (distance == rDistance) // { // cout << "点在圆上" << endl; // } // else if (distance > rDistance) // { // cout << "点在圆外" << endl; // } // else // { // cout << "点在圆内" << endl; // } //} //int main() //{ // Circle c; // c.setR(10); // Point center; // center.setX(10); // center.setY(0); // c.setCenter(center); // // Point p; // p.setX(10); // p.setY(10); // isIncircle(c, p); // return 0; //}
//对象的初始化和清理 //构造函数和析构函数 //构造函数进行初始化的操作 //class Person //{ //public: // //1.构造函数 // //没有返回值 不用写void // //函数名与类名相同 // //构造函数可以有参数,可以发生重载 // //创建对象的时候 构造函数会自动调用,而且只调用一次 // Person() // { // cout << "Person构造函数的调用" << endl; // } // //2.析构函数 进行清理的操作 // //没有返回值 不写void // //函数名和类名相同 在名称前加~ // //析构函数不可以有参数的,不可以发生重载 // //对象在销毁前会自动调用析构函数,并且只有一次 // ~Person() // { // cout << "Person的析构函数调用" << endl; // } //}; // 构造和析构都是必须有的实现,如果自己不提供,编译器会提供一个空实现的构造和析构 //void test01() //{ // Person p;//在栈上的数据,test01执行完毕后,释放 //} //int main() //{ // //test01(); // Person p; // system("pause"); // return 0; //} //构造函数的分类及调用 //两种分类方式 //有参构造和无参构造 //普通构造和拷贝构造 //class Person //{ //public: // //构造函数 // Person() // { // cout << "Person的无参构造函数的调用" << endl; // } // Person(int a) // { // age = a; // cout << "Person的有参构造函数的调用" << endl; // } // //拷贝构造函数 // Person(const Person &p) // { // //将传入的人身上的所有属性,拷贝到我身上 // cout << "Person的拷贝构造函数的调用" << endl; // age = p.age; // } // ~Person() // { // //析构函数 // cout << "Person的析构函数的调用" << endl; // // } // int age ; //}; //void test01() //{ // //1.括号法 // //Person p1;//默认构造函数调用 // //Person p2(10);//有参构造函数 // //Person p3(p2);//拷贝构造函数 // 注意事项 // 调用默认构造函数的时候,不要加() // //cout << "p2的年龄:" << p2.age << endl; // //cout << "p3的年龄:" << p3.age << endl; // // //2.显示法 // //Person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象 // Person p1; // Person p2=Person(10);//有参构造 // Person p3 = Person(p2);//拷贝构造 // //注意事项 // // 不要利用拷贝构造函数初始化匿名对象 // //Person(p3);//会报重定义 // // // //3.隐式转换法 // Person p4 = 10;//有参构造 相当于 写了 Person p4=Person(10) // Person p5 = p4;//拷贝构造 // // //} //int main() //{ // test01(); // return 0; //} //拷贝构造函数的调用时机 //1.使用一个已经创建完毕的对象来初始化一个新对象 //2.值传递的方式给函数参数传值 //3.值方式返回局部对象 //class Person //{ //public: // Person()//默认构造函数 // { // cout << "Person默认构造函数的调用" << endl; // } // Person(int age)//有参构造函数 // { // cout << "Person有参构造函数调用" << endl; // m_age = age; // } // Person(const Person& p)//拷贝构造函数 // { // cout << "Person拷贝构造函数的调用" << endl; // m_age = p.m_age; // } // ~Person()//析构函数 // { // cout << "Person析构函数的调用" << endl; // // } // int m_age; //}; // 1.使用一个已经创建完毕的对象来初始化一个新对象 //void test01() //{ // Person p1(20);//有参 析构 // Person p2(p1);//拷贝 析构 // cout << "p2的年龄:" <<p2.m_age<< endl;//p2.age=20 //} 2.值传递的方式给函数参数传值 //void dowork(Person p) //{ // //} //void test02() //{ // Person p;//调用默认构造函数+析构 // dowork(p);//调用拷贝构造函数+析构 //} 3.值方式返回局部对象 //Person dowork2() //{ // Person p1;//调用默认构造函数+析构 // cout << (int*)&p1 << endl; // return p1; //} //void test03() //{ // Person p = dowork2();//调用拷贝构造函数+析构 // cout << (int*)&p<< endl; //} //int main() //{ // //test01(); // //test02(); // test03(); // return 0; //} //构造函数调用规则 // 创建一个类 c++会给每个类添加3个类-默认构造(空实现)析构函数(空实现) 拷贝构造(值拷贝) //如果用户定义有参构造函数 c++不再提供默认无参构造 //如果我们写了有参构造函数,编译器就不再提供默认构造函数,依然提供拷贝函数 //如果我们写了拷贝构造函数,编译器就不再提供其他的函数了 //class Person //{ //public: // //Person() // //{ // // cout << "Person的默认构造函数" << endl; // //} // Person(int age) // { // cout << "Person的有参构造函数" << endl; // } // Person(const Person& p) // { // cout << "Person的拷贝构造函数" << endl; // // } // ~Person() // { // cout << "Person的析构函数调用" << endl; // // } // int m_age; //}; //void test01() //{ // Person p; // p.m_age =18; // Person p2(p); // cout << "p2的年龄为:" << p.m_age << endl; //} //void test02() //{ // Person p(28); // Person p2(p); // //} //int main() //{ // //test01(); // test02(); // return 0; //} //深拷贝与浅拷贝 //浅拷贝:简单的赋值拷贝操作 //深拷贝:在堆区重新申请空间,进行拷贝操作 //class Person //{ //public: // Person() // { // cout << "Person的默认构造函数" << endl; // } // // Person(int age,int height) // { // cout << "Person的有参构造函数" << endl; // m_age = age; // m_height=new int(height); // // } // // //自己实现一个拷贝构造函数 解决浅拷贝带来的问题 // Person(const Person& p) // { // cout << "Person的拷贝构造函数" << endl; // m_age = p.m_age; // //深拷贝操作 // m_height = new int(*p.m_height); // } // // ~Person() // { // //析构代码将堆区开辟数据做释放操作 // cout << "Person的析构函数调用" << endl; // if (m_height != NULL) // { // delete m_height; // m_height = NULL; // } // } // int m_age; // int* m_height; //}; //void test01() //{ // Person p1(18,180); // Person p2(p1);//如果利用编译器提供的拷贝构造函数,会做浅拷贝操作(容易重复释放) // cout << "p1的年龄为:" << p1.m_age << "身高为:" << *p1.m_height << endl; // cout << "p2的年龄为:" << p2.m_age << "身高为:"<<*p2.m_height << endl; // //} //int main() //{ // test01(); // // return 0; //} //初始化列表 //class Person //{ //public: // //传统初始化操作 // //Person(int a, int b, int c) // //{ // // m_a = a; // // m_b = b; // // m_c = c; // //} // //初始化列表初始化属性 // Person(int a,int b,int c) : m_a(a), m_b(b), m_c(c) // { // // } // int m_a; // int m_b; // int m_c; //}; //void test01() //{ // //Person p(10, 20, 30); // Person p(30,20,10); // cout << "m_a=" << p.m_a << endl; // cout << "m_b=" << p.m_b << endl; // cout << "m_c=" << p.m_c << endl; //} //int main() //{ // test01(); // return 0; //} //类对象作为类的成员 //class Phone //{ //public: // Phone(string pname) // { // m_Pname = pname; // cout << "Phone构造函数调用" << endl; // // } // // ~Phone() // { // cout << "Phone的析构函数调用" << endl; // } // string m_Pname; //}; //class Person //{ //public: // //Phone m_phone=pname; // Person(string name, string pname):m_name(name),m_phone(pname) // { // cout << "Person构造函数调用" << endl; // } // ~Person() // { // cout << "Person的析构函数调用" << endl; // } // string m_name; // Phone m_phone; //}; 当其他类作为本类成员,构造时先构造其他类对象,再构造自身,析构的顺序与构造相反 //void test01() //{ // Person p("张三", "iphone"); // cout << p.m_name << "用" << p.m_phone.m_Pname << endl; // //} //int main() //{ // test01(); // return 0; //} //静态成员 //静态成员变量 // 所有对象共享同一份数据 // 在编译阶段分配内存 // 类内声明,类外初始化 //class Person //{ //public: // // 所有对象共享同一份数据 // // 在编译阶段分配内存 类内声明,类外初始化 // static int m_a; // // //静态成员变量也是有访问权限的 //private: // static int m_b; // //}; // int Person::m_a=100; // int Person::m_b = 200; // // //void test01() //{ // Person p; // cout << p.m_a << endl; // Person p2; // p2.m_a = 200; // cout << p.m_a << endl; // cout << p2.m_a << endl; // //} //void test02() //{ // //静态成员变量 不属于某个对象上,所有对象都共享同一份数据 // //因此静态成员变量有两种访问方式 // // // //1.通过对象进行访问 // Person p; // cout << p.m_a << endl; // //2.通过类名进行访问 // cout << Person::m_a << endl; // //cout << Person::m_b << endl;//类外访问不到私有静态成员变量 // // //} //int main() //{ // test02(); // return 0; //} // //静态成员函数 //所有对象共享一个函数 //class Person //{ //public: // //静态成员函数只能访问静态的成员变量 // static void func() // { // m_a = 100;//静态成员函数可以访问静态成员变量 // m_b = 200;//静态成员函数不可以访问非静态成员变量 // cout << "static void func调用" << endl; // } // static int m_a;//静态成员变量 // int m_b ;//非静态成员变量 // 静态成员函数的访问权限 //private: // static void func2() // { // cout << "static void func2调用" << endl; // } //}; 有两种的访问方式 //void test01() //{ // //1.通过对象进行访问 // Person p; // p.func(); // //2.通过类名进行访问 // Person::func(); // Person::func2();//类外访问不到私有的静态成员函数 //} //int main() //{ // test01(); // return 0; //} //C++对象模型和this指针 //成员变量和成员函数分开存储 //class Person //{ //public: // int m_a;//非静态成员变量 属于类的对象上 // // static int m_b;//静态成员变量 属于类的对象上 // // void func() // { // //非静态成员函数 不属于类的对象上 // } // static void func2() // { // //静态成员函数 不属于类的对象上 // } //}; //void test01() //{ // Person p; // //空对象占用的内存空间为:1 // //c++会给每个空对象分配一个内存空间 为了区分空对象占内存的位置 // //每个空对象也有一个独一无二的内存地址 // cout << "sizeof(p)=="<<sizeof(p) << endl; //} //void test02() //{ // Person p; // cout << "sizeof(p)==" << sizeof(p) << endl; // //} //int main() //{ // test02(); // return 0; //} //C++提供特殊的对象指针,this指针,指向被调用的成员函数所属的对象 //class Person //{ //public: // Person(int age) // { // //this指针指向 被调用的成员函数 所属的对象 // //当形参和成员变量重名时,可用this指针区分 // this->age = age;//1.解决名称冲突 // } // Person& PersonAddage(Person& p) // { // this->age += p.age; // //this指向p2的指针,而*this指向的是p2对象本身 // return *this; // } // int age; //}; // //void test01() //{ // Person p1(18); // cout << "年龄:" << p1.age << endl; //} 2.返回对象本身用*this //void test02() //{ // Person p1(10); // Person p2(10); // //链式编程 // p2.PersonAddage(p1).PersonAddage(p1).PersonAddage(p1); // cout << "年龄:" << p2.age << endl; // //} //int main() //{ // test01(); // test02(); // // return 0; //} //空指针访问成员函数 //class Person //{ //public: // void showClassname() // { // cout << "this is Person class" << endl; // } // void showPersonage() // { // //报错原因是因为传入的指针为NULL // if (this==NULL) // { // return; // } // cout << "age=" << m_age << endl; // } // int m_age; //}; //void test01() //{ // Person* p = NULL; // p->showClassname(); // p->showPersonage(); //} //int main() //{ // test01(); // return 0; //} //const修饰成员函数 //常函数 //class Person //{ //public: // //this指针的本质 是指针常量 指针的指向是不可修改的 // //const Person* const this; // //成员函数后面加const 修饰的是this指向,让指针指向的值也不可以修改 // void showPerson()const//常函数 // { // //this->m_a = 100; // this->m_b = 100; // //this->NULL;//this指针不可以修改指针的指向 // } // // void func() // { // // } // int m_a; // mutable int m_b;//特殊变量 即使在常函数中 ,也可以修改这个值 加关键字mutable //}; //void test01() //{ // Person p; // p.showPerson(); //} //void test02() //{ // const Person p;//在对象前加const,变为常对象 // // //p.m_a = 100;//不可修改 // p.m_b = 100;//m_b是特殊值,在常对象下也可以修改 // // //常对象只能调用常对象 // p.showPerson(); // p.func();//不可调用 //} //int main() //{ // test01(); // return 0; //} //友元 //全局函数做友元 //class Building //{ // friend void goodgay(Building* building);//goodgay是Building好朋友 可以访问building中的私有成员 // //public: // Building() // { // m_sittingroom = "客厅"; // m_bedroom = "卧室"; // } //public: // string m_sittingroom; //private: // string m_bedroom; //}; 全局函数 //void goodgay(Building *building) //{ // cout << "gay正在访问:" << building->m_sittingroom << endl; // cout << "gay正在访问:" << building->m_bedroom << endl; // //} //void test01() //{ // Building building; // goodgay(&building); //} //int main() //{ // test01(); // return 0; //} // //类做友元 //class Building; //class Goodgay //{ //public: // Goodgay(); // void visit();//参观函数 访问Building中的属性 //private: // Building *building; //}; //class Building //{ // //Goodgay是本类的好朋友 可以访问本类的私有成员 // friend class Goodgay; //public: // Building(); //public: // string m_sittingroom; //private: // string m_bedroom; //}; 类外写成员函数 //Building::Building() //{ // m_sittingroom = "客厅"; // m_bedroom = "卧室"; //} //Goodgay::Goodgay() //{ // //创建建筑物对象 // building = new Building; //} // void Goodgay::visit() // { // cout << "gay正在访问:" << building->m_sittingroom << endl; // cout << "gay正在访问:" << building->m_bedroom << endl; // } // //void test01() //{ // Goodgay gg; // gg.visit(); //} //int main() //{ // test01(); // return 0; //} //成员函数做友元 //class Building; //class Goodgay //{ //public: // Goodgay(); // void visit();//让visit可以访问Building中私有成员 // void visit2();//让visit2不可以访问Building中私有成员 // // Building* building; //}; // //class Building //{ // //告诉编译器 Goodgay类下的visit成员函数作为本类的好朋友,可以访问私有成员 // friend void Goodgay::visit(); // friend void Goodgay::visit2(); // //public: // Building(); //public: // string m_sittingroom; //private: // string m_bedroom; //}; 类外实现成员函数 //Building::Building() //{ // m_sittingroom = "客厅" ; // m_bedroom = "卧室" ; //} // //Goodgay::Goodgay() //{ // building = new Building; //} // //void Goodgay::visit() //{ // cout << "visit函数正在访问:" << building->m_sittingroom << endl; // cout << "visit函数正在访问:" << building->m_bedroom << endl; // //} //void Goodgay::visit2() //{ // cout << "visit2函数正在访问:" << building->m_sittingroom << endl; // cout << "visit2函数正在访问:" << building->m_bedroom << endl; // // //} //void test01() //{ // Goodgay gg; // gg.visit(); // gg.visit2(); //} //int main() //{ // test01(); // return 0; //} //c++加号运算符重载 //1.成员函数重载加号 //class Person //{ //public: // //成员函数 // Person operator+(Person &p) // { // Person temp; // temp.m_a = this->m_a + p.m_a; // temp.m_b = this->m_b + p.m_b; // return temp; // } // int m_a; // int m_b; //}; //全局函数重载+号 //Person operator+(Person& p1, Person& p2) //{ // Person temp; // temp.m_a = p1.m_a + p2.m_a; // temp.m_b = p1.m_b + p2.m_b; // return temp; //} // //函数重载的版本 //Person operator+(Person& p1, int num) //{ // Person temp; // temp.m_a = p1.m_a + num; // temp.m_b = p1.m_b + num; // return temp; //} //void test01() //{ // Person p1; // p1.m_a = 10; // p1.m_b = 10; // Person p2; // p2.m_a = 10; // p2.m_b = 10; // // Person p3 = p1 + p2; // Person p4 = p1 + 100; // cout << "p3.m_a=" << p3.m_a << endl; // cout << "p3.m_b=" << p3.m_b << endl; // cout << "p4.m_a=" << p4.m_a << endl; // cout << "p4.m_b=" << p4.m_b << endl; //} //int main() //{ // test01(); // return 0; //} //左移运算符重载 //class Person //{ // friend ostream& operator<<(ostream& out, Person& p); //public: // Person(int a, int b) // { // this->m_a = a; // this->m_b = b; // } //private: // int m_a; // int m_b; //}; 利用全局函数重载左移运算符 //ostream& operator<<(ostream &out, Person& p)//本质 operator<<(cout,p) 简化cout<<p //{ // out << "m_a=" << p.m_a << " m_b=" << p.m_b; // return out; //} //void test01() //{ // Person p(10,20); // // cout << p << endl; //} //int main() //{ // test01(); // return 0; //} //递增运算符重载 //自定义的整型变量 //class myInteger //{ // friend ostream& operator<<(ostream& cout, myInteger myint); //public: // myInteger() // { // m_num = 0; // } // //重载前置++运算符 // myInteger& operator++() // { // m_num++; // return *this;//返回引用是为了一直对一个数据进行递增操作 // } // //重载后置++运算符 // myInteger& operator++(int)//int代表占位参数,可以用于区分前置和后置递增 // { // //先记录当时结果 // myInteger temp = *this; // //后递增 // m_num++; // //最后记录结果做返回 // return temp; // } //private: // int m_num; //}; 重载左移运算符 //ostream& operator<<(ostream& cout, myInteger myint) //{ // cout << myint.m_num << endl; // return cout; //} //void test01() //{ // myInteger myint; // cout << ++myint << endl; // cout << myint << endl; // //} //void test02() //{ // myInteger myint2; // cout << myint2++ << endl; // cout << myint2 << endl; // //} //int main() //{ // test01(); // test02(); // return 0; //} //赋值运算符重载 //class Person //{ //public: // Person(int age) // { // m_age = new int(age); // } // // ~Person() // { // if (m_age != NULL) // { // delete m_age; // m_age = NULL; // } // } // // //重载赋值运算符 // Person& operator=(Person& p) // { // //先判断是否有属性在堆区,如果有先释放再深拷贝 // if (m_age != NULL) // { // delete m_age; // m_age = NULL; // } // //深拷贝 // m_age = new int(*p.m_age); // // //返回对象本身 // return *this; // } // int *m_age; //}; //void test01() //{ // Person p1(18); // // Person p2(20); // // Person p3(30); // // p3 = p2 = p1; // // cout << "p1年龄:" << *p1.m_age << endl; // cout << "p2年龄:" << *p2.m_age << endl; // cout << "p3年龄:" << *p3.m_age << endl; // // //} //int main() //{ // test01(); // return 0; //} //关系运算符重载 //class Person //{ //public: // Person(string name,int age) // { // m_name = name; // m_age = age; // } // //重载关系运算符 // // bool operator==(Person& p) // { // if (this->m_name == p.m_name && this->m_age == p.m_age) // { // return true; // } // else // { // return false; // } // } // bool operator!=(Person& p) // { // if (this->m_name == p.m_name && this->m_age == p.m_age) // { // return false; // } // else // { // return true; // } // } // string m_name; // int m_age; //}; // //void test01() //{ // Person p1("Tom",18); // // Person p2("Tom",18); // // if (p1 == p2) // { // cout << "p1和p2相等" << endl; // } // else // { // cout << "p1和p2不相等" << endl; // // } // // if (p1 != p2) // { // cout << "p1和p2不相等" << endl; // } // else // { // cout << "p1和p2相等" << endl; // // } //} //int main() //{ // test01(); // return 0; //} //函数调用运算符重载 //打印输出类 //class MyPrint //{ //public: // //重载函数调用运算符 // void operator()(string test) // { // cout << test << endl; // } //}; //void MyPrint02(string test) //{ // cout << test << endl; //} //void test01() //{ // MyPrint myprint; // myprint("Hello world!");//由于类似函数调用 称为仿函数 // MyPrint02("Hello Newcoder"); //} 仿函数非常灵活 没有固定写法 加法类 //class MyAdd //{ //public: // int operator()(int num1, int num2) // { // return num1 + num2; // } //}; //void test02() //{ // MyAdd myadd; // int ret=myadd(100, 100); // cout << "ret=" << ret << endl; // //匿名函数对象 // cout << MyAdd()(100, 200) << endl; //} //int main() //{ // test01(); // test02(); // return 0; //}
//继承 //普通实现页面 //JAVA页面 //class Java //{ //public: // void header() // { // cout << "首页、公开课、登录、注册....(公共头部)" << endl; // } // void footer() // { // cout << "帮助中心、交流合作、站内地图......(公共底部)" << endl; // } // void left() // { // cout << "Java、Python、C++、.....(公共分类服务)" << endl; // } // void content() // { // cout << "Java学科视频" << endl; // } // //}; // // Python页面 //class Python //{ //public: // void header() // { // cout << "首页、公开课、登录、注册....(公共头部)" << endl; // } // void footer() // { // cout << "帮助中心、交流合作、站内地图......(公共底部)" << endl; // } // void left() // { // cout << "Java、Python、C++、.....(公共分类服务)" << endl; // } // void content() // { // cout << "Python学科视频" << endl; // } // //}; C++页面 // //class CPP //{ //public: // void header() // { // cout << "首页、公开课、登录、注册....(公共头部)" << endl; // } // void footer() // { // cout << "帮助中心、交流合作、站内地图......(公共底部)" << endl; // } // void left() // { // cout << "Java、Python、C++、.....(公共分类服务)" << endl; // } // void content() // { // cout << "C++学科视频" << endl; // } // //}; //继承实现页面--减少重复代码 //class BasePage //{ //public: // void header() // { // cout << "首页、公开课、登录、注册....(公共头部)" << endl; // } // void footer() // { // cout << "帮助中心、交流合作、站内地图......(公共底部)" << endl; // } // void left() // { // cout << "Java、Python、C++、.....(公共分类服务)" << endl; // } // //}; //Java页面 //继承语法:class 子类:继承方式 父类 //子类 也称为派生类 //父类 也称为基类 //class Java :public BasePage //{ //public: // void content() // { // cout << "Java学科视频" << endl; // } //}; Python 页面 //class Python :public BasePage //{ //public: // void content() // { // cout << "Python学科视频" << endl; // } //}; C++页面 //class CPP :public BasePage //{ //public: // void content() // { // cout << "C++学科视频" << endl; // } //}; //void test01() //{ // cout << "Java下载视频页面如下" << endl; // Java ja; // ja.header(); // ja.footer(); // ja.left(); // ja.content(); // cout << "------------------" << endl; // // cout << "Python下载视频页面如下" << endl; // Python py; // py.header(); // py.footer(); // py.left(); // py.content(); // cout << "------------------" << endl; // // cout <<"C++下载视频页面如下" << endl; // CPP cpp; // cpp.header(); // cpp.footer(); // cpp.left(); // cpp.content(); //} //int main() //{ // test01(); // return 0; //} //继承方式 //公共继承 //class Base1 //{ //public: // int m_a; //protected: // int m_b; //private: // int m_c; //}; // //class Son1 : public Base1 //{ //public: // void func() // { // m_a = 10;//父类中的公共权限成员到子类中依然是公共权限 // m_b = 10;//父类中的保护权限成员到子类中依然是保护权限 // //m_c = 10;//父类中的私有权限成员到子类中无法访问 // } //}; //void test01() //{ // Son1 s1; // s1.m_a = 100; // s1.m_b = 100;//到Son1中m_b是保护权限 类外访问不到 // //} //int main() //{ // test01(); // return 0; //} 保护继承 //class Base2 //{ //public: // int m_a; //protected: // int m_b; //private: // int m_c; //}; // //class Son2 : protected Base2 //{ //public: // void func() // { // m_a = 10;//父类中的公共权限成员到子类中变为保护权限 // m_b = 10;//父类中的保护权限成员到子类中变为保护权限 // //m_c = 10;//父类中的私有权限成员到子类中无法访问 // } //}; //void test02() //{ // Son2 s1; // s1.m_a = 100;//到Son2中m_a变为保护权限 类外访问不到 // s1.m_b = 100;//到Son2中m_b是保护权限 类外访问不到 //} //int main() //{ // test02(); // return 0; //} 私有继承 //class Base3 //{ //public: // int m_a; //protected: // int m_b; //private: // int m_c; //}; // //class Son3 :private Base3 //{ //public: // void func() // { // m_a = 10;//父类中的公共权限成员到子类中变为私有权限 // m_b = 10;//父类中的保护权限成员到子类中变为私有权限 // m_c = 10;//父类中的私有权限成员到子类中无法访问 // } //}; //class GrandSon3 :private Son3 //{ //public: // void func() // { // m_a = 100;//到了Son3中m_a变为私有,即使是子类也访问不到 // m_b = 100;//到了Son3中m_b变为私有,即使是子类也访问不到 // } // //} //void test03() //{ // Son3 s1; // s1.m_a = 100;//到Son2中m_a变为私有权限 类外访问不到 // s1.m_b = 100;//到Son2中m_b是私有权限 类外访问不到 //} //int main() //{ // test03(); // return 0; //} //继承中的对象模型 //class Base //{ //public: // int m_a; //protected: // int m_b; //private: // int m_c; //}; 公共继承 //class Son :public Base //{ //public: // int m_d; //}; 利用开发人员命令工具查看对象模型 跳转盘符 E: 跳转文件路径 cd 具体路径下 查看命令 c1/d1 reportSingleClassLatout类名 文件名 //void test01() //{ // //父类中所有非静态成员属性都会被子类继承下去 // //父类中私有成员属性,是被编译器给隐藏了,因此访问不到,但被继承了 // cout << "sizeof Son=" << sizeof(Son) << endl;//16 //} //int main() //{ // test01(); // return 0; //} //继承中构造和析构顺序 //class Base //{ //public: // Base() // { // cout << "Base构造函数" << endl; // } // ~Base() // { // cout << "Base析构函数" << endl; // } //}; //class Son :public Base //{ //public: // Son() // { // cout << "Son构造函数" << endl; // } // ~Son() // { // cout << "Son析构函数" << endl; // } //}; //void test01() //{ // //Base b; // Son s;//继承中的构造和析构顺序 先构造父类 再构造子类 析构的顺序与构造相反 // //Base构造函数 Son构造函数 Son析构函数 Base析构函数 //} //int main() //{ // test01(); // return 0; //} //继承中同名成员的处理方式 //class Base //{ //public: // Base() // { // m_a = 100; // } // void func() // { // cout << "Base-func()函数调用" << endl; // } // void func(int a) // { // cout << "Son-func(int a)函数调用" << endl; // } // int m_a; //}; //class Son :public Base //{ //public: // Son() // { // m_a = 200; // } // void func() // { // cout << "Son-func()函数调用" << endl; // } // //}; //void test01() //{ // Son s; // cout << "Son下的m_a=" << s.m_a << endl; // //如果通过子类对象 访问到父类中的同名成员,需要加个作用域 // cout << "Base下的m_a=" << s.Base::m_a << endl; //} //void test02() //{ // Son s; // s.func();//直接调用 调用的是子类中的同名成员 // //如何调用父类中的同名成员 // s.Base::func(); // // //如果子类中出现和父类同名的成员函数 子类的同名成员会隐藏掉父类中所有同名成员函数 // //如果想访问到父类中被隐藏的成员函数,需要加作用域 // s.Base::func(100); //} //int main() //{ // test01(); // test02(); // return 0; //} //继承同名静态成员处理方式 //class Base //{ //public: // static int m_a; // static void func() // { // cout << "Base-static void func()" << endl; // } // static void func(int a) // { // cout << "Base-static void func(int a)" << endl; // } //}; //int Base::m_a = 100; //class Son:public Base //{ //public: // static int m_a; // //}; //int Son::m_a = 200; 同名静态成员属性 //void test01() //{ // //1.通过对象来访问 // cout << "通过对象访问" << endl; // Son s; // cout << "Son下的m_a=" << s.m_a << endl; // cout << "Base下的m_a=" << s.Base::m_a << endl; // //2.通过类名来访问 // cout << "通过类名访问" << endl; // cout << "Son下的m_a=" << Son::m_a << endl; // //第一个::代表通过类名方式访问 第二个::代表访问父类作用域下 // cout << "Base下的m_a=" << Son::Base::m_a << endl; //} //void test02() //{ // Son s; // //1.通过对象访问 // s.func(); // s.func(100); // s.Base::func(); // s.Base::func(100); // //2.通过类名方式访问 // Son::func(); // Son::func(100); // Son::Base::func(); // Son::Base::func(100); //} //int main() //{ // test02(); // return 0; //} //多继承语法 //class Base1 //{ //public: // Base1() // { // m_a = 100; // } // int m_a; //}; //class Base2 //{ //public: // Base2() // { // m_a = 200; // } // int m_a; //}; 子类 需要继承Base1和Base2 语法:class子类:继承方式 父类1,继承方式 父类2... //class Son :public Base1, public Base2 //{ //public: // Son() // { // m_c = 300; // m_d = 400; // } // int m_c; // int m_d; //}; //void test01() //{ // Son s; // cout << "size of s=" << sizeof(s) << endl; // cout << "Base1下的m_a=" << s.Base1::m_a << endl; // cout << "Base2下的m_a=" << s.Base2::m_a << endl; // //} //int main() //{ // test01(); // return 0; //} //菱形继承 //动物类 //class Animal //{ //public: // int m_age; //}; 利用虚继承 解决菱形继承的问题 继承之前 加上关键字 virtual变为虚继承 Animal类成为 虚基类 羊类 //class Sheep:public virtual Animal{}; // 驼类 //class cmcel:public virtual Animal{}; // 羊驼类 //class Shcel :public Sheep, public cmcel{}; //void test01() //{ // Shcel st; // st.Sheep::m_age = 18; // st.cmcel::m_age = 28; // //当菱形继承,两个父类拥有相同数据 需要加以作用域区分 // cout << "st.Sheep::m_age =" << st.Sheep::m_age << endl; // cout << "st.cmcel::m_age =" << st.cmcel::m_age << endl; // cout << "m_age =" << st.m_age << endl; //} //int main() //{ // test01(); // return 0; //} //多态 //静态多态 // 函数重载和运算符重载(编译阶段确定函数地址) //动态多态:1.有继承关系 2.子类重写父类的虚函数 //派生类和虚函数实现运行时多态(运行阶段确定函数地址) //动物类 //class Animal //{ //public: // virtual void speak() // { // cout << "动物在说话" << endl; // } //}; // 猫类 //class cat :public Animal //{ //public: // //重写 函数返回值类型 函数名 参数列表 完全相同 // void speak() // { // cout << "小猫在说话" << endl; // } //}; // 狗类 //class dog :public Animal //{ //public: // void speak() // { // cout << "狗在说话" << endl; // } //}; 执行说话的函数 地址早绑定 在编译阶段确定函数地址 如果想执行让猫说话,那么这个函数地址就不能提前绑定,需要在运行阶段进行绑定,地址晚绑定 动态多态: //void dospeak(Animal& animal)//Animal & animal=cat; //{ // //父类的指针或引用 执行子类对象 // animal.speak(); //} //void test01() //{ // cat c; // dospeak(c); //} //void test02() //{ // dog d; // dospeak(d); //} //int main() //{ // test01(); // test02(); // return 0; //} //多态案例-计算器类 //利用多态实现计算器 //多态的好处 1.结构清晰 2.可读性强 3.对于扩展和维护性高 //class abstractcalculator //{ //public: // virtual int getresult() // { // return 0; // } // int m_num1; // int m_num2; //}; 加法计算器类 //class addcalculator : public abstractcalculator //{ //public: // int getresult() // { // return m_num1 + m_num2; // } //}; 减法计算器类 //class subcalculator :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 - m_num2; // } //}; 乘法计算器类 //class mulcalculator : public abstractcalculator //{ //public: // int getresult() // { // return m_num1 * m_num2; // } //}; 除法计算器类 //class divcalcultor :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 / m_num2; // } //}; //void test01() //{ // //多态使用条件 // //父类指针或者引用指向子类对象 // //加法运算 // abstractcalculator* abc = new addcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "+" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // //减法运算 // abc = new subcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "-" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // // //乘法运算 // abc = new mulcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "*" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // // //除法运算 // abc = new divcalcultor; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "/" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; //} //int main() //{ // test01(); // return 0; //} //纯虚函数和抽象类 //纯虚函数语法 //virtual 返回值类型 函数名 (参数列表)=0; //当类中有了纯虚函数 这个类也称为抽象类 //class Base //{ //public: // virtual void func() = 0;//纯虚函数 // //只要有一个纯虚函数,这个类成为抽象类 //}; //class Son:public Base //{ //public: // virtual void func() // { // cout << "func的调用" << endl; // }; //}; //void test01() //{ 1.抽象类无法实例化对象 // /*Base b; // new Base;*/ 2.抽象类的子类必须重写父类中的纯虚函数,否则也属于抽象类 // //Son s;//子类必须重写父类中的纯虚函数,否则无法实例化对象 // Base* base = new Son; // base->func(); //} //int main() //{ // test01(); // return 0; //} //多态案例2-制作饮品 //class abstractdrinking //{ //public: // //煮水 // virtual void boil() = 0; // //冲泡 // virtual void brew() = 0; // //倒杯 // virtual void pourIncup() = 0; // //加入辅料 // virtual void putSomething() = 0; // //制作饮品 // virtual void makedrink() // { // boil(); // brew(); // pourIncup(); // putSomething(); // } //}; // 制作咖啡 //class coffee :public abstractdrinking //{ //public: // //煮水 // virtual void boil() // { // cout << "煮农夫山泉" << endl; // } // // //冲泡 // virtual void brew() // { // cout << "冲泡咖啡" << endl; // } // //倒入杯中 // virtual void pourIncup() // { // cout << "倒入杯中" << endl; // } // //加入辅料 // virtual void putSomething() // { // cout << "加入糖奶" << endl; // } //}; // //class tea :public abstractdrinking //{ //public: // //煮水 // virtual void boil() // { // cout << "煮矿泉水" << endl; // } // // //冲泡 // virtual void brew() // { // cout << "冲泡茶叶" << endl; // } // //倒入杯中 // virtual void pourIncup() // { // cout << "倒入茶杯中" << endl; // } // //加入辅料 // virtual void putSomething() // { // cout << "加入茶叶" << endl; // } //}; 制作函数 //void dowork(abstractdrinking * abs) //{ // abs->makedrink(); // delete abs;//手动释放 //} //void test01() //{ // //制作咖啡 // dowork(new coffee); // cout << "-----------------------------" << endl; // //制作茶 // dowork(new tea); //} //int main() //{ // test01(); // return 0; //} //虚析构和纯虚析构 //class Animal //{ //public: // Animal() // { // cout << "Animal的构造函数调用" << endl; // } // //利用虚析构可以解决 父类指针释放子类对象时不干净的问题 // //virtual ~Animal() // //{ // // cout << "Animal的虚析构函数的调用" << endl; // //} // // // //纯虚析构(需要声明也需要实现) // //有了纯虚析构之后 这个类也属于抽象类 无法实例化对象 // virtual ~Animal() = 0; // //纯虚函数 // virtual void speak() = 0; //}; //Animal::~Animal() //{ // cout << "Animal纯虚析构函数调用" << endl; //} // //class Cat :public Animal //{ //public: // Cat(string name) // { // m_name = new string(name); // } // virtual void speak() // { // cout << *m_name << "小猫在说话" << endl; // } // ~Cat() // { // if (m_name != NULL) // { // cout << "Cat的析构函数调用" << endl; // delete m_name; // m_name = NULL; // } // } // string* m_name; //}; // //void test01() //{ // Animal* animal = new Cat("Tom"); // animal->speak(); // //父类的指针在析构的时候 不会调用子类的析构函数 // //导致子类如果有堆区数据,会出现内存的泄露情况 // delete animal; //} //int main() //{ // test01(); // return 0; //} //多态案例3-电脑组装 //抽象的CPU类 //class CPU //{ //public: // //抽象的计算函数 // virtual void calculate() = 0; //}; 抽象的显卡类 //class VideoCard //{ //public: // //抽象显示函数 // virtual void display() = 0; //}; 抽象内存条类 //class Memory //{ //public: // //抽象存储函数 // virtual void storage() = 0; //}; 抽象电脑类 //class Computer //{ // //构造函数传入三个零件的指针 //public: // Computer(CPU* cpu, VideoCard* vc, Memory* mem) // { // m_cpu = cpu; // m_vc = vc; // m_mem = mem; // } // //提供工作函数 // void work() // { // //让零件工作起来,调用接口 // m_cpu->calculate(); // m_vc->display(); // m_mem->storage(); // } // ~Computer() // { // if (m_cpu != NULL) // { // delete m_cpu; // m_cpu = NULL; // } // if (m_vc != NULL) // { // delete m_vc; // m_vc = NULL; // } // if (m_mem != NULL) // { // delete m_mem; // m_mem = NULL; // } // } //private: // CPU* m_cpu;//CPU零件指针 // VideoCard* m_vc;//显卡零件指针 // Memory* m_mem;//内存条零件指针 //}; //class InterCpu :public CPU //{ //public: // virtual void calculate() // { // cout << "Intel的CPU开始工作" << endl; // } // //}; //class InterVideoCard :public VideoCard //{ //public: // virtual void display() // { // cout << "Intel的显卡开始工作" << endl; // } // //}; //class InteMemory : public Memory //{ //public: // virtual void storage() // { // cout << "Intel的内存条开始工作" << endl; // } // //}; //class Lenovocpu :public CPU //{ //public: // virtual void calculate() // { // cout << "Lenovo的CPU开始工作" << endl; // } //}; //class LenovoVideoCard : public VideoCard //{ //public: // virtual void display() // { // cout << "Lenovo的显卡开始工作" << endl; // } // //}; //class LenovoMemory :public Memory //{ //public: // virtual void storage() // { // cout << "Lenovo的内存条开始工作" << endl; // } //}; //void test01() //{ // cout << "第一台电脑开始工作" << endl; // //第一台电脑的零件 // CPU* intelCpu = new InterCpu; // VideoCard* intelCard = new InterVideoCard; // Memory* intelMem = new InteMemory; // // //创建第一台电脑 // Computer* computer1 = new Computer(intelCpu, intelCard, intelMem); // computer1->work(); // delete computer1; // // cout << "-----------------------" << endl; // cout << "第二台电脑开始工作" << endl; // //创建第二台电脑 // Computer* computer2 = new Computer(new Lenovocpu, new LenovoVideoCard,new LenovoMemory); // computer2->work(); // delete computer2; // // cout << "-----------------------" << endl; // cout << "第三台电脑开始工作" << endl; // //创建第三台电脑 // Computer* computer3= new Computer(new InterCpu, new LenovoVideoCard, new LenovoMemory); // computer3->work(); // delete computer3; //} // //int main() //{ // test01(); // return 0; //} //文件操作 //C++中对文件操作包含头文件<fstream> // //1.文本文件 -以AskⅡ码形式存储在计算机中 // //2.二进制文件-以二进制形式存储在计算机中 // //操作文件三大类 // 包含头文件<fstream> // //1.ofsream:写操作 //打开文件 ofs.open("文件路径",打开方式); //写数据 ofs<<"写入的数据"; //关闭文件 ofs.close(); //文件打开方式 //ios::in-为读文件而打开文件 ios::out-为写文件而打开文件 ios::ate-初始位置:文件尾 //ios::app-追加方式写文件 ios::trunc-如果文件存在先删除,再创建 ios::binary-二进制方式 //#include<fstream> //void test01() //{ // //创建流对象 // ofstream ofs; // //指定打开方式 // ofs.open("test.txt", ios::out); // //写内容 // ofs << "姓名:张三" << endl; // ofs << "性别:男" << endl; // ofs << "年龄:18" << endl; // //关闭文件 // ofs.close(); // //} //int main() //{ // test01(); // return 0; //} //读文件 //1.头文件 //#include<fstream> //void test01() //{ // //2.创建流对象 // ifstream ifs; // //3.打开文件并判断文件是否打开成功 // ifs.open("test.txt", ios::in); // // if (!ifs.is_open()) // { // cout << "文件打开失败" << endl; // return; // } // //4.读数据 // //第一种 // /*char buf[1024] = { 0 }; // while (ifs>>buf) // { // cout << buf << endl; // }*/ // //第二种 // //char buf[1024] = { 0 }; // //while (ifs.getline(buf, sizeof(buf))) // //{ // // cout << buf << endl; // //} // //第三种 // /*string buf; // while (getline(ifs, buf)) // { // cout << buf << endl; // }*/ // //第四种 // char c; // while ((c = ifs.get() )!= EOF) // { // cout << c; // } // //5.关闭文件 // ifs.close(); // //} //int main() //{ // test01(); // return 0; //}
//二进制写文件 #include<fstream> //class Person //{ //public: // char m_name[64]; // int m_age; //}; // //void test01() //{ // //创建流对象 // ofstream ofs; // //ofstream ofs("person.txt",ios::out|ios::binary); // //打开文件 // ofs.open("person.txt", ios::out | ios::binary); // //写文件 // Person p = { "张三", 18 }; // // ofs.write((const char*)&p, sizeof(Person)); // //关闭文件 // ofs.close(); //} //int main() //{ // test01(); // return 0; //} // //二进制读文件 //class Person //{ //public: // char m_name[64]; // int m_age; //}; //void test01() //{ 创建流对象 // ifstream ifs; 打开文件 判断文件是否打开成功 // ifs.open("person.txt", ios::in | ios::binary); // // if (!ifs.is_open()) // { // cout << "文件打开失败" << endl; // return; // } // //读文件 // Person p; // ifs.read((char*)&p, sizeof(Person)); // cout << "姓名:" << p.m_name << "年龄:" << p.m_age << endl; // //关闭文件 // ifs.close(); //} //int main() //{ // test01(); // return 0; //} //C++提高编程 // //模板 // //函数模板-提高复用性 // 函数模板语法 // template<typename T> //函数声明或定义 // template-声明创建模板 // typename-其后面的符号是一种数据类型 可以用class代替 //实现两个整型交换的函数 //void swapInt(int &a, int &b) //{ // int temp = a; // a = b; // b = temp; //} 实现两个浮点型交换的函数 //void swapdouble(double& a, double& b) //{ // double temp = a; // a = b; // b = temp; //} //函数模板 // //template<typename T>//声明一个模板,告诉编译器后面代码紧跟的T不要报错,T是一个通用数据类型 //void mySwap(T &a, T& b) //{ // T temp = a; // a = b; // b = temp; //} // //void test01() //{ // int a = 10; // int b = 20; // //利用函数模板交换 // //两种方式使用函数模板 // //1.自动类型推导 // //mySwap(a, b); // // //2.显示指定类型 // mySwap<int>(a, b); // cout << "a=" << a << " b=" << b << endl; //} // //void test02() //{ // double a = 0.01; // double b = 0.02; // //1.自动类型推导 // //mySwap(a, b); // // //2.显示指定类型 // mySwap<double>(a, b); // cout << "a=" << a << " b=" << b << endl; //} //int main() //{ // test01(); // test02(); // return 0; //} //函数模板注意事项 // template<class T>//typename可以替换成class // 1.自动类型推导,必须推导出一致的数据类型T才可以使用 // void mySwap(T& a, T& b) // { // T temp = a; // a = b; // b = temp; // } // // void test01() // { // int a = 10; // double b = 20.00; // mySwap(a, b);//类型不一致 // cout << "a=" << a << endl; // cout << "b=" << b << endl; // } // //2.模板必须要确定出T的数据类型,才可以使用 //template<class T> //void func() //{ // cout << "func函数的调用" << endl; //} // //int main() //{ // //func();//必须要确定出T的数据类型 // func<int>(); // return 0; //} //函数模板的案例 //实现一个通用的对数组排序的函数 //从大到小 //算法 选择 //测试 char 数组、int数组 //交换函数模板 //template<class T> //void mySwap(T& a, T& b) //{ // T temp = a; // a = b; // b = temp; //} 实现一个通用的对数组排序的函数 //template<class T> //void mySort(T arr[],int len) //{ // for (int i = 0; i < len; i++) // { // int max = i;//认定最大值的下标 // for (int j = i + 1; j < len; j++) // { // if (arr[max] < arr[j])//认定的最大值比遍历的数值要小,说明j下标的元素才是真正的最大值 // { // max = j;//更新最大值下标 // } // } // if (max != i) // { // //交换max和i元素 // mySwap(arr[max], arr[i]); // } // } //} // 提供打印数组的模板 //template<class T> //void printArray(T arr[], int len) //{ // for (int i = 0; i < len; i++) // { // cout << arr[i] << " "; // } // cout << endl; //} //void test01() //{ // //测试char数组 // char charArr[] = "badcfe"; // int num = sizeof(charArr)/sizeof(char); // mySort(charArr, num); // printArray(charArr, num); //} //void test02() //{ // //测试int数组 // int intArr[] = { 7,5,3,8,9,3,1 }; // int num = sizeof(intArr) / sizeof(int); // mySort(intArr, num); // printArray(intArr, num); //} //int main() //{ // test01(); // test02(); // return 0; //} //普通函数与函数模板的转换 //1.普通函数调用时可以发生自动类型转换 //2.函数模板 用自动类型推导,不可以发生隐式类型转换 //3.函数模板 用显示指定类型,可以发生隐式类型转换 //普通函数 //int myAdd01(int a,int b) //{ // return a + b; //} //void test01() //{ // int a = 10; // int b = 20; // char c = 'c';//a-97,c-99 // //1.普通函数调用时可以发生自动类型转换 // cout << myAdd01(a, c) << endl; //} //int main() //{ // test01(); // return 0; //} //函数模板 //template<class T> //T myAdd02(T a,T b) //{ // return a + b; //} //void test01() //{ // int a = 10; // int b = 20; // char c = 'c'; // //2.函数模板 用自动类型推导,不可以发生隐式类型转换 // //cout << myAdd02(a, c) << endl; // // //3.函数模板 用显示指定类型,可以发生隐式类型转换 // cout << myAdd02<int>(a, c) << endl; //} //int main() //{ // test01(); // return 0; //} //普通函数和函数模板的调用规则 //1.如果函数模板和普通函数都可以调用,优先调用普通函数 //2.如果通过空模板参数列表 强制调用 函数模板 //3.函数模板也可以发生函数重载 //4.如果函数模板可以产生更好的匹配 优先调用函数模板 //void myPrint(int a, int b) //{ // cout << "调用的普通函数" << endl; //} // //template<class T> //void myPrint(T a, T b) //{ // cout << "调用函数模板" << endl; //} // //template<class T> //void myPrint(T a, T b,T c) //{ // cout << "调用重载的模板" << endl; //} //void test01() //{ // int a = 0; // int b = 1; // int c = 2; // //1.如果函数模板和普通函数都可以调用,优先调用普通函数 // myPrint(a, b); // // //2.如果通过空模板参数列表 强制调用 函数模板 // myPrint<>(a, b); // // //3.函数模板也可以发生函数重载 // myPrint<>(a,b,c); // // // //4.如果函数模板可以产生更好的匹配 优先调用函数模板 // char c1 = 'a'; // char c2 = 'b'; // myPrint(c1, c2); // // //提供了函数模板就不要提供普通函数 容易发生二义性 //} //int main() //{ // test01(); // return 0; //} //模板的局限性 //模板并不是万能的,有些特定的数据类型 需要用具体化的方式做特殊实现 //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // // string m_name; // int m_age; //}; // 对比两个数据是否相等 //template<class T> //bool myCompare(T& a, T& b) //{ // if (a == b) // { // return true; // } // else // { // return false; // } //} // 利用具体化的Person版本实现代码,具体化优先调用 //template<>bool myCompare(Person& p1, Person& p2) //{ // if (p1.m_name == p2.m_name && p1.m_age == p2.m_age) // { // return true; // } // else // { // return false; // } //} // //void test01() //{ // int a = 10; // int b = 20; // bool ret = myCompare(a, b); // if (ret) // { // cout <<"a=b" << endl; // } // else // { // cout << "a!=b" << endl; // } //} //void test02() //{ // Person p1("Tom", 10); // Person p2("Tom", 10); // // bool ret = myCompare(p1, p2); // if (ret) // { // cout << "a=b" << endl; // // } // else // { // cout << "a!=b" << endl; // } //} //int main() //{ // test01(); // test02(); // // return 0; //} // 类模板 //建立一个通用的类,类中的成员 数据类型可以不具体制定 用一个虚拟的类型来表示 //template<class NameType,class AgeType> //class Person //{ //public: // Person(NameType name,AgeType age) // { // this->m_name = name; // this->m_age = age; // } // // void showPerson() // { // cout << "name:" << this->m_name << " age:" << this->m_age << endl; // } // string m_name; // int m_age; //}; //void test01() //{ // Person<string, int> p1("孙悟空",99); // p1.showPerson(); //} //int main() //{ // test01(); // return 0; //} //类模板与函数模板的区别 //1.类模板没有自动类型推导的使用方式 //2.类模板在模板参数列表中可以有默认参数 //2.类模板在模板参数列表中可以有默认参数 //template<class NameType=string,class AgeType=int> //class Person //{ //public: // Person(NameType name, AgeType age) // { // this->m_name = name; // this->m_age = age; // } // // void showPerson() // { // cout << "name:" << this->m_name << " age:" << this->m_age << endl; // } // NameType m_name; // AgeType m_age; //}; // //void test01() //{ // //Person p("孙悟空",1000); //1.类模板没有自动类型推导的使用方式 // Person<string, int>p("孙悟空", 1000); // p.showPerson(); //} // 2.类模板在模板参数列表中可以有默认参数 //void test02() //{ // Person<>p("张三", 999); // p.showPerson(); //} //int main() //{ // test01(); // test02(); // return 0; //} //类模板中成员函数创建时机 //类模板中成员函数只有在调用时才去创建 //class Person1 //{ //public: // void showPerson1() // { // cout << "Person1 show" << endl; // } //}; // //class Person2 //{ //public: // void showPerson2() // { // cout << "Person2 show" << endl; // } //}; //template<class T> //class Myclass //{ //public: // T obj; // // //类模板中成员函数 // void func1() // { // obj.showPerson1(); // } // void func2() // { // obj.showPerson2(); // } //}; //void test01() //{ // Myclass<Person2>m; // //类模板中成员函数只有在调用时才去创建 // //m.func1(); // m.func2(); // //} //int main() //{ // test01(); // return 0; //} //类模板对象做函数参数 //1.指定函数的类型 //2.参数模板化 //3.整个类模板化 //template<class T1,class T2> //class Person //{ //public: // Person(T1 name, T2 age) // { // this->m_name=name; // this->m_age=age; // } // // void showPerson() // { // cout << "姓名:" << this->m_name << " 年龄:" << this->m_age << endl; // } // T1 m_name; // T2 m_age; //}; // 1.指定传入类型 //void printPerson1(Person<string,int>&p) //{ // p.showPerson(); //} //void test01() //{ // Person<string, int>p("张三", 100); // printPerson1(p); //} 2.将参数模板化 //template<class T1,class T2> //void printPerson2(Person<T1, T2>& p) //{ // p.showPerson(); // cout << "T1的类型为:" << typeid(T1).name() << endl; // cout << "T2的类型为:" << typeid(T2).name() << endl; // //} //void test02() //{ // Person<string, int>p("李四", 200); // printPerson2(p); //} 3.整个类模板化 //template<class T> //void printPerson3(T& p) //{ // p.showPerson(); // cout << "T的数据为:" << typeid(T).name() << endl; //} //void test03() //{ // Person<string, int>p("唐僧", 30); // printPerson3(p); //} //int main() //{ // test01(); // test02(); // test03(); // return 0; //} //类模板与继承 //template<class T> //class Base //{ //public: // T m; //}; // class Son :public Base//必须知道父类T的数据类型才能继承 //class Son : public Base<int> //{ // //}; //void test01() //{ // Son s1; //} // 如果想灵活指定父类中的T类型,子类也需要变类模板 //template<class T1,class T2> //class Son2 :public Base<T2> //{ //public: // Son2() // { // cout << "T1的数据类型:" << typeid(T1).name() << endl; // cout << "T2的数据类型:" << typeid(T2).name() << endl; // } // T1 obj; //}; // //void test02() //{ // Son2<int, char>S2;//T1-int T2-char //} //int main() //{ // test01(); // test02(); // // return 0; //} //类模板成员函数的类外实现 //template<class T1,class T2> //class Person //{ //public: // Person(T1 name, T2 age); // // void showPerson(); // // T1 m_name; // T2 m_age; //}; // 构造函数的类外实现 //template<class T1,class T2> // Person<T1,T2>::Person(T1 name, T2 age) //{ // // this->m_name = name; // this->m_age = age; //} // template<class T1, class T2> // void Person<T1,T2>::showPerson() //{ // cout << "姓名:" << this->m_name << " 年龄:" << m_age << endl; // } // //void test01() //{ // Person<string, int>p("张三", 100); // p.showPerson(); //} //int main() //{ // test01(); // return 0; //} //类模板的分文件编写问题及解决 //template<class T1,class T2> //class Person //{ //public: // Person(T1 name, T2 age); // // void showPerson(); // // T1 m_name; // T2 m_age; //}; //template<class T1, class T2> //Person<T1, T2>::Person(T1 name, T2 age) //{ // this->m_name = name; // this->m_age = age; //} // //template<class T1, class T2> //void Person<T1, T2>::showPerson() //{ // cout << "姓名:" << this->m_name << " 年龄:" << this->m_age << endl; //} //第一种解决方式 //#include"Person.cpp" 第二种解决方式 将.h和.cpp中的内容写到一起 将后缀名改为.hpp文件(常用) //#include"Person.hpp" // //void test01() //{ // Person<string, int>p("TOM", 100); // p.showPerson(); //} //int main() //{ // test01(); // return 0; //} //类模板与友元 //掌握类模板配合友元函数的类内和类外实现 //提前让编译器知道Person存在 //template<class T1, class T2> //class Person; // 类外实现 //template<class T1, class T2> //void printPerson2(Person<T1, T2>p) //{ // cout << "类外实现--姓名:" << p.m_name << " 类外实现---年龄:" << p.m_age << endl; //} //template<class T1,class T2> //class Person //{ // //全局函数 类内实现 // friend void printPerson(Person<T1, T2>p) // { // cout << "姓名:" << p.m_name << " 年龄:" << p.m_age << endl; // } // // //全局函数 类外实现 // //加空模板参数列表 // //如果全局函数 是类外实现 需要让编译器提前知道这个函数存在 // friend void printPerson2<>(Person<T1, T2>p); // //public: // Person(T1 name, T2 age) // { // this->m_name = name; // this->m_age = age; // } //private: // T1 m_name; // T2 m_age; //}; // 1.全局函数在类内实现 //void test01() //{ // Person<string, int>p("gide",23); // printPerson(p); //} 2.全局函数在类外实现 //void test02() //{ // Person<string,int>p("Tom", 21); // printPerson2(p); //} //int main() //{ // test01(); // test02(); // return 0; //} //类模板案例 //#include"myArray.hpp" // //void printArray(MyArray<int>&arr) //{ // for (int i = 0; i < arr.getSize(); i++) // { // cout << arr[i] << endl; // } //} // //void test01() //{ // MyArray<int>arr1(5); // // for (int i = 0; i < 5; i++) // { // arr1.Push_back(i);//利用尾插法向数组中插入数据 // } // cout << "arr1的打印输出为:" << endl; // // printArray(arr1); // // cout << "arr1的容量为:" << arr1.getCapacity() << endl; // cout << "arr1的大小为:" << arr1.getSize() << endl; // // MyArray<int>arr2(arr1); // // cout << "arr2的打印输出为:" << endl; // // printArray(arr2); // // //尾删 // arr2.Pop_back(); // // cout << "arr2尾删后" << endl; // cout << "arr2尾删后的容量为:" << arr2.getCapacity() << endl; // cout << "arr2的大小为:" << arr2.getSize() << endl; //} // 测试自定义数据类型 //class Person //{ //public: // Person(){} // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // // string m_name; // int m_age; //}; // //void printPersonArray(MyArray<Person>&arr) //{ // for (int i = 0; i < arr.getSize(); i++) // { // cout << "姓名:" << arr[i].m_name << " 年龄:" << arr[i].m_age << endl; // } //} // //void test02() //{ // MyArray<Person>arr(10); // Person p1("张三", 100); // Person p2("李四", 200); // Person p3("王五", 300); // Person p4("赵六", 400); // Person p5("周期", 500); // // //将数据插入到数组中 // arr.Push_back(p1); // arr.Push_back(p2); // arr.Push_back(p3); // arr.Push_back(p4); // arr.Push_back(p5); // // //打印数组 // printPersonArray(arr); // // //输出容量和大小 // cout << "arr容量为:" << arr.getCapacity() << endl; // cout << "arr大小为:" << arr.getSize() << endl; //} //int main() //{ // test01(); // test02(); // // return 0; //} //STL-标准模板库 //STL分为容器 算法 迭代器 //STL六大组件-容器 算法 迭代器 仿函数 适配器 空间配置器 // 1.容器:vector list deque set map // 2.算法:sort find copy for_each // 3.迭代器:容器和算法之前通过迭代器连接 //4.仿函数:行为类似函数,作为算法的某种策略 //5.适配器:修饰接口 //6.空间配置器:负责空间的配置与管理 //容器算法迭代器 //vector //#include<vector> //#include<algorithm>//标准算法头文件 // //void MyPrint(int val) //{ // cout << val << endl; //} // vector容器存放内置数据类型 //void test01() //{ // //创建了一个vector容器,数组 // vector<int>v; // //向容器尾插数据 // v.push_back(10); // v.push_back(20); // v.push_back(30); // v.push_back(40); // // //通过迭代器访问容器中的数据 // //vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素 // //vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个位置 // // 第一种遍历方式 // //while (itBegin != itEnd) // //{ // // cout << *itBegin << endl; // // itBegin++; // //} // // //第二种遍历方式 // //for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // //{ // // cout << *it << endl; // //} // // //第三种遍历方式 利用STL提供的遍历算法 // for_each(v.begin(), v.end(), MyPrint); //} //int main() //{ // test01(); // return 0; //} //Vector存放自定义数据类型 #include<vector> //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // string m_name; // int m_age; //}; // //void test01() //{ // vector<Person>v; // // Person p1("aaa", 10); // Person p2("bbb", 10); // Person p3("ccc", 10); // Person p4("ddd", 10); // Person p5("eee", 10); // Person p6("fff", 10); // // //向容器中添加数据 // v.push_back(p1); // v.push_back(p2); // v.push_back(p3); // v.push_back(p4); // v.push_back(p5); // v.push_back(p6); // 遍历容器中的数据 // for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) // { // cout << "姓名:" << it->m_name << " 年龄:"<<it->m_age<<endl; // } //} // 存放自定义数据类型的指针 //void test02() //{ // vector<Person*>v; // // Person p1("aaa", 10); // Person p2("bbb", 10); // Person p3("ccc", 10); // Person p4("ddd", 10); // Person p5("eee", 10); // Person p6("fff", 10); // // //向容器中添加数据 // v.push_back(&p1); // v.push_back(&p2); // v.push_back(&p3); // v.push_back(&p4); // v.push_back(&p5); // v.push_back(&p6); // // //遍历容器 // for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) // { // cout << "姓名:" << (*it)->m_name << " 年龄:" << (*it)->m_age << endl; // } //} //int main() //{ // test01(); // test02(); // // return 0; //} //Vector容器嵌套容器 //void test01() //{ // vector<vector<int>>v; // // //创建小容器 // vector<int>v1; // vector<int>v2; // vector<int>v3; // vector<int>v4; // // //向小容器中添加数据 // for (int i = 0; i < 4; i++) // { // v1.push_back(i + 1); // v2.push_back(i + 2); // v3.push_back(i + 3); // v4.push_back(i + 4); // } // // //将小容器插入到大容器中 // v.push_back(v1); // v.push_back(v2); // v.push_back(v3); // v.push_back(v4); // // //通过大容器,把所有数据遍历一遍 // for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++) // { // //(*it)---容器vector<int> // for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) // { // cout << *vit << " "; // } // cout << endl; // } // //} //int main() //{ // test01(); // return 0; //} //string容器-构造函数 //void test01() //{ // string s1;//默认构造 // // const char* str = "hello world"; // // string s2(str); // cout << "s2=" << s2 << endl; // // string s3(s2);//拷贝构造 // cout << "s3=" << s3 << endl; // // string s4(10, 'a'); // cout << "s4=" << s4; //} //int main() //{ // test01(); // return 0; //} //string赋值操作 //void test01() //{ // string str1; // str1 = "hello world"; // cout << "str1=" << str1<<endl; // // string str2; // str2 = str1; // cout << "str2=" << str2<<endl; // // string str3; // str3 = 'a'; // cout << "str3=" << str3 << endl; // // string str4; // str4.assign("hello coder");//.assign 赋值 // cout << "str4=" << str4 << endl; // // string str5; // str5.assign("hello c++", 5); // cout << "str5=" << str5 << endl; // // string str6; // str6.assign(str5); // cout << "str6=" << str6 << endl; // // string str7; // str7.assign(10, 'w'); // cout << "str7=" << str7 << endl; //} //int main() //{ // test01(); // return 0; //} //string字符串拼接 //void test01() //{ // string str1 ="我"; // str1 += "爱你"; // cout << "str1="<<str1 << endl; // str1 += '!'; // cout << "str1=" << str1 << endl; // // string str2 = " LOL"; // str1 += str2; // cout << "str1=" << str1 << endl; // // string str3 = "I"; // str3.append(" love");//.append 追加 // cout << "str3=" << str3 << endl; // // str3.append(" game abcde", 5); // cout << "str3=" << str3 << endl; // // str3.append(str2); // cout << "str3=" << str3 << endl; // // str3.append(str2, 2, 3);//从第二个字符开始截取 截取3个字符 // cout << "str3=" << str3 << endl; // //} //int main() //{ // test01(); // return 0; //} //string查找和替换 //1.查找 //void test01() //{ // string str1 = "abcdefg"; // int pos=str1.find("de");//find从左往右查 int pos = str1.find("df"); // if (pos == -1) // { // cout << "未找到字符串" << endl; // } // else // { // cout << "pos=" << pos << endl; // } // // //rfind // //rfind从右往左查 find从左往右查 // pos=str1.rfind("de"); // cout << "pos=" << pos << endl; //} // 2.替换 // //void test02() //{ // string str1 = "abcdef"; // str1.replace(1, 3, "1111");//从第一个字符起替换3个字符 // cout << "str1=" << str1 << endl; // //} // //int main() //{ // test01(); // test02(); // return 0; //} //string字符串比较 //void test01() //{ // string str1 = "hello"; // string str2 = "aello"; // // if (str1.compare(str2) == 0)//按照AskⅡ逐个对比 // { // cout << "str1等于str2" << endl; // } // else if(str1.compare(str2)>0) // { // cout << "str1大于str2" << endl; // } // else // { // cout << "str1小于str2" << endl; // } //} //int main() //{ // test01(); // return 0; //} //string字符存取 //void test01() //{ // string str = "hello"; // cout << "str=" << str << endl; // // //1.通过[]访问单个字符 // for (int i = 0; i < str.size(); i++)//字符串长度 -str.size // { // cout << str[i] << " "; // } // cout << endl; // // //2.通过at方式访问单个字符 // for (int i = 0; i < str.size(); i++) // { // cout << str.at(i) << " "; // } // cout << endl; // // //修改单个字符(也可以通过[]或at) // str[0] = 'x'; // cout << "str=" << str << endl; // // str.at(1) = 'x'; // cout << "str=" << str << endl; // //} //int main() //{ // test01(); // return 0; //} //string插入和删除 //void test01() //{ // string str = "hello"; // // //插入 // str.insert(1, "111"); // cout << "str=" << str << endl; // // //删除 // str.erase(1, 3);//从第位置1起 删3个字符 // cout << "str=" << str << endl; //} //int main() //{ // test01(); // return 0; //} //string子串 //void test01() //{ // string str = "abcdef"; // // string subStr = str.substr(1, 3);//从位置1开始 截取3个 // cout << "subStr=" << subStr << endl; //} //void test02() //{ // string email = "zhangsan@sina.com"; // // //从邮件地址中 获取 用户名信息 // int pos=email.find("@");//8 // cout << pos << endl; // // string userName = email.substr(0, pos);//从0开始 截取8个 // cout << userName << endl; //} //int main() //{ // test01(); // test02(); // return 0; //} //vector与普通数组的区别 //普通数组是静态空间 vector可以动态扩展 //push_back()-尾插 pop_back()-尾删 .insert()-插入 v.begin()-首位 v.end()-末位 v.rend()-首位前一位 //Vector容器-构造函数 //void printVector(vector<int>v); // //void test01() //{ // vector<int>v1;//默认构造/无参构造 // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // printVector(v1); // // //通过区间方式构造函数 // vector<int>v2(v1.begin(), v1.end()); // printVector(v2); // // //n个elem方式构造 // vector<int>v3(10, 100);//10个100 // printVector(v3); // // //拷贝构造 // vector<int>v4(v3); // printVector(v4); // //} // //void printVector(vector<int>v) //{ // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //int main() //{ // test01(); // return 0; //} //vector-赋值 //void printVector(vector<int>& v) //{ // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // vector<int>v1; // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // printVector(v1); // // //赋值 operator= // vector<int>v2; // v2 = v1; // printVector(v2); // // //assgin // vector<int>v3; // v3.assign(v1.begin(), v1.end()); // printVector(v3); // // //n个elem方式赋值 // vector<int>v4; // v4.assign(10, 100);//赋值10个100 // printVector(v4); //} //int main() //{ // test01(); // return 0; //} //vector容量和大小 //void printVector(vector<int>&v) //{ // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} // //void test01() //{ // vector<int>v1; // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // printVector(v1); // // if (v1.empty())//为真 代表为空 // { // cout << "v1为空" << endl; // } // else // { // cout << "v1不为空" << endl; // cout <<"v1的容量为:"<< v1.capacity() << endl; // cout << "v1的大小为:" << v1.size() << endl; // } // //重新指定大小 // v1.resize(15,100);//利用重载版本,可以指定默认填充值,参数2 // printVector(v1);//如果重新指定的过长 默认用0填充新的位置 // // v1.resize(5); // printVector(v1); //如果重新指定的短,超出的部分会删掉 // //} // //int main() //{ // test01(); // return 0; //} //vector的插入和删除 //void printVector(vector<int>&v) //{ // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} // //void test01() //{ // vector<int>v1; // //尾插 // v1.push_back(10); // v1.push_back(20); // v1.push_back(30); // v1.push_back(40); // v1.push_back(50); // // //遍历 // printVector(v1); // // //尾删 // v1.pop_back(); // printVector(v1); // // //插入 第一个参数是迭代器 // v1.insert(v1.begin(),100);//在头部插入1个100 // printVector(v1); // // v1.insert(v1.begin(), 2, 1000);//插入两个1000在头部 // printVector(v1); // // //删除 // v1.erase(v1.begin());//删除头部 // printVector(v1); // // //清空 // v1.erase(v1.begin(), v1.end()); // v1.clear(); // printVector(v1); //} // //int main() //{ // test01(); // return 0; //} //vector数据存取 //void printVector(vector<int>& v) //{ // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // vector<int>v1; // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // //利用[]访问数组中的元素 // for (int i = 0; i < v1.size(); i++) // { // cout << v1[i] <<" "; // } // cout << endl; // //利用at方式访问元素 // for (int i = 0; i < v1.size(); i++) // { // cout << v1.at(i) << " "; // } // cout << endl; // // //获取第一个元素 // cout << "第一个元素:" << v1.front() << endl; // //获取最后一个元素 // cout << "最后一个元素:" << v1.back() << endl; //}
//int main() //{ // test01(); // return 0; //} //vector互换容器 //void printVector(vector<int>& v) //{ // for (vector<int>::iterator it = v.begin(); it < v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // vector<int>v1; // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // printVector(v1); // // vector<int>v2; // for (int i = 10; i > 0; i--) // { // v2.push_back(i); // } // printVector(v2); // cout << "交换后:" << endl; // v1.swap(v2);//swap 互换容器 // printVector(v1); // printVector(v2); //} swap可以收缩内存空间 //void test02() //{ // vector<int>v; // for (int i = 0; i < 100000; i++) // { // v.push_back(i); // } // cout << "v的容量:" << v.capacity() << endl; // cout << "v的大小:" << v.size() << endl; // // v.resize(3);//重新指定大小 // cout << "v的容量:" << v.capacity() << endl; // cout << "v的大小:" << v.size() << endl; // // //用swap来收缩内存 // vector<int>(v).swap(v);//vector<int>(v)-匿名对象 // cout << "v的容量:" << v.capacity() << endl; // cout << "v的大小:" << v.size() << endl; //} //int main() //{ // test01(); // test02(); // // return 0; //} //vector预留空间 //void test01() //{ // vector<int>v; // // int num = 0; // int* p = NULL; // // // //利用reserve预留空间 // v.reserve(100000);//预留100000空间 // // for (int i = 0; i < 100000; i++) // { // v.push_back(i); // // if (p != &v[0]) // { // p = &v[0]; // num++;//统计开辟地址的次数(预留后只需开辟一次) // } // } // cout << "num=" << num << endl; // //} //int main() //{ // test01(); // return 0; //} //deque容器-双端数组 //可以对头端进行插入删除操作 //deque和vector区别:对头部插入删除速度比vector快 //deque构造函数 #include<deque> //void printDeque(const deque<int>& d) //{ // for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) // { // //*it = 100; 容器里中的数据不可修改 // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // deque<int>d1; // for (int i = 0; i < 10; i++) // { // d1.push_back(i); // } // printDeque(d1); // // deque<int>d2(d1.begin(), d1.end()); // printDeque(d2); // // deque<int>d3(10, 100);//容器里放10个100 // printDeque(d3); // // deque<int>d4(d3); // printDeque(d4); // //} //int main() //{ // test01(); // return 0; //} //deque赋值 //void printDeque(const deque<int>& d) //{ // for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // deque<int>d1; // for (int i = 0; i < 10; i++) // { // d1.push_back(i); // } // printDeque(d1); // // //operato=赋值 // deque<int>d2; // d2 = d1; // printDeque(d2); // // //assign赋值 // deque<int>d3; // d3.assign(d1.begin(), d1.end()); // printDeque(d3); // // deque<int>d4; // d4.assign(10, 100); // printDeque(d4); // //} //int main() //{ // test01(); // return 0; //} //deque大小操作 //void printDeque(const deque<int>& d) //{ // for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // deque<int>d1; // for (int i = 0; i < 10; i++) // { // d1.push_back(i); // } // printDeque(d1); // // if (d1.empty()) // { // cout << "deque容器里为空" << endl; // } // else // { // cout << "deque容器里不为空" << endl; // cout << "d1的大小:" << d1.size() << endl; // //deque容器里没有容量的概念 // } // d1.resize(15, 1);//重新指定尺寸 多出来的用1填充 // printDeque(d1); // // d1.resize(5);//多出来的会删除 // printDeque(d1); //} //int main() //{ // test01(); // return 0; //} //deque的插入和删除 //void printDeque(const deque<int>& d) //{ // for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} // //void test01() //{ // deque<int>d1; // //尾插 // d1.push_back(10); // d1.push_back(20); // // //头插 // d1.push_front(100); // d1.push_front(200); // // //200 100 10 20 // printDeque(d1); // // d1.pop_back();//200 100 10 // printDeque(d1); // // d1.pop_front();//100 10 // printDeque(d1); //} //void test02() //{ // deque<int>d1; // d1.push_back(10); // d1.push_back(10); // d1.push_front(100); // d1.push_front(200); // // //200 100 10 20 // printDeque(d1); // // //1000 200 100 10 10 // d1.insert(d1.begin(), 1000); // printDeque(d1); // // //1000 1000 1000 200 100 10 10 // d1.insert(d1.begin(), 2,1000); // printDeque(d1); // // //按照区间进行插入 // deque<int>d2; // d2.push_back(1); // d2.push_back(2); // d2.push_back(3); // // //1 2 3 1000 1000 1000 200 100 10 10 // d1.insert(d1.begin(), d2.begin(), d2.end());//在d1的头部插入一个d2的头部到尾部 // printDeque(d1); //} // //void test03() //{ // deque<int>d1; // d1.push_back(10); // d1.push_back(20); // d1.push_back(100); // d1.push_back(200); // // //删除 // deque<int>::iterator it = d1.begin(); // it++;//删除第二个位置 // d1.erase(it); // //200 10 20 // printDeque(d1); // // //按区间的方式删除 // d1.erase(d1.begin(), d1.end()); // printDeque(d1); // //清空 // d1.clear(); // printDeque(d1); //} //int main() //{ // test03(); // return 0; //} //deque数据存取 //void test01() //{ // deque<int>d; // d.push_back(10); // d.push_back(20); // d.push_back(30); // d.push_front(100); // d.push_front(200); // d.push_front(300); // // //通过[]方式访问元素 // for (int i = 0; i < d.size(); i++) // { // cout << d[i] << " "; // } // cout << endl; // // //通过at方式访问元素 // for (int i = 0; i < d.size(); i++) // { // cout << d.at(i) << " "; // } // cout << endl; // // cout << "第一个元素为:" << d.front() << endl; // cout << "最后一个元素为:" << d.back() << endl; //} //int main() //{ // test01(); // return 0; //} //deque排序 //#include<algorithm>//标准算法头文件 //void printDeque(const deque<int>& d) //{ // for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // deque<int>d; // d.push_back(10); // d.push_back(20); // d.push_back(30); // d.push_front(100); // d.push_front(200); // d.push_front(300); // // printDeque(d); // // //排序 // //对于支持随机访问的迭代器 都可以利用sort算法直接对其进行排序 // //vector容器也可利用sort进行排序 // sort(d.begin(), d.end());//默认升序 // printDeque(d); // //} //int main() //{ // test01(); // return 0; //} //案例-评委打分 //选手类 #include<list> #include<algorithm> //class Person //{ //public: // Person(string name, int score) // { // this->m_name = name; // this->m_score = score; // } // string m_name; // int m_score; //}; // //void creatPerson(list<Person>&L) //{ // string nameSeed = "ABCDE"; // // for (int i = 0; i < 5; i++) // { // string name = "选手"; // name += nameSeed[i]; // // int score = 0; // // Person p(name, score); // // //将创建的Person对象放入到容器中 // L.push_back(p); // } //} // 打分 //void setScore(list<Person>&L) //{ // for (list<Person>::iterator it = L.begin(); it != L.end(); it++) // { // //将评委的分数 放入到list容器中 // list<int>L; // for (int i = 0; i < 10; i++) // { // int score = rand()%41+60; //60-100 // L.push_back(score); // } // // //排序 // L.sort(); // // //去除最高和最低分 // L.pop_front(); // L.pop_back(); // // //取平均分 // int sum = 0; // for (list<int>::iterator dit = L.begin(); dit != L.end(); dit++) // { // sum += *dit;//累加每个评委的分数 // } // int avg = sum / L.size(); // // //将平均分赋值给选手 // it->m_score = avg; // } //} // //void showScore(list<Person>& L) //{ // for (list<Person>::iterator it = L.begin(); it != L.end(); it++) // { // cout << "姓名:" << it->m_name << " 平均分:" << it->m_score << endl; // } //} // 指定排序规则 //bool comparePerson(Person &p1,Person &p2) //{ // //按照分数做一个升序 // // return p1.m_score > p2.m_score; //} // //int main() //{ // //随机数种子 // srand((unsigned int)time(NULL)); // // //1.创建5名选手 // list<Person>L;//存放选手的容器 // creatPerson(L); // // //2.给5名选手打分 // setScore(L); // // //3.显示最后的得分 // showScore(L); // // //按照分数进行排序 // L.sort(comparePerson); // cout << "排序后:------------------" << endl; // showScore(L); // // return 0; //} //stack容器-栈 //先进后出 //stack-构造函数 #include<stack> //void test01() //{ // //特点:先进后出 // stack<int>s; // // //入栈 // s.push(10); // s.push(20); // s.push(30); // s.push(40); // // cout << "栈的大小:" << s.size() << endl; // // //只要栈不为空,查看栈顶,并且执行出栈操作 // while (!s.empty()) // { // //查看栈顶元素 // cout << "栈顶元素为:" << s.top() << endl; // // //出栈 // s.pop(); // } // cout << "栈的大小:" << s.size() << endl; //} //int main() //{ // test01(); // return 0; //} //队列queue容器-先进先出 #include<queue> //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // string m_name; // int m_age; //}; //void test01() //{ // //创建队列 // queue<Person>q; // // //准备数据 // Person p1("唐僧", 1000); // Person p2("孙悟空", 900); // Person p3("猪八戒", 800); // Person p4("沙僧", 700); // // //入队 // q.push(p1); // q.push(p2); // q.push(p3); // q.push(p4); // // //判断只要队列不为空,查看队头,查看队尾,出队 // cout << "队列的大小:" << q.size() << endl; // // while (!q.empty()) // { // cout << "队列头元素----姓名:" << q.front().m_name << endl; // // cout << "队列尾元素----姓名:" << q.back().m_name << endl; // // //出队列 // q.pop(); // } // // cout << "队列的大小:" << q.size() << endl; //} //int main() //{ // test01(); // return 0; //} //list容器 //链表 优点:可以对任意位置进行快速插入或删除元素 //缺点:遍历速度慢、占用空间比数组大、采用动态分配,不会造成内存浪费和溢出 //STL中的链表是一个双向循环链表 //list构造函数 #include<list> //void printList(const list<int>& L) //{ // for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // //创建List容器 // list<int>L1; // // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // // printList(L1); // // //区间方式构造 // list<int>L2(L1.begin(), L1.end()); // printList(L2); // // //拷贝构造 // list<int>L3(L2); // printList(L3); // // //n个elem // list<int>L4(10, 1000); // printList(L4); //} // //int main() //{ // test01(); // return 0; //} //赋值与交换 //void printList(const list<int>& L) //{ //for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) //{ // cout << *it << " "; //} //cout << endl; //} //void test01() //{ // //创建List容器 // list<int>L1; // // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // printList(L1); // // //赋值 // list<int>L2; // L2 = L1; // printList(L2); // // list<int>L3; // L3.assign(L2.begin(),L2.end()); // printList(L3); // // list<int>L4; // L4.assign(10, 100); // printList(L4); //} //void test02() //{ // //交换 // list<int>L1; // // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // // list<int>L2; // L2.assign(10, 100); // // cout << "交换前:" << endl; // printList(L1); // printList(L2); // // L1.swap(L2); // cout << "交换后:" << endl; // printList(L1); // printList(L2); //} //int main() //{ // test01(); // test02(); // // return 0; //} //list大小操作 //void printList(const list<int>&L) //{ // for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // list<int>L1; // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // // printList(L1); // // //判断容器是否为空 // if (L1.empty()) // { // cout << "L1为空!" << endl; // } // else // { // cout << "L1不为空!" << endl; // cout << "L1的元素个数" << L1.size() << endl; // } // //重新指定他的大小 // L1.resize(10,10000); // printList(L1); // // L1.resize(2); // printList(L1); //} //int main() //{ // test01(); // return 0; //} //list插入和删除 //void printList(const list<int>& L) //{ // for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // list<int>L1; // //尾插 // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // // //头插 // L1.push_front(100); // L1.push_front(200); // L1.push_front(300); // // printList(L1); // // //尾删 // L1.pop_back(); // L1.pop_front(); // printList(L1); // // //头插 // list<int>::iterator it = L1.begin(); // L1.insert(++it, 1000); // printList(L1); // // //删除 // it = L1.begin(); // L1.erase(++it); // printList(L1); // // //移除 // L1.push_back(10000); // L1.push_back(10000); // L1.push_back(10000); // L1.push_back(10000); // printList(L1); // // L1.remove(10000); // printList(L1); // // L1.clear(); // printList(L1); //} //int main() //{ // test01(); // return 0; //} //list-数据存取 //void printList(const list<int>& L) //{ // for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // list<int>L1; // //尾插 // L1.push_back(10); // L1.push_back(20); // L1.push_back(30); // L1.push_back(40); // // printList(L1); // //L1[0] L1.at(0) 不可访问 // //原因是list本质是链表,不是连续线性空间存储数据,迭代器不支持随机访问 // cout << "第一个元素:" << L1.front() << endl; // cout << "最后一个元素:" << L1.back() << endl; // // //验证迭代器是不支持随机访问的 // list<int>::iterator it = L1.begin(); // //it += 2;//不支持 // it++;//支持 //} //int main() //{ // test01(); // return 0; //} //list反转和排序 //void printList(const list<int>& L) //{ // for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} // //bool myCompare(int v1,int v2) //{ // //降序 让第一个数大于第二个数 // return v1 > v2; //} //void test01() //{ // list<int>L1; // //尾插 // L1.push_back(20); // L1.push_back(10); // L1.push_back(50); // L1.push_back(40); // // cout << "反转前:" << endl; // printList(L1); // // //反转 // cout << "反转后:" << endl; // L1.reverse(); // printList(L1); // // //排序 // cout << "排序后:" << endl; // L1.sort();//默认排序从小到大 // printList(L1); // // L1.sort(myCompare);//从大到小排序 // printList(L1); //} // //int main() //{ // test01(); // return 0; //} //list排序案例 //class Person //{ //public: // Person(string name, int age, int height) // { // this->m_name = name; // this->m_age = age; // this->m_height = height; // } // string m_name; // int m_age; // int m_height; //}; // 指定排序规则 //bool comparePerson(Person &p1,Person &p2) //{ // //按照年龄做一个升序 // if (p1.m_age == p2.m_age) // { // //年龄相同 按身高降序 // return p1.m_height > p2.m_height; // } // return p1.m_age < p2.m_age; //} //void test01() //{ // //创建容器 // list<Person>L; // // //准备数据 // Person p1("刘备", 35, 175); // Person p2("张飞", 45, 165); // Person p3("关羽", 15, 155); // Person p4("赵云", 55, 145); // Person p5("孙权", 45, 185); // Person p6("吕布", 45, 195); // // //插入数据 // L.push_back(p1); // L.push_back(p2); // L.push_back(p3); // L.push_back(p4); // L.push_back(p5); // L.push_back(p6); // // for (list<Person>::iterator it = L.begin(); it != L.end(); it++) // { // cout << "姓名:" << (*it).m_name << " 年龄: " << it->m_age << " 身高:" << (*it).m_height << endl; // } // // //排序 // cout << "--------------------" << endl; // cout << "排序后:" << endl; // // L.sort(comparePerson); // // for (list<Person>::iterator it = L.begin(); it != L.end(); it++) // { // cout << "姓名:" << (*it).m_name << " 年龄: " << it->m_age << " 身高:" << (*it).m_height << endl; // } //} //int main() //{ // test01(); // return 0; //} //set/multiset容器 //关联式容器 底层结构是二叉树 //set中不能有重复的元素 //multiset中可以有重复元素 //set构造和赋值 #include<set> //void printSet(set<int>& s) //{ // for (set<int>::iterator it = s.begin(); it != s.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // set<int>s1; // // //插入数据,只有insert方式 // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // //遍历容器 // //set容器的特点:所有元素插入的时候自动排序 // //set容器不允许插入重复的值 // printSet(s1); // // //拷贝构造 // set<int>s2(s1); // printSet(s2); // // //赋值 // set<int>s3; // s3 = s2; // printSet(s3); // //} //int main() //{ // test01(); // return 0; //} //set大小和交换 //void printSet(set<int>& s) //{ // for (set<int>::iterator it = s.begin(); it != s.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // set<int>s1; // // //插入数据,只有insert方式 // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // //遍历容器 // //set容器的特点:所有元素插入的时候自动排序 // //set容器不允许插入重复的值 // printSet(s1); // // if (s1.empty()) // { // cout << "set容器为空" << endl; // } // else // { // cout << "set容器不为空" << endl; // cout << "set容器的大小:" << s1.size() << endl; // } //} //void test02() //{ // //交换 // set<int>s1; // // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // set<int>s2; // s2.insert(10); // s2.insert(20); // s2.insert(30); // s2.insert(40); // s2.insert(50); // // cout << "交换前:" << endl; // printSet(s1); // printSet(s2); // // cout << "交换后:" << endl; // s1.swap(s2); // printSet(s1); // printSet(s2); //} //int main() //{ // test01(); // test02(); // // return 0; //} //set的插入和删除 //void printSet(set<int>& s) //{ // for (set<int>::iterator it = s.begin(); it != s.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // set<int>s1; // // //插入数据,只有insert方式 // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // //遍历容器 // //set容器的特点:所有元素插入的时候自动排序 // //set容器不允许插入重复的值 // printSet(s1); // // //删除 // s1.erase(s1.begin()); // printSet(s1); // // s1.erase(50); // printSet(s1); // // //清空 // s1.erase(s1.begin(), s1.end()); // printSet(s1); // // s1.clear(); // printSet(s1); // //} // //int main() //{ // test01(); // return 0; //} //set查找和统计 //void printSet(set<int>& s) //{ // for (set<int>::iterator it = s.begin(); it != s.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //void test01() //{ // set<int>s1; // // //插入数据,只有insert方式 // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // //遍历容器 // //set容器的特点:所有元素插入的时候自动排序 // //set容器不允许插入重复的值 // printSet(s1); // // //查找-返回的是迭代器 // set<int>::iterator pos = s1.find(300); // // if (pos != s1.end()) // { // cout << "找到元素:" << *pos << endl; // } // else // { // cout << "未找到元素" << endl; // } //} // //void test02() //{ // //统计 // set<int>s1; // // //插入数据,只有insert方式 // s1.insert(60); // s1.insert(70); // s1.insert(30); // s1.insert(50); // s1.insert(50); // // //统计30的个数 // int num = s1.count(300); // cout << "num=" << num << endl; //} //int main() //{ // test01(); // test02(); // return 0; //} //set和multiset的区别 //void printSet(set<int>& s) //{ // for (set<int>::iterator it = s.begin(); it != s.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} // //void test01() //{ // set<int>s; // //不允许插入重复值 // // pair<set<int>::iterator, bool>ret = s.insert(10); // // if (ret.second) // { // cout << "第一次插入成功" << endl; // } // else // { // cout << "第一次插入失败" << endl; // // } // ret=s.insert(10); // if (ret.second) // { // cout << "第一次插入成功" << endl; // } // else // { // cout << "第一次插入失败" << endl; // } // // multiset<int>ms; // //允许插入重复值 // ms.insert(10); // ms.insert(10); // ms.insert(10); // ms.insert(10); // // for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //int main() //{ // test01(); // return 0; //} //pair对组创建 //void test01() //{ // //第一种方式 // pair<string, int>p("Tom", 20); // cout << "姓名:" << p.first << " 年龄:" << p.second << endl; // // //第二种方式 // pair <string, int >p2= make_pair("Jerry", 40); // cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl; // //} //int main() //{ // test01(); // return 0; //} //set容器的排序 //class Mycompare //{ //public: // bool operator()(int a,int b)const // { // return a > b; // } //}; //void test01() //{ // set<int>s1; // // s1.insert(10); // s1.insert(30); // s1.insert(40); // s1.insert(20); // s1.insert(50); // // for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) // { // cout << *it << " "; // } // cout << endl; // // //指定排序规则 // set<int,Mycompare>s2; // // s2.insert(10); // s2.insert(30); // s2.insert(40); // s2.insert(20); // s2.insert(50); // // for (set<int,Mycompare>::iterator // = s2.begin(); dit != s2.end(); dit++) // { // cout << *dit << " "; // } // cout << endl; //} //int main() //{ // test01(); // return 0; //} //set自定义排序规则 //class Person //{ //public: // // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // string m_name; // int m_age; //}; // //class ComparePerson //{ //public: // bool operator()(const Person& p1, const Person& p2)const // { // //按照年龄降序 // return p1.m_age > p2.m_age; // } //}; //void test01() //{ // //自定义的数据类型 都会指定排序规则 // set<Person, ComparePerson>s; // // //创建Person对象 // Person p1("刘备", 24); // Person p2("关羽", 25); // Person p3("张飞", 26); // Person p4("赵云", 28); // Person p5("马超", 14); // // s.insert(p1); // s.insert(p2); // s.insert(p3); // s.insert(p4); // s.insert(p5); // // for (set<Person,ComparePerson>::iterator it = s.begin(); it != s.end(); it++) // { // cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl; // } //} //int main() //{ // test01(); // return 0; //} //map/multimap容器 //map中所有的元素都是pair //pair中第一个元素为key(键值),起到索引的作用、第二个元素为value(实值) //所有的元素都会按照key值排序 //本质:关联式容器 底层结构用二叉树实现 //二者区别:map中不允许有重复的key值 multimap中可以有重复 #include<map> //void printMap(map<int,int>& m) //{ // for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) // { // cout << "key值=" << (*it).first << " value=" << (*it).second << endl; // } // //} //void test01() //{ // //创建map容器 // map<int, int>m; // // m.insert(pair<int, int>(1, 10)); // m.insert(pair<int, int>(2, 20)); // m.insert(pair<int, int>(5, 30)); // m.insert(pair<int, int>(4, 40)); // m.insert(pair<int, int>(3, 50)); // // printMap(m);//按照key值自动排序 // // //拷贝构造 // map<int, int>m2(m); // printMap(m2);//按照key值自动排序 // // //赋值 // map<int, int>m3; // m3 = m2; // printMap(m3);//按照key值自动排序 // //} //int main() //{ // test01(); // return 0; //} //Map容器的大小和交换 //void printMap(map<int, int>& m) //{ // for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) // { // cout << "key值=" << (*it).first << " value=" << (*it).second << endl; // } // //} //void test01() //{ // //创建map容器 // map<int, int>m; // // m.insert(pair<int, int>(1, 10)); // m.insert(pair<int, int>(2, 20)); // m.insert(pair<int, int>(5, 30)); // // if (m.empty()) // { // cout << "map容器为空" << endl; // } // else // { // cout << "map容器不为空" << endl; // } // cout << "m的大小为:" << m.size() << endl; // // map<int, int>m2; // m2.insert(pair<int, int>(2, 100)); // m2.insert(pair<int, int>(4, 200)); // m2.insert(pair<int, int>(6, 300)); // printMap(m);//按照key值自动排序 // cout << "---------------------------" << endl; // printMap(m2);//按照key值自动排序 // // m2.swap(m); // cout << "---------------------------" << endl; // printMap(m);//按照key值自动排序 // cout << "---------------------------" << endl; // printMap(m2);//按照key值自动排序 // //} //int main() //{ // test01(); // return 0; //} //map的元素插入和删除 //void printMap(map<int, int>& m) //{ // for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) // { // cout << "key值=" << it->first << " value=" << (*it).second << endl; // } //} //void test01() //{ // //创建map容器 // map<int, int>m; // // //第一种 // m.insert(pair<int, int>(1, 10)); // //第二种 // m.insert(make_pair(2, 20)); // //第三种 // m.insert(map<int, int>::value_type(3, 30)); // //第四种 // m[4] = 40;//(不建议用) // // printMap(m); // cout << endl; // //删除 // m.erase(m.begin()); // printMap(m); // cout << endl; // m.erase(3);//按照key删除 // printMap(m); // // m.erase(m.begin(), m.end()); // printMap(m); // // m.clear(); // printMap(m); //} //int main() //{ // test01(); // return 0; //} //map的查找和统计 //void printMap(map<int, int>& m) //{ // for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) // { // cout << "key值=" << it->first << " value=" << (*it).second << endl; // } //} //void test01() //{ // //创建map容器 // map<int, int>m; // // m.insert(pair<int, int>(1, 10)); // m.insert(pair<int, int>(2, 20)); // m.insert(pair<int, int>(3, 30)); // m.insert(pair<int, int>(3, 50));//map中不允许插入重复的key值 // m.insert(pair<int, int>(4, 40)); // // //查找 // map<int, int>::iterator pos = m.find(3); // // if (pos != m.end()) // { // cout << "查到了元素key=" << (*pos).first << " value=" << pos->second << endl; // } // else // { // cout << "为找到元素" << endl; // } // // //统计 // int num = m.count(3); // cout << "num=" << num << endl; // //} //int main() //{ // test01(); // return 0; //} //map容器排序 //class MyCompare //{ //public: // bool operator()(int a, int b)const // { // //降序 // return a > b; // } //}; // //void test01() //{ // //创建map容器 // map<int, int,MyCompare>m; // // m.insert(pair<int, int>(1, 10)); // m.insert(pair<int, int>(3, 20)); // m.insert(pair<int, int>(2, 30)); // m.insert(pair<int, int>(5, 50));//map中不允许插入重复的key值 // m.insert(pair<int, int>(4, 40)); // // for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) // { // cout << "key值=" << it->first << " value=" << (*it).second << endl; // } //} //int main() //{ // test01(); // return 0; //} //员工分组的案例 //#define CEHUA 0 //#define MEISHU 1 //#define YANFA 2 // //class Worker //{ //public: // // string m_name; // int m_salary; //}; // //void creatWorker(vector<Worker>&v) //{ // string nameSeed = "ABCDEFGHIJ"; // for (int i = 0; i < 10; i++) // { // Worker worker; // worker.m_name = "员工"; // // worker.m_name += nameSeed[i]; // // worker.m_salary = rand() % 10000 + 10000;//10000-19999 // // //将员工放入容器中 // v.push_back(worker); // } //} // //void setGroup(vector<Worker>& v, multimap<int, Worker>& m) //{ // for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++) // { // //产生随机部门编号 // int deptId = rand() % 3;//0 1 2 // // //将员工插入到分组中 // //key部门编号 ,value具体的员工 // m.insert(make_pair(deptId, *it)); // } //} // //void showWorkerByGroup(multimap<int, Worker>& m) //{ // //0 A B C 1 D E 2 F G... // cout << "策划部门的信息:" << endl; // // multimap<int, Worker>::iterator pos = m.find(CEHUA); // // int count = m.count(CEHUA);//统计具体人数 // // int index = 0; // for (; pos != m.end() && index < count; pos++, index++) // { // cout << "姓名:" << pos->second.m_name << " 工资:" << pos->second.m_salary << endl; // } // // cout << "--------------------------------------" << endl; // cout << "美术部门:" << endl; // pos = m.find(MEISHU); // count = m.count(MEISHU);//统计具体人数 // index = 0; // for (; pos != m.end() && index < count; pos++, index++) // { // cout << "姓名:" << pos->second.m_name << " 工资:" << pos->second.m_salary << endl; // } // cout << "--------------------------------------" << endl; // cout << "研发部门:" << endl; // pos = m.find(YANFA); // count = m.count(YANFA);//统计具体人数 // index = 0; // for (; pos != m.end() && index < count; pos++, index++) // { // cout << "姓名:" << pos->second.m_name << " 工资:" << pos->second.m_salary << endl; // } //} //int main() //{ // srand((unsigned int)time(NULL));//随机数种子 // // //创建员工 // vector<Worker>vWorker; // creatWorker(vWorker); // // //员工分组 // multimap<int, Worker>mWorker; // setGroup(vWorker,mWorker); // // //分组显示员工 // showWorkerByGroup(mWorker); // // return 0; //} //STL-函数对象 //重载函数调用操作符的类 //class MyAdd //{ //public: // int operator()(int a, int b) // { // return a + b; // } //}; 函数对象在使用时,像普通函数一样,可以有参数,也可以有返回值 //void test01() //{ // MyAdd myadd; // cout << myadd(10, 10) << endl; //} //函数对象超出普通函数的概念,函数对象可以有自己的状态 //class Myprint //{ //public: // Myprint() // { // this->count = 0; // } // void operator()(string test) // { // cout << test << endl; // this->count++; // } // int count;//内部自己的状态 // //}; //void test02() //{ // Myprint myprint; // myprint("hello world!"); // myprint("hello world!"); // myprint("hello world!"); // myprint("hello world!"); // myprint("hello world!"); // // cout << "myprint调用的次数:" << myprint.count << endl; //} // 函数对象可以作为参数传递 //void doPrint(Myprint& mp,string test) //{ // mp(test); //} //void test03() //{ // Myprint myprint; // doPrint(myprint, "Hello c++"); //} //int main() //{ // test01(); // test02(); // test03(); // // return 0; //} //谓词 //布尔类型的返回数 //operator()接受一个参数,叫做一元谓词 //class GreaterFive //{ //public: // bool operator()(int a) // { // return a > 5; // } //}; //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // //查找大于5的数字 // //GreaterFive()匿名的函数对象 // vector<int>::iterator it=find_if(v.begin(), v.end(), GreaterFive()); // // if (it == v.end()) // { // cout << "未找到!" << endl; // } // else // { // cout << "找到了大于5的数字为:" << *it << endl; // } //} //int main() //{ // test01(); // return 0; //} //operator()接受2个参数,叫做二元谓词 //class MyCompare //{ //public: // bool operator()(int a, int b) // { // return a > b; // } //}; //void test01() //{ // vector<int>v; // v.push_back(10); // v.push_back(40); // v.push_back(20); // v.push_back(30); // v.push_back(50); // // sort(v.begin(), v.end()); // // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; // 使用算法对象 改变算法策略,排序规则改为从大到小 // cout << "------------------------------------------" << endl; // sort(v.begin(), v.end(), MyCompare()); // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; //} //int main() //{ // test01(); // return 0; //} //内建函数对象 //算术仿函数-实现四则运算 #include<functional>//内建函数对象的头文件 //negate一元仿函数 取反仿函数 //void test01() //{ // negate<int>n;//取反仿函数 // cout << n(50) << endl; //} plus 二元仿函数 加法 //void test02() //{ // plus<int>p; // cout << p(10, 20) << endl; //} //int main() //{ // test01(); // test02(); // return 0; //} //关系仿函数 //大于 greater //class MyCompare //{ //public: // bool operator()(int a, int b) // { // return a > b; // } //}; //void test01() //{ // vector<int>v; // // v.push_back(10); // v.push_back(20); // v.push_back(30); // v.push_back(40); // v.push_back(50); // // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; // // //降序 // sort(v.begin(), v.end(), MyCompare()); // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; // // //greater<int>()内建函数对象 // sort(v.begin(), v.end(), greater<int>()); // for (vector<int>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //逻辑仿函数 //逻辑非 logical_not //void test01() //{ // vector<bool>v; // // v.push_back(true); // v.push_back(true); // v.push_back(true); // v.push_back(false); // v.push_back(false); // // for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) // { // cout << *it << " "; // } // cout << endl; // // //利用逻辑非 将容器v搬运到容器v2中,并执行取反的操作 // vector<bool>v2; // v2.resize(v.size());//开辟空间 // // transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());//搬运 // // for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) // { // cout << *it << " "; // } // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //STL-常用算法 //for_each 遍历容器 //普通函数 //void print01(int val) //{ // cout << val << " "; //} //class print02 //{ //public: // void operator()(int val) // { // cout << val << " "; // } // //}; //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // // for_each(v.begin(),v.end(),print01); // cout << endl; // // for_each(v.begin(), v.end(), print02()); // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //transform 搬运容器 //class Transform //{ //public: // int operator()(int v) // { // return v; // } //}; //class MyPrint //{ //public: // void operator()(int val) // { // cout << val << " "; // } //}; //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // vector<int>vTarget;//目标容器 // // vTarget.resize(v.size());//目标容器 需要提前开辟空间 否则无法搬运 // // transform(v.begin(), v.end(), vTarget.begin(), Transform()); // // for_each(vTarget.begin(), vTarget.end(), MyPrint()); //} //int main() //{ // test01(); // return 0; //} //常用查找算法 //find //查找内置的数据类型 //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // //查找容器中是否有5这个元素 // vector<int>::iterator it=find(v.begin(), v.end(), 5); // if (it == v.end()) // { // cout << "没有找到!" << endl; // } // else // { // cout << "找到了:"<<*it << endl; // } //} //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // //重载==底层find知道如何对比Person数据类型 // bool operator==(const Person& p) // { // if (this->m_name == p.m_name && this->m_age == p.m_age) // { // return true; // } // else // { // return false; // } // } // string m_name; // int m_age; //}; 查找自定义的数据类型 //void test02() //{ // vector<Person>v; // //创建数据 // Person p1("aaa", 10); // Person p2("bbb", 20); // Person p3("ccc", 30); // Person p4("ddd", 40); // Person p5("eee", 50); // // //放到容器中 // v.push_back(p1); // v.push_back(p2); // v.push_back(p3); // v.push_back(p4); // v.push_back(p5); // // Person pp("bbb", 20); // vector<Person>::iterator it = find(v.begin(), v.end(), pp); // if(it==v.end()) // { // cout << "没有找到!" << endl; // } // else // { // cout << "找到姓名:" << it->m_name << " 年龄:" << it->m_age << endl; // } //} //int main() //{ // test01(); // test02(); // // return 0; //} //常用查找算法find_if //查找内置数据类型 //class Greater5 //{ //public: // bool operator()(int val) // { // return val > 5; // } //}; //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // vector<int>::iterator it = find_if(v.begin(), v.end(), Greater5()); // // if (it == v.end()) // { // cout << "没有找到!" << endl; // } // else // { // cout << "找到了大于5的数字:" << *it << endl; // } //} //查找自定义数据类型 //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // string m_name; // int m_age; //}; //class Greater20 //{ //public: // bool operator()(Person& p) // { // return p.m_age > 20; // } // //}; //void test02() //{ // vector<Person>v; // // Person p1("AA", 10); // Person p2("BB", 22); // Person p3("CC", 30); // // v.push_back(p1); // v.push_back(p2); // v.push_back(p3); // // vector<Person>::iterator it=find_if(v.begin(), v.end(), Greater20()); // for (; it != v.end(); it++) // { // cout << "找到姓名:" << it->m_name << " 找到年龄:" << it->m_age << endl; // } //} //int main() //{ // test01(); // test02(); // // return 0; //} //常用查找算法 adjacement_find //adjacent_find-查找相邻的重复元素 //void test01() //{ // vector<int>v; // v.push_back(0); // v.push_back(2); // v.push_back(0); // v.push_back(3); // v.push_back(0); // v.push_back(1); // v.push_back(4); // v.push_back(3); // v.push_back(3); // // vector<int>::iterator pos=adjacent_find(v.begin(), v.end()); // // if (pos == v.end()) // { // cout << "未找到相邻的重复元素" << endl; // } // else // { // cout << "找到相邻元素:" << *pos << endl; // } //} //int main() //{ // test01(); // return 0; //} //binary_search-二分查找法(必须是有序序列) //查找指定元素,查到返回true或false //void test01() //{ // vector<int>v; // for (int i = 0; i < 10; i++) // { // v.push_back(i); // } // //v.push_back(2); 如果是无序序列 结果未知 // //查找容器中是否有9 // bool ret=binary_search(v.begin(), v.end(), 9); // if (ret) // { // cout << "找到了元素!" << endl; // } // else // { // cout << "未找到元素!" << endl; // } // //} //int main() //{ // test01(); // return 0; //} //统计元素个数 //统计内置数据类型 //void test01() //{ // vector<int>v; // // v.push_back(10); // v.push_back(40); // v.push_back(30); // v.push_back(30); // v.push_back(20); // v.push_back(50); // v.push_back(10); // // int num=count(v.begin(), v.end(),40); // // cout << "num=" << num << endl; //} 统计自定义的数据类型 //class Person //{ //public: // Person(string name,int age) // { // this->m_name = name; // this->m_age = age; // } // //重载== // bool operator==(const Person& p) // { // if (this->m_age == p.m_age) // { // return true; // } // else // { // return false; // } // } // string m_name; // int m_age; //}; //void test02() //{ // vector<Person>v; // // Person p1("刘备", 10); // Person p2("关羽", 20); // Person p3("张飞", 40); // Person p4("赵云", 50); // Person p5("黄忠", 30); // Person p6("马超", 10); // // //将人员插入到容器中 // v.push_back(p1); // v.push_back(p2); // v.push_back(p3); // v.push_back(p4); // v.push_back(p5); // v.push_back(p6); // // Person p("诸葛亮", 10); // // int num = count(v.begin(), v.end(), p); // cout << "和诸葛亮同岁的人有:" << num<<"个" << endl; //} //int main() //{ // test01(); // test02(); // // return 0; //} //count_if 按条件统计元素个数 //class Greater20 //{ //public: // bool operator()(int val) // { // return val>20;//返回大于20的数 // } //}; // 统计内置的数据类型 //void test01() //{ // vector<int>v; // v.push_back(10); // v.push_back(20); // v.push_back(30); // v.push_back(40); // v.push_back(30); // v.push_back(20); // v.push_back(10); // // int num=count_if(v.begin(), v.end(), Greater20());//容器中大于20的元素的个数 // cout << "大于20的元素个数为:" << num << endl; //} //class Person //{ //public: // Person(string name, int age) // { // this->m_name = name; // this->m_age = age; // } // string m_name; // int m_age; //}; // //class Greater20 //{ //public: // bool operator()(const Person& p) // { // return p.m_age > 20; // } //}; 统计自定义的数据类型 //void test02() //{ // vector<Person>v; // // Person p1("刘备", 35); // Person p2("关羽", 25); // Person p3("张飞", 15); // Person p4("赵云", 45); // Person p5("曹操", 55); // Person p6("孙权", 45); // // v.push_back(p1); // v.push_back(p2); // v.push_back(p3); // v.push_back(p4); // v.push_back(p5); // v.push_back(p6); // // //统计 大于20岁人员个数 // int num=count_if(v.begin(), v.end(), Greater20()); // // cout << "大于20岁的人员个数为:" << num << endl; //} //int main() //{ // //test01(); // test02(); // // return 0; //} //常用的排序算法 //sort //void MyPrint(int val) //{ // cout << val << " "; //} // //void test01() //{ // vector<int>v; // // v.push_back(10); // v.push_back(40); // v.push_back(30); // v.push_back(20); // v.push_back(50); // // //利用sort进行升序 // sort(v.begin(), v.end()); // // for_each(v.begin(), v.end(), MyPrint); // cout << endl; // // //改变为 降序 // sort(v.begin(), v.end(), greater<int>()); // for_each(v.begin(), v.end(), MyPrint); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //random_shuffle //指定范围内的元素随机打乱 //void MyPrint(int val) //{ // cout << val << " "; //} //void test01() //{ // srand((unsigned int)time(NULL)); // // vector<int>v; // // for (int i = 0; i < 10 ; i++) // { // v.push_back(i); // } // //利用洗牌 算法 打乱顺序 // random_shuffle(v.begin(), v.end()); // // for_each(v.begin(), v.end(), MyPrint); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //merge //容器元素合并,并储存到另一个容器中 //两个序列都必须是有序序列 //void Myprint(int val) //{ // cout << val << " "; //} //void test01() //{ // vector<int>v1; // vector<int>v2; // // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // v2.push_back(i+1); // } // // //目标容器 // vector<int>vTarget; // // vTarget.resize(v1.size() + v2.size()); // // merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); // // for_each(vTarget.begin(), vTarget.end(), Myprint); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //reverse-将容器内元素进行翻转 //void MyPrint(int val) //{ // cout << val << " "; //} //void test01() //{ // vector<int>v; // v.push_back(10); // v.push_back(30); // v.push_back(20); // v.push_back(40); // v.push_back(50); // v.push_back(70); // // cout << "翻转前:————————————————————" << endl; // for_each(v.begin(), v.end(), MyPrint); // cout << endl; // cout << "翻转后:————————————————————" << endl; // reverse(v.begin(), v.end()); // for_each(v.begin(), v.end(), MyPrint); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //常用拷贝和替换算法 //copy //void Myprint(int val) //{ // cout << val << " "; //} //void test01() //{ // vector<int>v1; // for (int i = 0; i < 10; i++) // { // v1.push_back(i); // } // vector<int>v2; // v2.resize(v1.size()); // // copy(v1.begin(), v1.end(), v2.begin()); // //v2 = v1; // for_each(v2.begin(), v2.end(), Myprint); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //replace //class myPrint //{ //public: // void operator()(int val) // { // cout << val << " "; // } //}; //void test01() //{ // vector<int>v; // v.push_back(10); // v.push_back(20); // v.push_back(30); // v.push_back(20); // v.push_back(10); // // cout << "替换前:" << "-----------------" << endl; // for_each(v.begin(), v.end(), myPrint()); // cout << endl; // // replace(v.begin(), v.end(), 20,2000);//将v中的20替换成2000 // cout << "替换后:" << "-----------------" << endl; // for_each(v.begin(), v.end(), myPrint()); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //replace_if //class myPrint //{ //public: // void operator()(int val) // { // cout << val << " "; // } //}; // //class Greater30 //{ //public: // bool operator()(int val) // { // return val >= 30; // } //}; //void test01() //{ // vector<int>v; // // v.push_back(10); // v.push_back(20); // v.push_back(30); // v.push_back(40); // v.push_back(50); // // cout << "替换前:--------------------------------" << endl; // for_each(v.begin(), v.end(), myPrint()); // cout << endl; // //将大于等于30的替换为3000 // replace_if(v.begin(), v.end(), Greater30(), 3000); // cout << "替换后:--------------------------------" << endl; // for_each(v.begin(), v.end(), myPrint()); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //swap //class myPrint //{ //public: // void operator()(int val) // { // cout << val << " "; // } //}; //void test01() //{ // vector<int>v1; // // v1.push_back(10); // v1.push_back(20); // v1.push_back(30); // v1.push_back(40); // v1.push_back(50); // // vector<int>v2; // // v2.push_back(100); // v2.push_back(200); // v2.push_back(300); // v2.push_back(400); // v2.push_back(500); // v2.push_back(600); // // cout << "交换前:------------------" << endl; // for_each(v1.begin(), v1.end(), myPrint()); // cout << endl; // for_each(v2.begin(), v2.end(), myPrint()); // cout << endl; // // cout << "交换后:------------------" << endl; // swap(v1, v2); // // for_each(v1.begin(), v1.end(), myPrint()); // cout << endl; // for_each(v2.begin(), v2.end(), myPrint()); // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //算术生成算法 //accumulate/fill //accumlate #include<numeric> //void test01() //{ // vector<int>v; // // for (int i = 0; i <= 100; i++) // { // v.push_back(i); // } // int sum=accumulate(v.begin(),v.end(),1000);//参数3是一个起始的累加值 // // cout << "sum=" << sum << endl; //} //int main() //{ // test01(); // return 0; //} //fill //class myPrint //{ //public: // void operator()(int val) // { // cout << val << " "; // } //}; //void test01() //{ // vector<int>v; // // v.resize(10); // // fill(v.begin(), v.end(), 100);//后期重新填充 // // for_each(v.begin(), v.end(), myPrint()); // cout << endl; //} //int main() //{ // test01(); // return 0; //} //常用集合算法 // //获取交集 //set_intersection //void myPrint(int val) //{ // cout << val << " "; //} //void test01() //{ // vector<int>v1; // vector<int>v2; // //求交集的两个集合必须是有序序列 // for (int i = 0; i < 10; i++) // { // v1.push_back(i);//0-9 // v2.push_back(i+5);//5-14 // } // // vector<int>vTarget; // //目标容器需要提前开辟空间 // //两个容器包含小容器 // vTarget.resize(min(v1.size(),v2.size())); // // //获取交集 // vector<int>::iterator itEnd=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),vTarget.begin()); // // for_each(vTarget.begin(), itEnd, myPrint); // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //set_union //获取并集 //void myPrint(int val) //{ // cout << val << " "; //} //void test01() //{ // vector<int>v1; // vector<int>v2; // //求交集的两个集合必须是有序序列 // for (int i = 0; i < 10; i++) // { // v1.push_back(i);//0-9 // v2.push_back(i + 5);//5-14 // } // // vector<int>vTarget; // //目标容器需要提前开辟空间 // // vTarget.resize(v1.size()+v2.size()); // // //获取并集 // vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); // // for_each(vTarget.begin(),itEnd, myPrint); // cout << endl; // //} //int main() //{ // test01(); // return 0; //} //set_difference //求两个集合的差集 void myPrint(int val) { cout << val << " "; } void test01() { vector<int>v1; vector<int>v2; //求交集的两个集合必须是有序序列 for (int i = 0; i < 10; i++) { v1.push_back(i);//0-9 v2.push_back(i + 5);//5-14 } vector<int>vTarget; //目标容器需要提前开辟空间 vTarget.resize(max(v1.size(), v2.size())); vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin()); for_each(vTarget.begin(), itEnd, myPrint); cout << endl; } int main() { test01(); return 0; } //strcasecmp函数 // //函数功能 :比较参数s1和s2字符串,比较时会自动忽略大小写的差异。 // //返回值: 若参数s1和s2字符串相等则返回0。s1大于s2则返回大于0 的值,s1 小于s2 则返回小于0的值。 //lower_bound()和upper_bound()都是利用二分查找的方法在一个排好序的数组中进行查找的。 // //在从小到大的排序数组中, // //lower_bound(begin, end, num):从数组的begin位置到end - 1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin, 得到找到数字在数组中的下标。 // //upper_bound(begin, end, num):从数组的begin位置到end - 1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin, 得到找到数字在数组中的下标。 // //在从大到小的排序数组中,重载lower_bound()和upper_bound() // //lower_bound(begin, end, num, greater<type>()) :从数组的begin位置到end - 1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin, 得到找到数字在数组中的下标。 // //upper_bound(begin, end, num, greater<type>()) : 从数组的begin位置到end - 1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin, 得到找到数字在数组中的下标。 #include<iostream> using namespace std; #include<string> //多继承语法 //class Base1 //{ //public: // Base1() // { // m_a = 100; // } // int m_a; //}; //class Base2 //{ //public: // Base2() // { // m_a = 200; // } // int m_a; //}; 子类 需要继承Base1和Base2 语法:class子类:继承方式 父类1,继承方式 父类2... //class Son :public Base1, public Base2 //{ //public: // Son() // { // m_c = 300; // m_d = 400; // } // int m_c; // int m_d; //}; //void test01() //{ // Son s; // cout << "size of s=" << sizeof(s) << endl; // cout << "Base1下的m_a=" << s.Base1::m_a << endl; // cout << "Base2下的m_a=" << s.Base2::m_a << endl; // //} //int main() //{ // test01(); // return 0; //} //菱形继承 //动物类 //class Animal //{ //public: // int m_age; //}; 利用虚继承 解决菱形继承的问题 继承之前 加上关键字 virtual变为虚继承 Animal类成为 虚基类 羊类 //class Sheep:public virtual Animal{}; // 驼类 //class cmcel:public virtual Animal{}; // 羊驼类 //class Shcel :public Sheep, public cmcel{}; //void test01() //{ // Shcel st; // st.Sheep::m_age = 18; // st.cmcel::m_age = 28; // //当菱形继承,两个父类拥有相同数据 需要加以作用域区分 // cout << "st.Sheep::m_age =" << st.Sheep::m_age << endl; // cout << "st.cmcel::m_age =" << st.cmcel::m_age << endl; // cout << "m_age =" << st.m_age << endl; //} //int main() //{ // test01(); // return 0; //} //多态 //静态多态 // 函数重载和运算符重载(编译阶段确定函数地址) //动态多态:1.有继承关系 2.子类重写父类的虚函数 //派生类和虚函数实现运行时多态(运行阶段确定函数地址) //动物类 //class Animal //{ //public: // virtual void speak() // { // cout << "动物在说话" << endl; // } //}; // 猫类 //class cat :public Animal //{ //public: // //重写 函数返回值类型 函数名 参数列表 完全相同 // void speak() // { // cout << "小猫在说话" << endl; // } //}; // 狗类 //class dog :public Animal //{ //public: // void speak() // { // cout << "狗在说话" << endl; // } //}; 执行说话的函数 地址早绑定 在编译阶段确定函数地址 如果想执行让猫说话,那么这个函数地址就不能提前绑定,需要在运行阶段进行绑定,地址晚绑定 动态多态: //void dospeak(Animal& animal)//Animal & animal=cat; //{ // //父类的指针或引用 执行子类对象 // animal.speak(); //} //void test01() //{ // cat c; // dospeak(c); //} //void test02() //{ // dog d; // dospeak(d); //} //int main() //{ // test01(); // test02(); // return 0; //} //多态案例-计算器类 //利用多态实现计算器 //多态的好处 1.结构清晰 2.可读性强 3.对于扩展和维护性高 //class abstractcalculator //{ //public: // virtual int getresult() // { // return 0; // } // int m_num1; // int m_num2; //}; 加法计算器类 //class addcalculator :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 + m_num2; // } //}; 减法计算器类 //class subcalculator :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 - m_num2; // } //}; 乘法计算器类 //class mulcalculator :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 * m_num2; // } //}; 除法计算器类 //class divcalcultor :public abstractcalculator //{ //public: // int getresult() // { // return m_num1 / m_num2; // } //}; //void test01() //{ // //多态使用条件 // //父类指针或者引用指向子类对象 // //加法运算 // abstractcalculator* abc = new addcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "+" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // //减法运算 // abc = new subcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "-" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // // //乘法运算 // abc = new mulcalculator; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "*" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; // // //除法运算 // abc = new divcalcultor; // abc->m_num1 = 10; // abc->m_num2 = 10; // cout << abc->m_num1 << "/" << abc->m_num2 << "=" << abc->getresult() << endl; // //用完记得销毁 // delete abc; //} //int main() //{ // test01(); // return 0; //} //纯虚函数和抽象类 //纯虚函数语法 //virtual 返回值类型 函数名 (参数列表)=0; //当类中有了纯虚函数 这个类也称为抽象类 //class Base //{ //public: // virtual void func() = 0;//纯虚函数 // //只要有一个纯虚函数,这个类成为抽象类 //}; //class Son:public Base //{ //public: // virtual void func() // { // cout << "func的调用" << endl; // }; //}; //void test01() //{ 1.抽象类无法实例化对象 // /*Base b; // new Base;*/ 2.抽象类的子类必须重写父类中的纯虚函数,否则也属于抽象类 // //Son s;//子类必须重写父类中的纯虚函数,否则无法实例化对象 // Base* base = new Son; // base->func(); //} //int main() //{ // test01(); // return 0; //} //多态案例2-制作饮品 //class abstractdrinking //{ //public: // //煮水 // virtual void boil() = 0; // //冲泡 // virtual void brew() = 0; // //倒杯 // virtual void pourIncup() = 0; // //加入辅料 // virtual void putSomething() = 0; // //制作饮品 // virtual void makedrink() // { // boil(); // brew(); // pourIncup(); // putSomething(); // } //}; // 制作咖啡 //class coffee :public abstractdrinking //{ //public: // //煮水 // virtual void boil() // { // cout << "煮农夫山泉" << endl; // } // // //冲泡 // virtual void brew() // { // cout << "冲泡咖啡" << endl; // } // //倒入杯中 // virtual void pourIncup() // { // cout << "倒入杯中" << endl; // } // //加入辅料 // virtual void putSomething() // { // cout << "加入糖奶" << endl; // } //}; // //class tea :public abstractdrinking //{ //public: // //煮水 // virtual void boil() // { // cout << "煮矿泉水" << endl; // } // // //冲泡 // virtual void brew() // { // cout << "冲泡茶叶" << endl; // } // //倒入杯中 // virtual void pourIncup() // { // cout << "倒入茶杯中" << endl; // } // //加入辅料 // virtual void putSomething() // { // cout << "加入茶叶" << endl; // } //}; 制作函数 //void dowork(abstractdrinking*abs) //{ // abs->makedrink(); // delete abs;//手动释放 //} //void test01() //{ // //制作咖啡 // dowork(new coffee); // cout << "-----------------------------" << endl; // //制作茶 // dowork(new tea); //} //int main() //{ // test01(); // return 0; //} //虚析构和纯虚析构 //class Animal //{ //public: // Animal() // { // cout << "Animal的构造函数调用" << endl; // } // //利用虚析构可以解决 父类指针释放子类对象时不干净的问题 // //virtual ~Animal() // //{ // // cout << "Animal的虚析构函数的调用" << endl; // //} // // // //纯虚析构(需要声明也需要实现) // //有了纯虚析构之后 这个类也属于抽象类 无法实例化对象 // virtual ~Animal() = 0; // //纯虚函数 // virtual void speak() = 0; //}; //Animal::~Animal() //{ // cout << "Animal纯虚析构函数调用" << endl; //} // //class Cat :public Animal //{ //public: // Cat(string name) // { // m_name=new string(name); // } // virtual void speak() // { // cout <<*m_name<<"小猫在说话" << endl; // } // ~Cat() // { // if (m_name != NULL) // { // cout << "Cat的析构函数调用" << endl; // delete m_name; // m_name = NULL; // } // } // string *m_name; //}; // //void test01() //{ // Animal* animal = new Cat("Tom"); // animal->speak(); // //父类的指针在析构的时候 不会调用子类的析构函数 // //导致子类如果有堆区数据,会出现内存的泄露情况 // delete animal; //} //int main() //{ // test01(); // return 0; //}