zoukankan      html  css  js  c++  java
  • c++之结构体-结构数组排序

    结构体:

      结构体是一种特殊形态的类,与类的唯一区别是:类的缺省访问权限是private,而结构体的缺省访问权限是public。另外,结构体存在的主要原因就是与c语言保持兼容。

       什么时候用结构体,而不用类呢?主要用来保存数据。而没有什么操作的类型。

      人们通常将结构体的数据成员设为共有,因此这时使用结构体更加方便一些

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 struct Student{
     6     string name;
     7     long id;
     8     string sex;
     9     double score;
    10 };
    11 void main(){
    12     Student s;
    13     //访问结构体的数据成员
    14     s.name = "张三";
    15     s.sex = "";
    16     cout << s.name << "
    "<<s.sex << endl;
    17     //使用指针访问结构体的数据成员
    18     Student* ss;
    19     ss = &s;
    20     ss->score=99;
    21     ss->sex="";
    22     ss->name="李四";
    23     cout << ss->name << "
    " << ss->sex << "
    " << ss->score << endl;
    24     //结构体类型相同的两个变量,相互之间可以进行赋值
    25     Student s1;
    26     s1 = s;
    27     cout << s1.name << "
    " << s1.sex <<endl;
    28 }

     对结构数组以某一个成员作为关键字进行排序,下面提供了两种方法,一种是直接用数组访问的方式,一种是用指针间接访问的方式

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 //对结构数组以某一成员做关键字进行排序
     6 struct Person{        //结构定义
     7     string name;
     8     unsigned int id;
     9     double salary;        //结构成员声明
    10 };
    11 Person p[3];    //结构数组定义
    12 
    13 //使用指针的方法排序
    14 void main(){
    15     Person* pp[3];
    16     Person* temp;
    17     for(int i=0;i<3;i++){
    18         pp[i] = &p[i];
    19     }
    20     for(int i = 0;i<3;i++){
    21         cout << "请输入名字:";
    22         cin >> p[i].name;
    23         cout << "请输入ID:";
    24         cin >> p[i].id;
    25         cout << "请输入薪水:";
    26         cin >> p[i].salary;
    27     }
    28     cout << "After this sorting:" << endl;
    29     for(int i=1;i<3;i++){
    30         for(int j=0;j<3-i;j++){
    31             if(pp[j]->salary>pp[j+1]->salary){
    32                 temp = pp[j];
    33                 pp[j] = pp[j+1];
    34                 pp[j+1] = temp;
    35             }
    36         }
    37     }
    38     for(int i=0;i<3;i++){
    39         cout << "name:" << pp[i]->name << "	" << "id :" << pp[i]->id<< "	" << "salary:" << pp[i]->salary << endl;
    40     }
    41 }
    43 void main2(){ 44 Person temp; //结构变量定义 45 for(int i = 0;i<3;i++){ 46 cout << "请输入名字:"; 47 cin >> p[i].name; 48 cout << "请输入ID:"; 49 cin >> p[i].id; 50 cout << "请输入薪水:"; 51 cin >> p[i].salary; 52 } //输入数据 53 54 cout << "After this sorting:"; 55 56 for(int i=1;i<3;i++){ 57 for(int j = 0;j<3-i;j++){ 58 if(p[j].salary>p[j+1].salary){ 59 temp = p[j]; 60 p[j] = p[j+1]; 61 p[j+1] = temp; 62 } 63 } 64 } //使用冒泡排序,用salary的属性进行排序 65 for(int i=0;i<3;i++){ 66 cout << "name:" << p[i].name << " " << "id :" << p[i].id<< " " << "salary:" << p[i].salary << endl; 67 } //数据输出 68 }
  • 相关阅读:
    D2. Remove the Substring (hard version)(思维 )
    暑假集训
    AcWing:167. 木棒(dfs + 剪枝)
    AcWing:109. 天才ACM(倍增 + 归并排序)
    AcWing:99. 激光炸弹(前缀和)
    B. Interesting Array(线段树)
    Best Reward HDU
    G. Swapping Places
    How many HDU
    GSS4&&花仔游历各国
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4316742.html
Copyright © 2011-2022 走看看