cpp: pointer
// 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 文件
// 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 文件
/// <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指向自已 }
/// <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; }
// 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; } }
// 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
/// <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(涂聚文)