vector排序与查找
本示例使用比较函数,函数对象进行vector的排序与查找操作。
#include <algorithm> #include <vector> using namespace std;#define MAX_NAME_LEN 32 struct Student {int no;char name[MAX_NAME_LEN];int age;bool operator==(const Student& other) const{return no == other.no;}bool operator==(int studentNo) const{return no == studentNo;} };typedef std::vector<Student> TVecStudent;Student g_students[] = {{1, "James", 17},{2, "Tom", 16},{3, "David", 18},{4, "Paul", 15}, };void InitStudent(TVecStudent& students) {students.assign(&g_students[0], &g_students[0] + sizeof(g_students)/sizeof(g_students[0])); }void PrintStudent(const Student& student) {printf("no:%d, name:%s, age:%d\n", student.no, student.name, student.age); }void PrintStudents(TVecStudent& students) {TVecStudent::const_iterator iter = students.begin();for (; students.end() != iter; ++iter){const Student& student = *iter;PrintStudent(student);} }// 小于比较函数运算子 bool lessStudentAge(const Student& lft, const Student& rht) {return lft.age < rht.age; }// 大于比较函数对象运算子 struct GreaterAgeSorter {bool operator()(const Student& lft, const Student& rht){return lft.age > rht.age;} };// 学号查找函数对象运算子 struct StudentNoFinder {StudentNoFinder(int no){m_no = no;}bool operator()(const Student& student) const{return student.no == m_no;}private:int m_no; };
测试代码:
void testVector() {TVecStudent students;InitStudent(students);printf("Origin students:\n");PrintStudents(students);std::sort(students.begin(), students.end(), lessStudentAge);printf("\n\nSorted by age asc:\n");PrintStudents(students);std::sort(students.begin(), students.end(), GreaterAgeSorter());printf("\n\nSorted by age desc:\n");PrintStudents(students);TVecStudent::const_iterator itFind;itFind = std::find(students.begin(), students.end(), 1);if (students.end() != itFind){printf("\n\nStudent found:\n");PrintStudent(*itFind);}itFind = std::find_if(students.begin(), students.end(), StudentNoFinder(2));if (students.end() != itFind){printf("\n\nStudent found:\n");PrintStudent(*itFind);} }int main() {testVector();return 0; }
输出结果:
Origin students: no:1, name:James, age:17 no:2, name:Tom, age:16 no:3, name:David, age:18 no:4, name:Paul, age:15Sorted by age asc: no:4, name:Paul, age:15 no:2, name:Tom, age:16 no:1, name:James, age:17 no:3, name:David, age:18Sorted by age desc: no:3, name:David, age:18 no:1, name:James, age:17 no:2, name:Tom, age:16 no:4, name:Paul, age:15Student found: no:1, name:James, age:17Student found: no:2, name:Tom, age:16
转载于:https://www.cnblogs.com/shokey520/p/3790382.html
vector排序与查找相关推荐
- vector向量容器元素排序与查找
1.利用标准库函数sort()对vector进行排序 参考源码: #include <algorithm> #include <vector>vector<int> ...
- 设计一个排序和查找系统。能够实现对给定的一组学生的借书证信息(如:卡号、姓名、系别、班号等)进行排序和查找。
目录 实验目的 实验题目 排序模板 实验源代码 实验结果 实验目的 了解各种内部排序方法,掌握Shell排序.快速排序.堆排序,归并排序的排序方法,并将其运用到实际问题中. 实验题目 设计一个排序和查 ...
- 数据结构 排序和查找
#include <stdio.h> #include <stdlib.h> #include <time.h> const int ITEMNUM = 100; ...
- leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 1.8 Collections类操作集合详解——排序,查找,复制
Collections类操作集合详解 Collections 类是 Java 提供的一个操作 Set.List 和 Map 等集合的工具类. Collections 类提供了许多操作集合的静态方法,借 ...
- 教你用BitMap排序、查找和存储大量数据
Bit-map的基本思想就是用一个bit位来标记某个元素对应的Value,而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. Bit-map概述 假设现在有这样一 ...
- 数据结构排序、查找算法
前言 这是数据结构的实验四的题目. 为了自己能在繁杂的文件中顺利.快速地找到自己熟悉的排序.查找算法代码,故借CSDN平台存放本人写的代码.另外,还请有缘看到此文章的同行们能多多指点. 非常感谢. 1 ...
- 006-筛选分类排序搜索查找Filter-Classificatio-Sort-Search-Find-Seek-Locate
006-筛选分类排序搜索查找Filter-Classificatio-Sort-Search-Find-Seek-Locate https://www.cnblogs.com/delphixx/p/1 ...
- 排序与查找 详细分析
C语言五种基本排序算法 程序员可以使用的基本排序算法有5种: 插入排序(insertionsort.) 交换排序(exchangesOrt) 选择排序(selectionsort) 归并排序(merg ...
最新文章
- html哪个属性不会脱离文档流,html 在不给高的时候使用float为什么下面的文档会脱离文档流...
- c语言fopen函数打不开,[讨论]用fopen函数无法打开文件有哪几种情况
- 高效的深度学习:将深度学习模型变得更小、更快、更好的综述
- Three-way Partition
- fread函数和fwrite函数,read,write
- linux脚本格式模板,Linux Shell 常见的命令行格式简明总结
- 深度解析 | 基于DAG的分布式任务调度平台:Maat 1
- 三角传输的在链路均衡项目中的灵活应用
- 为什么属龙的有二婚命_为什么属龙的有二婚命 一生婚姻运势详解
- 一个有趣的例子,JS+CSS实现【兴趣是最好的老师】
- ubuntu 18.04 安装谷歌输入法
- ReactOS研究班
- 000001历史数据_平安银行(000001) - 历史数据 - 股票工具
- mac 版本navicate 如何安装破解版
- 设置redis key的expire过期时间
- mysql数据库操作宠物表_mysql数据库及表的基本操作
- poi给Word添加“下一页”
- 新加坡汇丰个人账户分析
- Zookeeper C API 官方示例程序
- 【BZOJ2844】albus就是要第一个出场 线性基 高斯消元