#pragma once #include <assert.h> namespace mwq { template <class T> struct Less { bool operator()(const T& x, const T& y) const { return x < y; } }; template <class T> struct Greater { bool operator()(const T& x, const T& y) const { return x > y; } }; template <class T, class Container = vector<T>, class Compare = Less<T>> //默认是大堆 class priority_queue { void Adjustup(int child) { int parent = (child - 1) / 2; Compare com; while (child > 0) { //if (_con[parent] < _con[child]) if(com(_con[parent], _con[child])) { swap(_con[parent], _con[child]); child = parent; parent = (child - 1) / 2; } else { break; } } } void AdjustDown(int parent) { int child = parent * 2 + 1; Compare com; while (child < _con.size()) { //if (child + 1 < _con.size() && _con[child] < _con[child + 1]) if (child + 1 < _con.size() && com(_con[child], _con[child + 1])) { child++; } //if (_con[parent] < _con[child]) if (com(_con[parent], _con[child])) { swap(_con[parent], _con[child]); parent = child; child = parent * 2 + 1; } else { break; } } } public: priority_queue() :_con() {} void push(const T& x) { _con.push_back(x); Adjustup(_con.size() - 1); } void pop() { assert(!empty()); swap(_con[0], _con[_con.size() - 1]); _con.pop_back(); AdjustDown(0); } T& top() { assert(!empty()); return _con[0]; } int size() const { return _con.size(); } bool empty() const { return _con.size() == 0; } private: vector<T> _con; }; void test1() { priority_queue<int> pq; pq.push(1); pq.push(2); pq.push(3); pq.push(4); pq.push(5); pq.push(6); while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); } cout << endl; } }
时间: 2024-03-01 20:54:00 浏览: 21
根据错误信息来看,你在编译 `void mwq::priority_queue<int,std::vector<T>,mwq::Less<T>>::AdjustDown(int)` 这个类模板成员函数时出现了问题。根据代码来看,你在类模板 `priority_queue` 中定义了一个成员函数 `AdjustDown(int parent)`,但是在函数中使用了模板参数 `T`,但是没有进行模板参数的声明。建议你在函数开头增加 `template <class T>` 声明即可,像这样:
```cpp
template <class T>
void AdjustDown(int parent)
{
//...
}
```
同时,为了保证 `AdjustDown` 函数中可以使用 `Compare` 类型进行比较,你需要在函数中加上 `Compare` 类型的声明,像这样:
```cpp
template <class T, class Container = vector<T>, class Compare = Less<T>>
class priority_queue
{
public:
//...
private:
Container _con;
Compare com; // 增加 Compare 声明
};
```
希望这些修改可以解决你的问题。
相关问题
/* Socket head file */ #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>/*sockaddr_in{}*/ #include<arpa/inet.h> /* Unix standard head file */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include <errno.h>/* strerror(),perror(),errno head file*/ #include <sys/time.h> #include <sys/wait.h> #define MAXBUFF 1024 #define PORT 9999怎么改成windows系统
在Windows系统中,网络编程需要使用Winsock API,因此需要引入`<winsock2.h>`头文件。此外,需要在程序中调用`WSAStartup()`函数和`WSACleanup()`函数来初始化和释放Winsock库。
下面是在Windows系统下进行网络编程的头文件和常量定义示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define MAXBUFF 1024
#define PORT 9999
```
其中,`<ws2tcpip.h>`头文件包含了一些网络编程相关的函数和结构体定义。`#pragma comment(lib, "Ws2_32.lib")`语句用于链接Winsock库。
需要注意的是,在Windows系统中,端口号的类型为`unsigned short`,而不是`int`。因此,定义端口号常量时应该使用`USHORT`类型:
```c
#define PORT (USHORT)9999
```
我的头文件是这样写的#include <stdio.h> #include <math.h> #include <fstream> #include <iostream> #include <string>//getline #include <sstream> #include "stb_image.h" #pragma comment(lib, "openGL32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glaux.lib") #include <gl/glaux.h> #include "glew.h" #include "glut.h" #include <vector>,请问如何避免报错gl.h included before glew.h呢
在你的头文件中,应该将 `glew.h` 的引入放在 `gl.h` 的引入之前。因此,你应该将 `glew.h` 的引入语句移到 `gl.h` 的引入语句之前,就像这样:
```
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <string>//getline
#include <sstream>
#include "stb_image.h"
#include <gl/glaux.h>
#include <vector>
#include "glew.h"
#include <gl/glut.h>
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
```
这样就能够避免 `gl.h included before glew.h` 的错误了。