cpp: pointer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | // ConsoleStructSimpleApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <fstream>//文件操作 #include <sstream>//int转string #include <iomanip>//cout格式化输出 setw() #include <stdlib.h> #include "GeovinDu.h" using namespace std; using namespace DuStructSimple; //#pragma pack(2) /// <summary> /// /// </summary> enum weekdayname { /// <summary> /// /// </summary> SUN=1, /// <summary> /// /// </summary> MON=2, /// <summary> /// /// </summary> TUE=3, /// <summary> /// /// </summary> WED=4, /// <summary> /// /// </summary> THU=5, /// <summary> /// /// </summary> FRI=6, /// <summary> /// /// </summary> SAT=7 }; /// <summary> /// /// </summary> enum color { red = 5, blue, green }; union person { int age; char name[256]; char sex[2]; }; union person2 { int age; char name; char sex; }du; void func( int ** pp) { *pp= new int (3); //*pp = 40; cout << "pp=" << pp << ",*pp=" << *pp << ",**pp=" << **pp<< endl; } void func2( int * pp) { *pp = 3; cout << "pp2=" << pp << ",*pp2=" << *pp << endl; } int main() { int * dup=0; func(&dup); cout << "p==" << dup << ",*p==" << *dup << endl; //2 cout << "**************2*************" << endl; int dup2 = 1; func2(&dup2); cout << "p2==" << dup << ",&p2==" << &dup << endl; int a = 10; cout << "&a=" << &a << endl; int * p = &a; cout << "p=" << p << ",&p=" <<&p<< ",*p=" <<*p << endl; int ** j = &p; cout << "j=" << j << ",&j=" << &j << endl; cout << "*j=" << *j << endl; cout<< ",**j=" <<**j<< endl; //person per; //per.age = 30; ////per.name = "g"; ////per.sex = "m"; //cout << "person内存:" << sizeof(person) << endl; //cout << "person2 内存:" << sizeof(person2) << endl; //cout << "person sex内存 " << sizeof(per.sex) << endl; //cout << "person2 sex内存 " << sizeof(du.sex) << endl; ////地址相同: //cout << "per.name=" << &per.name << endl; //cout << "per.age=" << &per.age << endl; //cout << "per=" << &per << endl; // std::cout << "Hello World!涂聚文 Geovin Du\n"; //int num; //cout << "输入5-7(红,蓝,绿):" << endl; //cin >> num; //switch (num) //{ //case 5: // cout << "红色" << endl; // break; //case 6: // cout << "蓝色" << endl; // break; //case 7: // cout << "绿色" << endl; // break; //default: // cout << "错误!" << endl; // break; //} //cout << "枚举类型:" << endl; //cout << SUN <<","<<MON<<","<<TUE<<"," << WED <<","<<THU<<","<<FRI<<","<<SAT<< endl; //enum weekdayname day; //day = FRI; //cout << day << endl; //GeovinDu geovin; // //geovin.DisplayStructSize(); //geovin.displayStudent(); // 无用 //geovin.dispalyStructDemo(); // // //geovin.dsipalyFuntion(); //geovin.dipslayStructCustomer(); system ( "pause" ); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | // ConsoleStructSimpleApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <fstream>//文件操作 #include <sstream>//int转string #include <iomanip>//cout格式化输出 setw() #include <stdlib.h> #include "GeovinDu.h" using namespace std; using namespace DuStructSimple; //#pragma pack(2) /// <summary> /// /// </summary> enum weekdayname { /// <summary> /// /// </summary> SUN=1, /// <summary> /// /// </summary> MON=2, /// <summary> /// /// </summary> TUE=3, /// <summary> /// /// </summary> WED=4, /// <summary> /// /// </summary> THU=5, /// <summary> /// /// </summary> FRI=6, /// <summary> /// /// </summary> SAT=7 }; /// <summary> /// /// </summary> enum color { red = 5, blue, green }; union person { int age; char name[256]; char sex[2]; }; union person2 { int age; char name; char sex; }du; /// <summary> /// 二级指针 /// </summary> /// <param name="pp">形参</param> void func( int ** pp) { int * p = 0; int ** dup = &p; *dup = new int (3); cout << "&pp=" << &pp << endl; cout << "*pp=" << *pp << endl; cout << "**pp=" << **pp << endl; *pp= new int (3); //*pp = 40; cout << "pp=" << pp << ",*pp=" << *pp << ",**pp=" << **pp<< ",&pp=" <<&pp<< endl; //*pp指向p, pp指向new, &pp指向自已 } /// <summary> /// /// </summary> /// <param name="pp"></param> void func2( int * pp) { *pp = 3; cout << "pp2=" << pp << ",*pp2=" << *pp << endl; } int main() { //int* dup=0; //func(&dup); //cout << "p==" << dup << ",*p==" << *dup << endl; //2 cout << "**************2*************" << endl; int dup2 = 10; cout << "dup2=" << &dup2 << endl; int * dupp = &dup2; cout << "dupp=" << &dupp << endl; func(&dupp); cout << "p2==" << dupp << ",&p2==" << &dupp << ",*dupp=" <<*dupp << endl; //3 cout << "3***********" << endl; int kk = 20; int * dukk = &kk; func2(dukk); cout << "4************" << endl; int a = 10; cout << "&a=" << &a << endl; int * p = &a; cout << "p=" << p << ",&p=" <<&p<< ",*p=" <<*p << endl; int ** j = &p; cout << "j=" << j << ",&j=" << &j << endl; cout << "*j=" << *j << endl; cout<< ",**j=" <<**j<< endl; //person per; //per.age = 30; ////per.name = "g"; ////per.sex = "m"; //cout << "person内存:" << sizeof(person) << endl; //cout << "person2 内存:" << sizeof(person2) << endl; //cout << "person sex内存 " << sizeof(per.sex) << endl; //cout << "person2 sex内存 " << sizeof(du.sex) << endl; ////地址相同: //cout << "per.name=" << &per.name << endl; //cout << "per.age=" << &per.age << endl; //cout << "per=" << &per << endl; // std::cout << "Hello World!涂聚文 Geovin Du\n"; //int num; //cout << "输入5-7(红,蓝,绿):" << endl; //cin >> num; //switch (num) //{ //case 5: // cout << "红色" << endl; // break; //case 6: // cout << "蓝色" << endl; // break; //case 7: // cout << "绿色" << endl; // break; //default: // cout << "错误!" << endl; // break; //} //cout << "枚举类型:" << endl; //cout << SUN <<","<<MON<<","<<TUE<<"," << WED <<","<<THU<<","<<FRI<<","<<SAT<< endl; //enum weekdayname day; //day = FRI; //cout << day << endl; //GeovinDu geovin; // //geovin.DisplayStructSize(); //geovin.displayStudent(); // 无用 //geovin.dispalyStructDemo(); // // //geovin.dsipalyFuntion(); //geovin.dipslayStructCustomer(); system ( "pause" ); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /// <summary> /// 指针 /// </summary> /// <param name="pp">形参</param> void func3( int * pp) { int * p = 0; int ** dup = &p; *dup = new int (3); cout << "&pp=" << &pp << endl; cout << "*pp=" << *pp << endl; //cout << "**pp=" << **pp << endl; pp = new int (3); //*pp = new int(3); //*pp = 40; //cout << "pp=" << pp << ",*pp=" << *pp << ",**pp=" << **pp << ",&pp=" << &pp << endl; cout << "pp=" << pp << ",*pp=" << *pp << ",&pp=" << &pp << endl; //*pp指向p, pp指向new, &pp指向自已 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /// <summary> /// 二级指针 /// </summary> /// <param name="pp">形参</param> int func( int ** pp) { int * p = 0; int ** dup = &p; *dup = new int (3); cout << "&pp=" << &pp << endl; cout << "*pp=" << *pp << endl; cout << "**pp=" << **pp << endl; *pp= new int (3); //*pp = 40; cout << "pp=" << pp << ",*pp=" << *pp << ",**pp=" << **pp<< ",&pp=" <<&pp<< endl; //*pp指向p, pp指向new, &pp指向自已 return **pp; } /// <summary> /// 指针 /// </summary> /// <param name="pp">形参</param> int func3( int * pp) { int * p = 0; int ** dup = &p; *dup = new int (3); cout << "&pp=" << &pp << endl; cout << "*pp=" << *pp << endl; //cout << "**pp=" << **pp << endl; pp = new int (3); //*pp = new int(3); //*pp = 40; //cout << "pp=" << pp << ",*pp=" << *pp << ",**pp=" << **pp << ",&pp=" << &pp << endl; cout << "pp=" << pp << ",*pp=" << *pp << ",&pp=" << &pp << endl; //*pp指向p, pp指向new, &pp指向自已 return *pp; } /// <summary> /// /// </summary> /// <param name="pp"></param> int func2( int * pp) { *pp = 3; cout << "pp2=" << pp << ",*pp2=" << *pp << endl; return *pp; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | // ManageStudent.h: 此文件包含 "ManageStudent" 类。当作视图式结构体 策略模式 Strategy Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef HEROSIMPLE_H #define HEROSIMPLE_H #include<cstring> #include<stdbool.h> #include<stdlib.h> #include<iostream> #include<malloc.h> #include<cmath> #include <iostream> #include <sstream> #include <vector> #include <algorithm> #include <array> #include <functional> #include <list> #include <string> #include <string.h> using namespace std; namespace DuStructSimple { /// <summary> /// /// </summary> struct hero { /// <summary> /// /// </summary> string name; /// <summary> /// /// </summary> int age; /// <summary> /// /// </summary> float HealthPoint; /// <summary> /// /// </summary> long property; /// <summary> /// /// </summary> string sex; /// <summary> /// /// </summary> /// <param name="Obj"></param> /// <returns></returns> bool operator<( const hero& Obj) const { return HealthPoint < Obj.HealthPoint; } }; /// <summary> /// /// </summary> /// <param name="arr"></param> /// <param name="len"></param> void bubbleSort(hero arr[], int len) { for ( int i = 0; i < len - 1; i++) { for ( int j = 0; j < len - 1 - i; j++) { if (arr[j].age > arr[j + 1].age) { hero temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> bool compareTwohero(hero a, hero b) { // If total marks are not same then // returns true for higher total if (a.HealthPoint != b.HealthPoint) return a.HealthPoint > b.HealthPoint; // If marks in Maths are same then // returns true for higher marks if (a.property != b.property) return a.property > b.property; return (a.age > b.age); } } #endif /// <summary> /// 英雄显示示例 /// </summary> void GeovinDu::displayHero() { cout << "英雄年龄排序:" << endl; hero rosdu[5] = { { "赵二" ,23,100,50, "男" },{ "李三" ,28,105,70, "男" },{ "王四" ,26,120,40, "男" },{ "刘英" ,21,90,80, "女" },{ "何五" ,20,130,78, "男" } }; vector<hero> ros = { { "赵二" ,23,100,50, "男" },{ "李三" ,28,105,70, "男" },{ "王四" ,26,120,40, "男" },{ "刘英" ,21,90,80, "女" },{ "何五" ,20,130,78, "男" } }; bubbleSort(rosdu, 5); cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : rosdu) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age<< "\t" <<du.sex << endl; } cout << "英雄生命点排序:" << endl; sort(ros.begin(), ros.end(), compareTwohero); cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : ros) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age << "\t" << du.sex << endl; } cout << "英雄财点排序:" << endl; // Using lambda expressions in C++11 sort(ros.begin(), ros.end(), []( const hero& lhs, const hero& rhs) { return lhs.property < rhs.property; });; cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : ros) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age << "\t" << du.sex << endl; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | // hero.h: 此文件包含 "hero" 类。当作视图式结构体 策略模式 Strategy Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef HEROSIMPLE_H #define HEROSIMPLE_H #include<cstring> #include<stdbool.h> #include<stdlib.h> #include<iostream> #include<malloc.h> #include<cmath> #include <iostream> #include <sstream> #include <vector> #include <algorithm> #include <array> #include <functional> #include <list> #include <string> #include <string.h> using namespace std; namespace DuStructSimple { /// <summary> /// /// </summary> struct hero { /// <summary> /// /// </summary> string name; /// <summary> /// /// </summary> int age; /// <summary> /// /// </summary> float HealthPoint; /// <summary> /// /// </summary> long property; /// <summary> /// /// </summary> string sex; /// <summary> /// /// </summary> /// <param name="Obj"></param> /// <returns></returns> bool operator<( const hero& Obj) const { return HealthPoint < Obj.HealthPoint; } }; /// <summary> /// 年龄排序 /// </summary> /// <param name="arr"></param> /// <param name="len"></param> void bubbleAgeSort(hero arr[], int len) { for ( int i = 0; i < len - 1; i++) { for ( int j = 0; j < len - 1 - i; j++) { if (arr[j].age > arr[j + 1].age) { hero temp = arr[j]; //替换值 arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } /// <summary> /// 容器生命点排序 /// </summary> /// <param name="query"></param> void bubbleHealthPointSort(vector<hero>& query) { int n = query.size(); //; for ( int i = 0; i < n - 1; i++) { for ( int j = 0; j < n - i - 1; j++) { if (query[j].HealthPoint > query[j + 1].HealthPoint) { swap(query[j], query[j + 1]); } } } } /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> bool compareTwohero(hero a, hero b) { // If total marks are not same then // returns true for higher total if (a.HealthPoint != b.HealthPoint) return a.HealthPoint > b.HealthPoint; // If marks in Maths are same then // returns true for higher marks if (a.property != b.property) return a.property > b.property; return (a.age > b.age); } } #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /// <summary> /// 英雄显示示例 /// </summary> void GeovinDu::displayHero() { hero rosdu[5] = { { "赵二" ,23,100,50, "男" },{ "李三" ,28,105,70, "男" },{ "王四" ,26,120,40, "男" },{ "刘英" ,21,90,80, "女" },{ "何五" ,20,130,78, "男" } }; vector<hero> ros = { { "赵二" ,23,100,50, "男" },{ "Geovin Du" ,28,105,70, "男" },{ "涂聚文" ,26,120,40, "男" },{ "刘英" ,21,90,80, "女" },{ "何五" ,20,130,78, "男" } }; //1种冒泡排序 cout << "英雄年龄冒泡排序:" << endl; bubbleAgeSort(rosdu, 5); cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : rosdu) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age<< "\t" <<du.sex << endl; } cout << "英雄生命点排序:" << endl; //2种排序 sort(ros.begin(), ros.end(), compareTwohero); cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : ros) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age << "\t" << du.sex << endl; } cout << "英雄生命点容器冒泡排序:" << endl; //3容器冒泡种排序 bubbleHealthPointSort(ros); cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : ros) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age << "\t" << du.sex << endl; } cout << "英雄财点排序:" << endl; //4种排序 // Using lambda expressions in C++11 sort(ros.begin(), ros.end(), []( const hero& lhs, const hero& rhs) { return lhs.property < rhs.property; });; cout << "\t姓名" << "\t生命值" << "\t财点" << "\t年龄" << "\t性别" << endl; for ( auto du : ros) { cout << "\t" << du.name << "\t" << du.HealthPoint << "\t" << du.property << "\t" << du.age << "\t" << du.sex << endl; } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
指针
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2015-05-14 sql:SQL Server metadata queries