项目场景:
在调用自己写的一个简化版优先级队列时候报语法错误:模板生成错误
问题描述
模板生成错误,如下图
原因分析:
问题的分析:刚开始怀疑写模板写错了,之后看错误列表发现是top函数不属于vector。
解决方案:
该问题的具体解决方案:修改为属于vector的成员函数即可
修改为:
相关代码:
// periority_queue.h
#pragma once
#include<vector>
#include<iostream>
using namespace std;
template<typename T>
struct Less
{
public:
bool operator()(const T& x, const T& y)
{
return x < y;
}
};
template<typename T>
struct Greater
{
public:
bool operator()(const T& x, const T& y)
{
return x > y;
}
};
template<class T, class Container = vector<T>, class Compare = Greater<T>>
class periority_queue
{
private:
Container _con;
public:
void adjust_up(int child)
{
Compare com;
int parent = (child - 1) / 2;
while (child > 0)
{
if (com(_con[child], _con[parent]))
{
swap(_con[child], _con[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void push(const T& x)
{
_con.push_back(x);
adjust_up(_con.size() - 1);
}
void adjust_down(int parent)
{
Compare com;
int child = parent * 2 + 1;
while (child < _con.size())
{
if (child + 1 < _con.size() && com(_con[child + 1], _con[child]))
{
child = child + 1;
}
if (com(_con[child], _con[parent]))
{
swap(_con[child], _con[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void pop()
{
swap(_con[0], _con[_con.size() - 1]);
_con.pop_back();
adjust_down(0);
}
size_t size()
{
return _con.size();
}
bool empty()
{
return _con.empty();
}
const T& top()
{
return _con.top();
}
};
// Test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"priority_queue.h"
//#include<iostream>
//#include <queue>
//#include<algorithm>
//using namespace std;
//仿函数类
//template<typename T>
//struct Less
//{
//public:
// bool operator()(const T& x, const T& y)
// {
// return x < y;
// }
//};
//
//void Test()
//{
// vector<int> v = { 1,3,4,2 };
// vector<int>::iterator it = v.begin();
// while (it != v.end())
// {
// cout << *it << " ";
// it++;
// }
// cout << endl;
//
// sort(v.begin(), v.end(), Less<int>());
// it = v.begin();
// while (it != v.end())
// {
// cout << *it << " ";
// it++;
// }
//}
void Test_priority_queue(void)
{
periority_queue<int> pq;
pq.push(2);
pq.push(3);
pq.push(4);
pq.push(1);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
}
int main()
{
/*priority_queue<int> ps;
ps.push(2);
ps.push(3);
ps.push(4);
ps.push(1);
while (!ps.empty())
{
cout << ps.top() << " ";
ps.pop();
}*/
/*priority_queue<int, vector<int>, greater<int>> pq;
pq.push(2);
pq.push(3);
pq.push(4);
pq.push(1);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}*/
/*vector<int> v = { 1,3,4,2 };
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}
cout << endl;
sort(v.begin(), v.end());
it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}*/
/*vector<int> v = { 1,3,4,2 };
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}
cout << endl;
sort(v.begin(), v.end(), greater<int>());
it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}*/
//Test();
Test_priority_queue();
return 0;
}
EOF