目的:

定义5个学生,包含名字和分数,对成员进行从大到小排序,并输出

#include <iostream>
#include <cstring>
#include <vector>
#include <random>
#include <ctime>
#include <algorithm>
using namespace std;
//定义Student类,设置名字和分数属性
class Student{
public:
Student(int score,string name){
this->name = name;
this->score = score;
}
public:
int score;
string name;
};
//设置类成员
void SetStudent(vector<Student> &s){
string name[] ={"aa","bb","cc","dd","ee"};
Student a(,"");
srand(time());
for(int i =;i<;i++){
a.name = name[i];
a.score = +random()%;
s.push_back(a);
}
}
//定义针对Student类的排序规则函数
bool My_count(Student &v1,Student &v2){
return v1.score>v2.score;
}
//设置分数,对分数进行排名
void SetScore(vector<Student> &s){
sort(s.begin(),s.end(),My_count);//sort()支持随机存储容器,传入规则时,不带()
}
//利用迭代器对成员进行输出
void CoutScore(vector<Student> &s){
for(vector<Student>::iterator it = s.begin();it != s.end();it++){
cout <<"Student :"<<(*it).name <<" "<<"Score :"<<(*it).score<<endl;
}
} int main() {
vector<Student> s;
SetStudent(s);
SetScore(s);
CoutScore(s);
return ;
}

C++vector针对排序操作练习的更多相关文章

  1. [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

  2. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  3. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  4. Oracle 利用执行计划来避免排序操作

    在oracle中,利用index来避免排序 SQL) NOT NULL); SQL> CREATE INDEX IND_T_NOSORT_NAME ON T_NOSORT(NAME); SQL& ...

  5. C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique

    std::sort      对vector成员进行排序; std::sort(v.begin(),v.end(),compare);   std::lower_bound 在排序的vector中进行 ...

  6. Linq查询操作之排序操作

    在Linq中排序操作可以按照一个或多个关键字对序列进行排序.其中第一个排序关键字为主要关键字,第二个排序关键字为次要关键字.Linq排序操作共包含以下5个基本的操作. 1.OrderBy操作,根据排序 ...

  7. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  8. 1555: Inversion Sequence (通过逆序数复原序列 vector的骚操作!!!)

    1555: Inversion Sequence Submit Page    Summary    Time Limit: 2 Sec     Memory Limit: 256 Mb     Su ...

  9. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

随机推荐

  1. idea构建spark开发环境,并本地运行wordcount

    1.首先现在idea,官网:https://www.jetbrains.com/idea/ 2.安装jdk1.8,scala2.11 3.下载idea后,需要在idea中安装scala的插件,安装的方 ...

  2. MongoDB知识点拾遗梳理

    MongoDB数据库安装: >apt-get install mongodb 0.MongoDB状态查看.启动.停止       >/etc/init.d/mongodb status或s ...

  3. python命令行运行py文件找不到模块的解决办法

    问题: 新建了一个项目,目录结构如下: 然后在pycharm中运行glovar是没有问题的,但是在命令行中运行就会提示找不到init模块 这是因为在pycharm中运行的时候,pycharm会自动将项 ...

  4. C# 延迟函数

    #region 延迟函数 public static bool Delay(int delayTime) { DateTime now = DateTime.Now; int s; do { Time ...

  5. 集群web-ui

    一旦Hadoop集群启动并运行,可以通过web-ui进行集群查看,如下所述: 1.hdfs的集群端口号默认50070 NameNode  http://localhost1:50070(默认端口号po ...

  6. python--sort()和sorted()高级排序

    1.list中的sort()方法: def sort(self, key=None, reverse=False): # real signature unknown; restored from _ ...

  7. php 浏览记录功能

    /** * 历史浏览记录 * $data array 浏览记录里作品的必要信息 */ private function _history($data){ if(!$data || !is_array( ...

  8. 把一个给定的值存储到一个整数中指定的几个位《C与指针5.8.5》

    编写一个函数,把一个给定的值存储到一个整数中指定的几个位.它的原型如下: int store_bit_field(int original_value, int value_to_store, uns ...

  9. Visual C++ 6.0对任意三个数字进行排序

    # include <stdio.h> int main (void) { int a, b, c; int t; printf("请输入三个整数,中间以空格隔开:") ...

  10. python学习3---产生随机数

    1.产生随机数 import random #random.random()生成[0.0,1.0)之间的浮点数 print(random.random()) #random.randint(a,b)生 ...