当前位置:网站首页 > 技术博客 > 正文

swap函数交换数组元素



 1 #include<iostream>  2 #include<string>  3 #include<algorithm>//sort函数包含的头文件  4 using namespace std;  5 //定义一个学生类型的结构体  6 typedef struct student  7 {  8 string name; //学生姓名  9 int achievement; //学生成绩 10 } student; 11 12 13 14 15 //用来显示学生信息的函数 16 void show(student *stu,int n) 17 { 18 for(int i = 0; i < n; i++) 19  { 20 cout<<"姓名:"<<stu[i].name<<' '<<"成绩:"<<stu[i].achievement<<endl; 21  } 22 } 23 24 int main() 25 { 26 student stu[] = { {"张三",99},{"李四",87},{"王二",100} ,{"麻子",60}}; 27 cout<<"交换前:"<<endl; 28 show(stu,4); 29 swap(stu[0],stu[3]); 30 cout<<"交换后:"<<endl; 31 show(stu,4); 32 return 0; 33 }

版权声明


相关文章:

  • pymol使用教程实际例2025-05-15 22:01:06
  • springboot h2数据库2025-05-15 22:01:06
  • sp.socrcv指令说明2025-05-15 22:01:06
  • 记录鼠标键盘操作的软件2025-05-15 22:01:06
  • 命令模式定义2025-05-15 22:01:06
  • idea打包jar包含依赖2025-05-15 22:01:06
  • 文件对比工具 beyond compare2025-05-15 22:01:06
  • sql编程实例2025-05-15 22:01:06
  • 最大似然估计求解步骤2025-05-15 22:01:06
  • c语言中memset函数的用法2025-05-15 22:01:06