C++ vector 排序
C++ vector 排序
C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结构体类型时,我们该怎样实现升序与降序排列呢?有两种方法,下面的例子能很好的说明: 方法1:
我们直接来看代码吧,比较简单,容易理解:
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct AssessTypeInfo
{
unsigned int m_uiType; //类型ID
char m_szName[64]; //类型名称
unsigned int m_uiTotal; //总分数
bool operator < (const AssessTypeInfo& rhs ) const //升序排序时必须写的函数
{
return m_uiType < rhs.m_uiType;
}
bool operator > (const AssessTypeInfo& rhs ) const //降序排序时必须写的函数
{
return m_uiType > rhs.m_uiType;
}
}
int main()
{
vector<AssessTypeInfo > ctn ;
AssessTypeInfo a1;
a1.m_uiType=1;
AssessTypeInfo a2;
a2.m_uiType=2;
AssessTypeInfo a3;
a3.m_uiType=3;
ctn.push_back(a1);
ctn.push_back(a2);
ctn.push_back(a3);
//升序排序
sort(ctn.begin(), ctn.end(),less<AssessTypeInfo>()) ; //或者sort(ctn.begin(), ctn.end()) 默认情况为升序
for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);
//降序排序
sort(ctn.begin(), ctn.end(),greater<AssessTypeInfo>()) ;
for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);
return 0 ;
}
以上方法就可以实现升序排序,输出结果为 1 2 3
降序排序结果3 2 1。
方法2 : 不修改结构体或类的定义部分,我们用函数对象来实现:
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct AssessTypeInfo
{
unsigned int m_uiType; //类型ID
char m_szName[64]; //类型名称
unsigned int m_uiTotal; //总分数
};
bool lessmark(const AssessTypeInfo& s1,const AssessTypeInfo& s2)
{
return s1.m_uiType < s2.m_uiType;
}
bool greatermark(const AssessTypeInfo& s1,const AssessTypeInfo& s2)
{
return s1.m_uiType > s2.m_uiType;
}
int main()
{
vector<AssessTypeInfo > ctn ;
AssessTypeInfo a1;
a1.m_uiType=1;
AssessTypeInfo a2;
a2.m_uiType=2;
AssessTypeInfo a3;
a3.m_uiType=3;
ctn.push_back(a1);
ctn.push_back(a2);
ctn.push_back(a3);
sort(ctn.begin(), ctn.end(),lessmark) ; //升序排序
for ( int i=0; i<3; i++ )
printf("%d\n",ctn[i].m_uiType);
sort(ctn.begin(), ctn.end(),greatermark) ; //降序排序
return 0 ;
}
C++ vector 排序的更多相关文章
- 1016. Phone Bills (25) -vector排序(sort函数)
题目如下: A long-distance telephone company charges its customers by the following rules: Making a long- ...
- 2.2 C语言_实现数据容器vector(排序功能)
上一节我们说到我们己经实现了一般Vector可以做到的自动扩充,告诉随机存取,那么现在我们需要完成vector的一个排序的功能. 排序算法我们网上一百度哇~~!很常见的就有8大排序算法: 1.选择排序 ...
- NX二次开发-C++的vector排序去重用法
#include <algorithm> //vector排序去重 sort( BoxNum.begin(), BoxNum.end()); BoxNum.erase(unique(Box ...
- STL之使用vector排序
应用场景: 在内存中维持一个有序的vector: // VectorSort.cpp : Defines the entry point for the console application. #i ...
- C++中的结构体vector排序
在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ...
- vector排序
// VectorSort.cpp : Defines the entry point for the console application. // #include "stdafx.h& ...
- java.util.Vector排序
Vector的排序: import java.util.*; class MyCompare implements Comparator //实现Comparator,定义自己的比较方法{public ...
- vector 排序
#include <vector> #include <algorithm> 一.vector保存的是基础数据类型(int.char.float等) vector<int ...
- C++标准库 vector排序
前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...
随机推荐
- “全栈2019”Java多线程第三十五章:如何获取线程被等待的时间?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- MariaDB 存储过程与函数(10)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- SpringBoot入门之内嵌Tomcat配置
spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默认为 / .需要用到的就是端口.上下文路径的修改,在spring boot中其修改方 ...
- joi库 学习笔记
零.背景 node.js 应用中,req.query / req.body 传来的参数需要做 valication( 合法性验证 ) 一.安装 https://github.com/hapijs/jo ...
- Shell - 简明Shell入门13 - 用户输入(UserInput)
示例脚本及注释 1 - arguments #!/bin/bash if [ -n "$1" ];then # 验证参数是否传入 echo "The first para ...
- Scala之隐式转换implicit详解
假设我们有一个表示文本的行数的类LineNumber: class LineNumber ( val num : Int ) 我们可以用这个类来表示一本书中每一页的行数: val lineNumOfP ...
- (转)X-Frame-Options响应头缺失漏洞
原文:https://blog.csdn.net/ljl890705/article/details/78071601 x-frame-options响应头缺失漏洞. 故名思意,就是返回的响应头信息中 ...
- webgl之五彩光源
一.Three.js中有哪些光源? 在Three.js中,光源有一个基类THREE.Light(hex),这个hex接受16进制颜色作为参数而初始化光源的颜色,比如我们要定义一种绿色的光源,可以这样来 ...
- 转:TCP为什么要3次握手和4次挥手时等待2MSL、 TCP如何保证消息顺序以及可靠性到达
关于tcp三次握手.四次挥手可以看这里:TCP与UDP的差别以及TCP三次握手.四次挥手 1.TCP为甚要3次握手? 在谢希仁著<计算机网络>第四版中讲“三次握手”的目的是“为了防止已失效 ...
- Android的几种弹出框
项目效果图: 新建一个项目,结构图如下所示: activity_main.xml: <?xml version="1.0" encoding="utf-8" ...