[C/C++标准库]_[优先队列priority_queue的使用]

std::priority_queue

场景:

1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就需要优先级越高的先执行。而queue并没有排序功能,这时priority_queue是比较好的选择.

2 对于异步的task也是一样,在不断添加新的task时,当然希望优先级越高的先执行.

解析:

1. 如果需要把优先级最高的先pop,那么comp比较时需要返回false.

代码:

//1.Elements are popped from the "back" of the specific container,
//which is known as the top of the priority queue.
//2.shall return true if a is considered to go before b
// in the strict weak ordering the function defines
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Task
{
public:
	Task(int priority):priority_(priority)
	{

	}
	~Task(){}

	int priority_;
	void DoWork()
	{
		cout << "DoWork: " << (int)this << " priority: " << priority_ << endl;
	}
};

class PriorityCompare
{
public:
	bool operator()(Task* t1,Task* t2)
	{
		return t1->priority_ > t2->priority_;
	}

	/* data */
};

int main(int argc, char const *argv[])
{
	PriorityCompare pc;
	priority_queue<Task*,std::vector<Task*>,PriorityCompare> tasks(pc);
	Task t1(1);
	Task t2(10);
	Task t3(3);
	Task t4(1);
	Task t5(8);
	Task t6(9);
	tasks.push(&t1);
	tasks.push(&t2);
	tasks.push(&t3);
	tasks.push(&t4);
	tasks.push(&t5);
	tasks.push(&t6);

	while(!tasks.empty())
	{
		Task* task = tasks.top();
		tasks.pop();
		task->DoWork();
	}

	return 0;
}

输出:

DoWork: 2293456 priority: 1
DoWork: 2293444 priority: 1
DoWork: 2293448 priority: 3
DoWork: 2293440 priority: 8
DoWork: 2293436 priority: 9
DoWork: 2293452 priority: 10

参考: http://www.cplusplus.com/reference/queue/priority_queue/

[C/C++标准库]_[优先队列priority_queue的使用]

时间: 2024-08-24 05:12:31

[C/C++标准库]_[优先队列priority_queue的使用]的相关文章

[C/C++标准库]_[读写中文路径的文件--写入unicode字符串]

场景: 1. 需要写入非ascii文本并且与本地编码无关时,除了utf8,unicode编码是另外一个选择,它的好处是占两个字节,便于统计字符和对字符进行处理,因为有对应的宽字节的函数,如wcslen. 2.需要注意的亮点,要先写入0xff,0xfe文件头,之后使用fwprintf时用%S(大写)格式写入宽字节字符串. 3.使用_wfopen支持中文路径. 代码1: #include <stdio.h> #include <stdint.h> #include <stdlib

[C/C++标准库]_[初级]_[交集和补集]

场景: 1. 计算std::vector A和 std::vector B里的相同的元素, 用于保留不删除. 2. 计算std::vector A和 std::vector B里各自的补集, 用于删除A的补集和添加B的补集,用在一些更新关联表的操作里. 比如联系人A所属分组B是一个集合BV, 把联系人A的所属分组 修改为集合CV, 就需要删除两个集合BV,CV的CV补集和新增BV补集. 3. C++标准库为我们提供了这些算法. 代码: // test_AndroidAssistant.cpp :

[C/C++标准库]_[初级]_[使用ctype里的isxxx函数时要注意的事项]

场景: 1. 标准库里的 ctype.h里的函数是用于1个字节的判断的,但是参数却是int, 这样很容易导致误用. isalpha iscntrl isdigit isgraph isprint ispunct isspace isxdigit isalnum islower isupper int isspace( int ch ); 最恶心的是vc++的实现会对超过1字节的值会直接崩溃,gcc不会!!! #if defined (_DEBUG) extern "C" int __c

[C/C++标准库]_[初级]_[使用模板删除字符串前后空格((w)string space)]

场景: 1. C++没有提供删除std::(w)string的前后空格的函数,比如TrimSpace. 2. 很多库都提供, 但是为了移植代码方便,最好还是能用标准库解决就用标准库. 下边用模板实现了移除空格的函数. test.cpp #include <iostream> #include <stdlib.h> #include <string.h> #include <string> #include <ctype.h> using name

[C/C++标准库]_[0基础]_[优先队列priority_queue的使用]

std::priority_queue 场景: 1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就须要优先级越高的先运行.而queue并没有排序功能,这时priority_queue是比較好的选择. 2 对于异步的task也是一样.在不断加入新的task时,当然希望优先级越高的先运行. 解析: 1. 假设须要把优先级最高的先pop,那么comp比較时须要返回false. 代码: //1.Elements are popped from the "back"

C++ Primer(一)_标准库_顺序容器

目录 顺序容器 顺序容器 选择什么容器根据业务需求, 研读STL剖析了解底层数据结构, 更加清楚各种优势劣势 零碎点 迭代器被设置为左闭右合带来的编程假设 begin == end,范围为空 begin != end, 至少一个元素 begin可递增至end 两大类型的容器初始化--同类型容器拷贝,迭代器范围拷贝 前者要求容器类型一致 后者只要求元素可转换 两大类型的容器赋值--=号赋值,assign赋值 前者用于列表或同类型容器 后者用于迭代器,初始化列表,(n,elem)方式:限制顺序容器

[C/C++标准库]_[初级]_[使用fstream合并文本文件]

场景: 1. 就是合并文本文件,并且从第2个文件起不要合并第一行. 2. 多加了一个功能,就是支持2个以上的文件合并. 3. 问题: http://ask.csdn.net/questions/192151 只能说很简单: 基础不过关吧,这位同学,也有可能不是开发的,放这里也是为了培训基础差的. test.cpp #include <fstream> #include <string> #include <iostream> using namespace std; /

[C/C++标准库]_[初级]_[过滤Windows文件名中的非法字符]

场景: 1. 通常生成文件时需要一个文件名,而生成文件名的方式可能是通过用户输入的字符,但是有些字符在windows上是不能作为文件名的,强行创建这类文件会失败. 2.一般可以通过正则表达式替换所有的非法字符,这里实现的是C++98 template(模板)方式的替换无效字符,std::string,std::wstring. 基本上windows上和字符串打交道都离不开wstring. 函数: template<class T> void FilterInvalidFileNameChar(

[C/C++标准库]_[初级]_[构造文件路径(stringByAppendingPathComponent)]

场景: 1. 很多情况下需要通过文件夹和文件名拼接文件路径字符串,每次都需要判断是否需要添加路径分隔符seperator很麻烦,所以可以写一个通用函数. 2. 大多数情况下都是windows使用wstring,mac使用string,所以用模版实现最通用. 函数: template<class T> T AppendPathComponent(const T& source,const T& component) { int length = source.length();