LAD*_*LAD 0 c++ arrays algorithm memory-management dynamic-memory-allocation
我试图用C++编写代码,从文件读取,一系列点,将其存储在动态数组中,然后打印回来.
这是我给出的规范:
"我们希望利用我们可以使用动态内存的事实,因此我们不是根据我们的估计在开始时分配足够大的内存量,而是实现以下算法:
最初,分配的内存非常少.
在循环的每次迭代中(从文件读取并存储到动态数组中),我们跟踪:
当由于新插入而导致元素数量大于数组最大大小时,内存重新分配需要按如下方式进行:
从我下面的代码中,我认为其他一切都很好但是最后一个要求,即在数组末尾添加新项目.
当数组Max_Size超过文件的元素数时,代码工作正常,但是当我尝试扩展num_elements时,结果是文件中的额外数字只保存为零
.
另外,分配还不允许使用向量.对不起,我忘了提这个,我是stackoverflow的新手,有点编程.
请帮忙
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct point {
double x;
double y;
};
int main () {
ifstream inputfile;
inputfile.open("datainput.txt");
if(!inputfile.is_open()){
cout << "could not open file" << endl;
exit(EXIT_FAILURE);
}
//initially very little memory is allocated
int Max_Size = 10;
int num_elements = 0;
point *pp = new point[Max_Size];
//read from file and store in dynamic array
for (int i = 0; !inputfile.eof(); i++) {
inputfile >> pp[i].x >> pp[i].y;
num_elements++; //keep track of number of elements in array
}
//detecting when number of elements exeeds max size due to new insertion:
if (num_elements > Max_Size){
// allocate another dynamic array with a greater maximum size
Max_Size *= 2; // Max_Size = 2*Max_Size to double max size whenever there's memory problem
point *pp2 = new point[Max_Size];
//copy all elements from previous array to new one
for (int j=0; j<(Max_Size/2); j++) {
pp2[j].x = pp[j].x ;
pp2[j].y = pp[j].y;
}
//deallocate memory area allocated for previous array
delete [] pp;
//get pointer to previous array to point to the new one
pp = pp2;
**//add new item at end of the array
for (int k = ((Max_Size/2)-1); k<num_elements; k++) {
inputfile.seekg(k, ios::beg) >> pp2[k].x;
inputfile.seekg(k, ios::beg) >> pp2[k].y;
}**
//print out dynamic array values
for (int l = 0; l<num_elements; l++) {
cout << pp2[l].x << ",";
cout << pp2[l].y << endl;
}
//delete dynamic array
delete [] pp2;
}
else {
//print out dynamic array values
for (int m = 0; m<num_elements; m++) {
cout << pp[m].x << ",";
cout << pp[m].y << endl;
}
//delete dynamic array
delete [] pp;
}
cout <<"Number of elements = " << num_elements <<endl;
//close file
inputfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其他人已经指出std::vector.以下是使用它的代码的大致外观:
#include <vector>
#include <iostream>
struct point {
double x;
double y;
friend std::istream &operator>>(std::istream &is, point &p) {
return is >> p.x >> p.y;
}
friend std::ostream &operator<<(std::ostream &os, point const &p) {
return os << p.x << "," << p.y;
}
};
int main() {
// open the file of data
std::ifstream in("datainput.txt");
// initialize the vector from the file of data:
std::vector<point> p {
std::istream_iterator<point>(in),
std::istream_iterator<point>() };
// print out the data:
std::copy(p.begin(), p.end(), std::ostream_iterator<point>(std::cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
除了比你发布的代码更短更简单之外,让它工作起来可能会更加简单(并且锦上添花)它几乎肯定会运行得更快1(特别是如果你有大量的数据) ).
1.公平地说,我觉得有必要指出,速度的巨大差异主要来自于使用\n而不是endl终止每一条线.这样可以避免在写入每行时刷新文件缓冲区,这样可以轻松地提高数量级的速度.另见:https://stackoverflow.com/a/1926432/179910