C ++中的整数的动态列表 [英] Dynamic list of integers in C++
问题描述
list.h文件:
class List
{
public:
// DEFAULT构造函数
List();
//解构释放释放的内存
〜List(); //防止内存泄漏
// COPY指针的构造方法
List(const List& value) ; //复制构造函数
//修改方法
void Insert(const int);
//用户ACCESS方法
void display(void)const;
private:
int size; //数组的最大大小
int count; //动态数组中的当前元素数目
protected :
int * intptr; //我们的int指针
};
list.cpp实现文件:
#includelist.h//包括我们的类定义
#include< iostream>
using namespace std;
// CONSTRUCTOR
List :: List(){
size = 2; //数组的初始大小
count = 0;
intptr = new int [size]; //开始2个整数大小的数组
}
// DECONSTRUCTOR
List ::〜List(){
delete [] intptr; // free allocated memory
}
//复制构造函数
List :: List(const List& value){
size = value.size ;
cout<< Copy con size:<<尺寸< endl;
count = value.count;
cout<< Compy count:< count<< endl;
if(count< size){
intptr = new int [size]; //分配新数据
} else {
intptr = new int [size + 2]; //分配新数据
}
for(int index = 0; index< count; index ++){
intptr [index] = value.intptr [index]
}
size = size + 2;
delete [] intptr;
intptr = value.intptr;
}
void List :: Insert(const int value){
//我们有房间吗?
if(count< size){
intptr [count] = value;
} else {//如果不是我们需要添加更多的元素到数组
intptr [count] = value; //使用复制构造函数调用DEEP副本
}
cout< SIZE:<尺寸< endl;
cout<< COUNT<< count<< endl;
count ++; //增加在数组中添加的项
}
void List :: display()const {
for(int i = 0; i cout < intptr [i]< endl;
}
main.cpp测试器
#include< iostream>
#includelist.h
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
系统(PAUSE);
return 0;
}
正确,特别是在 Insert()
方法中。尝试这样:
#includelist.h//包括我们的类定义
#include< iostream> ;
// CONSTRUCTOR
List :: List()
{
intptr = new int [2];
size = 2;
count = 0;
std :: cout<< 初始尺寸:<尺寸< count:< count<< std :: endl;
}
// DECONSTRUCTOR
List ::〜List()
{
delete [] intptr; // free allocated memory
}
//复制构造函数
List :: List(const List& value)
{
intptr = new int [ value.size]; //分配新数据
size = value.size;
count = value.count;
for(int index = 0; index< count; ++ index)
intptr [index] = value.intptr [index]
std :: cout<< Copy size:<<尺寸< count:< count<< std :: endl;
}
void List :: Insert(const int value)
{
if(count == size)
{
int * newintptr = new int [size + 2];
for(int index = 0; index< size; ++ index)
newintptr [index] = intptr [index]
delete [] intptr;
intptr = newintptr;
size + = 2;
}
intptr [count] = value;
++ count;
std :: cout<< New size:<<尺寸< count:< count<< std :: endl;
}
void List :: display()const
{
for(int i = 0; i std: :cout< intptr [i]< std :: endl;
}
。
#include< iostream>
#includelist.h
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4)
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9)
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
system(PAUSE);
List mylist2(myList); // copy construct a new list
mylist2.display();
system(PAUSE);
return 0;
}
这样说,你真的应该使用 std: :vector
代替,例如:
#include< iostream>
#include< vector>
#include< algorithm>
void displayValue(int value)
{
std :: cout<值<< std :: endl;
}
int main()
{
std :: vector< int>米尔斯特
mylist.push_back(5);
mylist.push_back(6);
mylist.push_back(2);
mylist.push_back(8);
mylist.push_back(4);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
std :: for_each(mylist.begin(),myList.end(),displayValue);
system(PAUSE);
std :: vector< int> myList2(myList);
std :: for_each(mylist2.begin(),myList2.end(),displayValue);
system(PAUSE);
return 0;
}
若要继续使用您的自定义 List
类,至少在其中使用 std :: vector
:
#include< vector>
class List
{
public:
// DEFAULT构造函数
List();
//修改方法
void Insert(const int);
//用户ACCESS方法
void display(void)const;
protected:
std :: vector< int>内韦茨
};
。
#includelist.h//包括我们的类定义
#include< iostream>
// CONSTRUCTOR
List :: List()
{
intvec.reserve(2);
std :: cout<< 初始尺寸:< intvec.capacity()<< count:< intvec.size()<< std :: endl;
}
//复制构造函数
List :: List(const List& value)
{
intvec = value.intvec;
std :: cout<< Copy size:<< invec.capacity()<< count:< intvec.size()<< std :: endl;
}
void List :: Insert(const int value)
{
intvec.push_back(value);
std :: cout<< New size:<< intvec.capacity()<< count:< intvec.size()<< std :: endl;
}
void List :: display()const
{
for(std :: vector< int> :: const_iterator iter = intvec.begin end = intvec.end(); iter!= end; ++ iter)
std :: cout< * iter<< std :: endl;
}
。
#include< iostream>
#includelist.h
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
system(PAUSE);
List mylist2(myList); // copy construct a new list
mylist2.display();
system(PAUSE);
return 0;
}
I'm trying to create a dynamic array in my List class that will start off with a size of 2 and when you insert values with the Insert method, it will check to see if there is enough space if not it will resize the array with a size + 2... The problem is it is crashing VS is complaining about corruption of the heap. Also I think my copy constructor isn't being called because the cout's arent displaying:
list.h File:
class List
{
public:
// DEFAULT Constructor
List();
// Deconstructor to free memory allocated
~List();// Prevent memory leaks
// COPY Constructor for pointers
List(const List& value);// copy constructor
//Modification methods
void Insert(const int);
// User ACCESS methods
void display(void) const;
private:
int size;// MAX size of the array
int count;// current number of elements in the dynamic array
protected:
int *intptr;// Our int pointer
};
list.cpp implementation file:
#include "list.h" // Include our Class defintion
#include <iostream>
using namespace std;
// CONSTRUCTOR
List::List() {
size = 2; // initial size of array
count = 0;
intptr = new int[size]; // Start off 2 integers sized array
}
// DECONSTRUCTOR
List::~List() {
delete[] intptr; // free allocated memory
}
// Copy constructor
List::List(const List& value) {
size = value.size;
cout << "Copy con size : " << size << endl;
count = value.count;
cout << "Compy count : " << count << endl;
if (count < size) {
intptr = new int[size]; // Allocate new data
} else {
intptr = new int[size + 2]; // Allocate new data
}
for (int index = 0; index < count; index++) {
intptr[index] = value.intptr[index];
}
size = size + 2;
delete[] intptr;
intptr = value.intptr;
}
void List::Insert(const int value) {
// do we have room?
if (count < size) {
intptr[count] = value;
} else { // if not we need to add more elements to array
intptr[count] = value; // DEEP copy invoked with copy constructor
}
cout << "SIZE: " << size << endl;
cout << "COUNT" << count << endl;
count++; // Increase items added in array
}
void List::display() const {
for (int i = 0; i < count; i++)
cout << intptr[i] << endl;
}
main.cpp tester
#include <iostream>
#include "list.h"
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
system("PAUSE");
return 0;
}
You are not managing the array correctly, especially in your Insert()
method. Try this instead:
#include "list.h" // Include our Class defintion
#include <iostream>
// CONSTRUCTOR
List::List()
{
intptr = new int[2];
size = 2;
count = 0;
std::cout << "Initial size : " << size << " count : " << count << std::endl;
}
// DECONSTRUCTOR
List::~List()
{
delete [] intptr; // free allocated memory
}
// Copy constructor
List::List(const List& value)
{
intptr = new int[value.size]; // Allocate new data
size = value.size;
count = value.count;
for(int index = 0; index < count; ++index)
intptr[index] = value.intptr[index];
std::cout << "Copy size : " << size << " count : " << count << std::endl;
}
void List::Insert(const int value)
{
if (count == size)
{
int *newintptr = new int[size+2];
for(int index = 0; index < size; ++index)
newintptr[index] = intptr[index];
delete[] intptr;
intptr = newintptr;
size += 2;
}
intptr[count] = value;
++count;
std::cout << "New size : " << size << " count : " << count << std::endl;
}
void List::display() const
{
for(int i = 0; i < count; i++)
std::cout << intptr[i] << std::endl;
}
.
#include <iostream>
#include "list.h"
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
system("PAUSE");
List mylist2(myList); // copy construct a new list
mylist2.display();
system("PAUSE");
return 0;
}
With that said, you really should use std::vector
instead, eg:
#include <iostream>
#include <vector>
#include <algorithm>
void displayValue(int value)
{
std::cout << value << std::endl;
}
int main()
{
std::vector<int> mylist;
mylist.push_back(5);
mylist.push_back(6);
mylist.push_back(2);
mylist.push_back(8);
mylist.push_back(4);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
mylist.push_back(8);
mylist.push_back(5);
mylist.push_back(9);
std::for_each(mylist.begin(), myList.end(), displayValue);
system("PAUSE");
std::vector<int> myList2(myList);
std::for_each(mylist2.begin(), myList2.end(), displayValue);
system("PAUSE");
return 0;
}
To take it a step further, if you want to keep using your custom List
class, at least use std::vector
inside of it:
#include <vector>
class List
{
public:
// DEFAULT Constructor
List();
//Modification methods
void Insert(const int);
// User ACCESS methods
void display(void) const;
protected:
std::vector<int> intvec;
};
.
#include "list.h" // Include our Class defintion
#include <iostream>
// CONSTRUCTOR
List::List()
{
intvec.reserve(2);
std::cout << "Initial size : " << intvec.capacity() << " count : " << intvec.size() << std::endl;
}
// Copy constructor
List::List(const List& value)
{
intvec = value.intvec;
std::cout << "Copy size : " << invec.capacity() << " count : " << intvec.size() << std::endl;
}
void List::Insert(const int value)
{
intvec.push_back(value);
std::cout << "New size : " << intvec.capacity() << " count : " << intvec.size() << std::endl;
}
void List::display() const
{
for(std::vector<int>::const_iterator iter = intvec.begin(), end = intvec.end(); iter != end; ++iter)
std::cout << *iter << std::endl;
}
.
#include <iostream>
#include "list.h"
int main()
{
List mylist;
mylist.Insert(5);
mylist.Insert(6);
mylist.Insert(2);
mylist.Insert(8);
mylist.Insert(4);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.Insert(8);
mylist.Insert(5);
mylist.Insert(9);
mylist.display();
system("PAUSE");
List mylist2(myList); // copy construct a new list
mylist2.display();
system("PAUSE");
return 0;
}
这篇关于C ++中的整数的动态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!