【发布时间】:2015-04-04 01:31:41
【问题描述】:
我需要用 c++ 将一个 txt 文件解析成二维数组。这是一个txt文件示例
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
448
ITEM: BOX BOUNDS pp pp ff
0 19.9348
0 19.9348
0 22.906
ITEM: ATOMS id type xs ys zs
1 1 0 0.125 0
2 1 0 0 0
3 1 0.125 0 0
...
...
在一个文件中有上千个重复。我尝试使用向量,但执行需要很长时间。所以,我决定使用指针。如果使用指针,我不担心会丢失多少时间步长信息。因为我能得到它。当我使用 1D 指针时一切正常,但是当我使用 2D 时,我遇到了问题,这是我的代码
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
using namespace std;
int main()
{
int global_memory = 700;
// set input file stream
ifstream fileLammpsTrajectory;
// initialy find the size of frames
// set string line
string line;
// open file
fileLammpsTrajectory.open("innickel81.lammpstrj");
// check if the file is good or close
if(!fileLammpsTrajectory.good()) return 1;
// initiate memory
int memory = global_memory;
// read increment
int increment = 0;
bool information = false;
// initiate total timestep
int timestep = 0;
// initiate atoms
int* atoms = new int[memory];
bool c_atoms = false;
int i_atoms = 0;
// initiate simulation box
int memory_box = global_memory;
string* box_bounds = new string[memory_box];
bool c_box = false;
int i_box = 0;
// initiate simulation box size
int memory_col = global_memory;
int memory_row = global_memory;
int i_row = 0;
int i_col = 0;
bool c_box_properties = false;
string** box_size = new string*[memory_row];
for(int i = 0; i < memory_row; i++) box_size[i] = new string[memory_col];
for(int i = 0; i < memory_row; i++) delete [] box_size[i];
delete [] box_size;
while(!fileLammpsTrajectory.eof())
{
getline(fileLammpsTrajectory,line);
increment++;
string::size_type ITEM = line.find("ITEM");
information = ITEM != string::npos ? true : false;
if(information)
{
// find how many timestep
if(line == "ITEM: TIMESTEP") timestep++;
// determine is compute atoms?
string::size_type NUMBER = line.find("NUMBER");
c_atoms = NUMBER != string::npos ? true : false;
// determine is compute simulation box?
string::size_type BOX = line.find("BOUNDS");
c_box = BOX != string::npos ? true : false;
}
// reset i_col to make sure array dimension
i_col = c_box ? i_col : 0;
if(c_box && information)
{
if(i_box >= memory_box)
{
memory_box = memory_box * 2;
string* temp_box_bounds = new string[memory_box];
for(int i = 0; i < i_box; i++) temp_box_bounds[i] = box_bounds[i];
delete [] box_bounds;
box_bounds = temp_box_bounds;
}
box_bounds[i_box] = line.substr(6);
i_box++;
i_row = i_box - 1;
}
// determine is to compute simulation box
c_box_properties = c_box && !information ? true : false;
if(c_box_properties)
{
if(i_row >= memory_row || i_col >= memory_col)
{
memory_row = i_row >= memory_row ? memory_row * 2 : memory_row;
memory_col = i_col >= memory_col ? memory_col * 2 : memory_col;
string** temp_box_size = new string*[memory_row];
for(int i = 0; i < memory_row; i++) temp_box_size[i] = new string[memory_col];
// copy
for(int i = 0; i < i_row; i++)
{
if(i_col < 1)
{
temp_box_size[i][0] = box_size[i][0];
}else{
for(int j = 0; j < i_col; j++)
{
temp_box_size[i][j] = box_size[i][j];
}
}
delete [] box_size[i];
}
delete [] box_size;
box_size = temp_box_size;
}
box_size[i_row][i_col] = line;
cout << box_size[i_row][i_col] << endl;
i_col++;
}
if(!information && c_atoms)
{
if(i_atoms >= memory)
{
memory = memory * 2;
int* temp_atoms = new int[memory];
for(int i = 0; i < i_atoms; i++) temp_atoms[i] = atoms[i];
delete [] atoms;
atoms = temp_atoms;
}
atoms[i_atoms] = atoi(line.c_str());
i_atoms++;
}
}
// net timestep
timestep--;
fileLammpsTrajectory.close();
return 0;
}
我想要的输出是
box_size[0][0] = 0 19.9348
box_size[0][1] = 0 19.9348
box_size[0][2] = 0 19.9348
...
结果
box_size[0][0] = 0 19.9348
box_size[0][1] =
box_size[0][2] =
...
不知道box_size的问题出在哪里,请帮我解决这个问题。
【问题讨论】:
-
使用
vector而不是new/delete,这样可以解决很多问题。如果需要很长时间,那么您的程序可能还有其他问题。 vector 和 new/delete 之间的性能差异几乎无法衡量。 -
除此之外,试试debugging your program
-
Matt McNabb :我之前尝试过。好的,我用vector再试一次,结果我再说一遍。
标签: c++ arrays pointers multidimensional-array