em bị lỗi breakpoint ở các chỗ delete (cấp phát động), em đang làm class thực hiện Couting Sort
em vẫn xuất được kết quả nhưng vẫn bị lỗi breakpoint ở các đoạn đó ạ
CountingSort.h
#pragma once
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <algorithm>
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include <conio.h>
using namespace std;
class CountingSort
{
int length;
int* Arr;
int* count_array ;
int* output ;
public:
CountingSort();
CountingSort(int n, int x);
~CountingSort();
//void Swap(int& m, int& n);
int GetLength();
void Increase();
friend istream& operator >> (istream&, CountingSort&);
friend wostream& operator << (wostream&, const CountingSort&);
};
CountingSort.cpp
#include "CountingSort.h"
CountingSort::CountingSort()
{
length = 0;
Arr = new int[length];
output = new int[length];
}
CountingSort::CountingSort(int n, int x)
{
Arr = new int[n];
for (int i = 0; i < n; i++)
{
Arr[i] = x;
}
}
CountingSort::~CountingSort()
{
delete[] Arr;
delete[] output;
Arr = NULL;
output = NULL;
}
istream& operator >> (istream& is, CountingSort& S)
{
_setmode(_fileno(stdout), _O_U16TEXT);
do
{
wcout << L"\nNhập vào số phần tử: "; is >> S.length;
if (S.length < 0) wcout << L"\nKhông hợp lệ! Vui lòng nhập lại!\n";
} while (S.length < 0);
S.Arr = new int[S.length];
wcout << L"\nHãy nhập các phần tử của mảng:\n";
for (int i = 0; i < S.length; i++)
{
wcout << "Arr[" << i << "] = ";
is >> S.Arr[i];
}
return is;
}
wostream& operator << (wostream& os, const CountingSort& S)
{
os << "\nArr = ";
for (int i = 0; i < S.length; i++)
{
os << S.Arr[i] << " ";
}
return os;
}
//void CountingSort::Swap(int& m, int& n)
//{
// int temp;
// temp = m;
// m = n;
// n = temp;
//}
int CountingSort::GetLength()
{
return length;
}
void CountingSort::Increase()
{
int n;
n = CountingSort::GetLength();
int max = Arr[0];
int min = Arr[0];
for (int i = 1; i < n; i++)
{
if (Arr[i] > max) max = Arr[i];
else if (Arr[i] < min) min = Arr[i];
}
int k = max - min + 1;
//wcout << max << " " << min;
count_array = new int[k]; //mảng đếm số lần xuất hiện của các phần tử trong Arr[].
for (int i = 0; i < k; i++)
count_array[i] = 0;
for (int i = 0; i < n; i++)
count_array[Arr[i] - min]++;
for (int i = 1; i < k; i++)
count_array[i] += count_array[i - 1];
wcout << endl;
for (int i = 0; i < n; i++)
{
output[count_array[Arr[i] - min] - 1] = Arr[i];
count_array[Arr[i] - min]--;
}
for (int i = 0; i < n; i++)
Arr[i] = output[i];
delete[] count_array;
count_array = NULL;
}