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当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...
随机推荐
- 【CF995F】 Cowmpany Cowmpensation
CF995F Cowmpany Cowmpensation Solution 这道题目可以看出我的代码能力是有多渣(代码能力严重退化) 我们先考虑dp,很容易写出方程: 设\(f_{i,j}\)表示以 ...
- XSS 跨站脚本攻击 的防御解决方案
虽然说在某些特殊情况下依然可能会产生XSS,但是如果严格按照此解决方案则能避免大部分XSS攻击. 原则:宁死也不让数据变成可执行的代码,不信任任何用户的数据,严格区数据和代码. XSS的演示 Exam ...
- 面向对象多继承(c3算法)、网络基础和编写网络相关的程序
一.面向对象多继承(c3算法) a.有多个父类先找左,再找右,如下示例: class A(object): pass class B(object): def f1(self): print('B') ...
- 在window主机上访问virtualbox虚拟机上centos7的tomcat服务
在virtualbox上装完centos7后,随后装了tomcat服务器,然后在主机上发现用google浏览器访问tomcat不了 于是用ping检测下,发现ping不通,经过多方查找,才配置好,于是 ...
- 【flex】学习笔记/总结
CSS3 flex布局 查看兼容情况: caniuse.com 盒子模型: content-box:平时普通盒子模型,padding/border 会使盒子变大 向外扩展 border-box:盒子模 ...
- 记录cacl()函数中使用scss变量不生效的问题
问题 使用cacl()动态计算元素的高度,运算中包含一个scss变量.如下: height: calc(100% - $ws-header-height); 在浏览器中发现并没有达到预期效果,scss ...
- ASP.NETCore学习记录(二) —— ASP.NET Core 中间件
ASP.NET Core 中间件 目录: 什么是中间件 ? IApplicationBuilder 使用 IApplicationBuilder 创建中间件 Run.Map 与 Use 方法 实战中间 ...
- POJ 2552
#include<iostream> #include<stdio.h> using namespace std; ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ...
- oracle 切换用户操作--or--sys用户密码忘记
1.sqlplus中以普通用户登录oracle后, 普通用户的登录方式: sqlplus /nolog conn 用户名/密码@IP地址/orcl:1521; 这个时候,想要切换sys用户,conn ...
- oracle ASM安装过程中UDEV实现磁盘绑定
UDEV相较于ORACLE 自己的ASMlib 相对比较成熟. 文章转载自: Maclean Liu的个人技术博客 [http://www.oracledatabase12g.com/] 在< ...