This commit is contained in:
huangsimin 2019-07-31 19:06:06 +08:00
commit 915661d03a
5 changed files with 93 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bin
build
.vscode

26
Makefile Normal file
View File

@ -0,0 +1,26 @@
CC := g++
C_FLAGS := -std=c++17 -Wall -Wextra -g
BIN := bin
SRC := src
INCLUDE := include
LIB := lib
LIBRARIES :=
ifeq ($(OS),Windows_NT)
EXECUTABLE := main.exe
else
EXECUTABLE := main
endif
all: $(BIN)/$(EXECUTABLE)
clean:
$(RM) $(BIN)/$(EXECUTABLE)
run: all
./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*
$(CC) $(C_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES)

1
src/heap.cpp Normal file
View File

@ -0,0 +1 @@
#include "heap.h"

51
src/heap.h Normal file
View File

@ -0,0 +1,51 @@
#include <vector>
using namespace std;
template <class T>
class Heap
{
public:
typedef int (*Compare)(T& , T& );
private:
std::vector<T> data;
Compare compare;
public:
Heap()
{
}
Heap(Compare _compare)
{
this->compare = _compare;
}
~Heap()
{
}
void push(T value)
{
unsigned long cur_idx = this->data.size();
this->data.push_back(value);
for (;cur_idx != 0;)
{
unsigned long push_idx = (cur_idx - 1) >> 1;
T &push_value = this->data[push_idx];
if (this->compare(value, push_value) > 0)
{
this->data[cur_idx] = push_value;
cur_idx = push_idx;
} else {
break;
}
}
this->data[cur_idx] = value;
}
};

12
src/main.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <iostream>
#include "heap.h"
int main(int argc, char *argv[]) {
Heap<int> h ;
h.push(3);
h.push(5);
h.push(2);
h.push(7);
h.push(1);
std::cout << "Hello Easy C++ project!" << std::endl;
}