无法扩展多维数组指针c ++

时间:2015-04-04 01:31:42

标签: c++ arrays pointers multidimensional-array

我需要用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的问题在哪里,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试了很多可能的方法后,问题是什么。我发现了解决方案。我忘了删除第一个初始box_size的命令。所以解决方案

    #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;
//    vector< vector<string> > 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)
            {
                break;
                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;
            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;
}