set<int,greater<int>>

2 篇文章 0 订阅
#include <iostream>
#include<set>
#include<functional> 
#include<iterator>
using namespace std;
int main()
{
	int arry1[] = { 3,3,6,7,6,1,3,2,3,2 };
	set<int> s1;
	set<int,greater<int> >s2;//由于greater<>是个函数,降序,所以要加这个头文件#include<functional>,
	for(size_t i = 0; i < sizeof(ary)/sizeof(int); ++i)
	{
		s1.insert(arry1[i]);
		s2.insert(arry1[i]);
	}
	set<int>::iterator It = s1.begin();
	std::cout<<"s1:";
	while(It != s1.end())
	{
		std::cout<<*It++<<"  ";
	}
	std::cout<<std::endl;
	It = s2.begin();
	std::cout<<"s2 : ";
	while(It != s2.end())
	{
		std::cout<<*It++<<" ";
	}
return 0;
}


VS2012中要加入#incude<functional>才能通过编译,但是在Code::Blocks编译器中,不用加#include<functional>也能运行。如果哪位朋友知道原因,可以指导我一下,谢谢

请给一下代码加注释,越详细越好。AStar.h:#ifndef ASTAR_H #define ASTAR_H #include <vector> using namespace std; class AStar { public: AStar(int n); void add_edge(int u, int v, int w); void set_heuristic(vector<int>& h); void shortest_path(int s, int t); vector<int> get_dist(); vector<int> get_prev(); private: struct edge { int to, weight; edge(int t, int w) : to(t), weight(w) {} }; int n; vector<vector<edge>> graph; vector<vector<edge>> rev_graph; vector<int> dist; vector<int> prev; vector<int> heuristic; }; class Astar { }; #endif;AStar.cpp:#include "AStar.h" #include <vector> #include <queue> #include <limits> using namespace std; AStar::AStar(int n) : n(n), graph(n), rev_graph(n), dist(n, numeric_limits<int>::max()), prev(n, -1), heuristic(n, 0) {} void AStar::add_edge(int u, int v, int w) { graph[u].push_back(edge(v, w)); rev_graph[v].push_back(edge(u, w)); } void AStar::set_heuristic(vector<int>& h) { heuristic = h; } void AStar::shortest_path(int s, int t) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[s] = 0; pq.push(make_pair(heuristic[s], s)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (u == t) return; for (auto& e : graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } for (auto& e : rev_graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } } } vector<int> AStar::get_dist() { return dist; } vector<int> AStar::get_prev() { return prev; }
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值