对于一个学校项目,我需要创建一个带有递归排序函数的构造函数,该函数由其中的老师提供,具有我必须自己创建的一些函数。当我启动程序时,结果显示一些空白行,然后是预期的输出。但是当我增加int_buffer a(100)或更高并再次尝试时程序崩溃。我认为这可能是内存泄漏,但不太确定,也不知道如何解决问题。我认为问题出在int_sorted.cpp
的代码: main.cpp中:
#include "int_buf.h"
#include "int_sorted.h"
#include <iostream>
#include <string>
void f(int_buffer& buf){
for (int* i = buf.begin();i != buf.end();i++) {
}
for (const int* i = buf.begin(); i != buf.end(); i++) {
}
}
int main(){
int_buffer a(100);
int_buffer b(50);
int r[] = {1,4,10,2,54,3,6,20};
for (size_t i = 0; i < a.size(); i++){
a[i] = std:: rand();
b[i] = std::rand();
}
int_sorted x(a.begin(), a.size());
}
std::cout << "\n";*/
for (int i = 0; i < x.size();i++) {
std::cout<<*(x.begin() + i)<<"\n";
return 0;
}
int_buf.h:
#ifndef INT_BUF_H
#define INT_BUF_H
#include <cstdlib>
class int_buffer {
public:
bool check_sorted(int_buffer int_check);
explicit int_buffer(size_t size);
int_buffer(const int * source, size_t size);
int_buffer(const int_buffer & rhs);
int_buffer(int_buffer && rhs);
int_buffer & operator =(const int_buffer & rhs);
int_buffer & operator =(int_buffer && rhs);
int& int_buffer::operator[](size_t index);
size_t size() const;
int * begin();
int * end();
const int* begin() const;
const int* end() const;
~int_buffer();
private:
size_t sz;
int* buf;
};
#endif
int_buf.cpp:
#include "int_buf.h"
#include <algorithm>
#include <iostream>
#include <cstddef>
bool int_buffer::check_sorted(int_buffer int_check){
int* a = int_check.begin();
int* b = nullptr;
while (a != int_check.end() -1){
b= a + 1;
if (*a > *b){
return false;
}
else{
a = b;
}
}
return true;
}
int_buffer::int_buffer(size_t size) :sz(size), buf(new int[size]){}
int_buffer::int_buffer(const int* source, size_t size) : sz(size), buf(new int[size]){
for (size_t i = 0; i < sz;i++){
buf[i] = *(source+i);
}
}
int_buffer::int_buffer(const int_buffer& rhs):sz(rhs.sz),buf(new int[rhs.sz]){
std::copy(rhs.begin(), rhs.end(), begin());
}
int_buffer::~int_buffer(){
delete[] buf;
}
int_buffer::int_buffer(int_buffer && rhs) :buf(nullptr), sz(0){
std::swap(buf, rhs.buf);
std::swap(sz, rhs.sz);
}
int_buffer& int_buffer::operator =(const int_buffer & rhs){
int_buffer temp(rhs);
std::swap(temp.buf,buf);
std::swap(temp.sz,sz);
return *this;
}
int_buffer & int_buffer::operator =(int_buffer&& rhs){
std::swap(buf, rhs.buf);
std::swap(sz, rhs.sz);
return *this;
}
int& int_buffer::operator[](size_t index){
return buf[index];
}
size_t int_buffer::size() const{
return sz;
}
int* int_buffer::begin(){
return buf;
}
int* int_buffer::end(){
return buf + sz;
}
const int* int_buffer::begin() const{
return buf;
}
const int* int_buffer::end() const{
return buf + sz;
}
int_sorted.h:
#ifndef INT_SORTED_H
#define INT_SORTED_H
#include "int_buf.h"
#include <iostream>
class int_sorted{
public:
int_sorted(const int* source, std::size_t size);
std::size_t size() const;
int* insert(int value);
const int* begin() const;
const int* end() const;
int_sorted merge(const int_sorted& merge_with) const;
int_sorted sort(const int*begin, const int* end);
void int_sorted::selSort(int_sorted& arr, int size);
private:
int_buffer buffer;
};
#endif
int_sorted.cpp:
#include "int_sorted.h"
#include "int_buf.h"
#include <iostream>
#include <algorithm>
#include <cstddef>
int_sorted::int_sorted(const int* source, size_t size) :buffer(source, size){
if (buffer.size() < 1) {}
else {
if (buffer.check_sorted(buffer)) {}
else {
int_buffer tmp(buffer.begin(), 0);
tmp = sort(buffer.begin(), buffer.end()).buffer;
buffer = tmp;
}
}
}
size_t int_sorted::size() const{
return buffer.size();
}
int* int_sorted::insert(int value) {
int_buffer tmp(buffer.size()+1);
for (int i =0; i < buffer.size();i++) {
tmp[i] = buffer[i];
}
tmp[buffer.size()] = value;
this->buffer = tmp;
return (buffer.end()-1);
}
const int* int_sorted::begin() const{
return buffer.begin();
}
const int* int_sorted::end() const{
return buffer.end();
}
int_sorted int_sorted::merge(const int_sorted& merge_with) const{
int_sorted c(this->begin(), 0);
const int* travA = this->begin();
const int* travB = merge_with.begin();
int tCountA = 0;
int tCountB = 0;
while ((travA + tCountA)!=this->end()&&(travB + tCountB )!=merge_with.end()){
if (travA[tCountA] < travB[tCountB]){
c.insert(travA[tCountA]);
tCountA++;
}
else{
c.insert(travB[tCountB]);
tCountB++;
}
}
while ((travA + tCountA)!=this->end()){
c.insert(travA[tCountA]);
tCountA++;
}
while ((travB + tCountB) != merge_with.end()){
c.insert(travB[tCountB]);
tCountB++;
}
std::cout << "\n";
return c;
}
int_sorted int_sorted::sort(const int* begin, const int* end) {
if (begin == end)return int_sorted(begin, 1);
if (begin == end - 1)return int_sorted(begin, 1);
ptrdiff_t half = (end - begin) / 2;
const int* mid = begin + half;
return sort(begin, mid).merge(sort(mid, end));
}
当我有int_buffer a(20);
时,这就是它的样子