c++ - 展开多维数组指针失败c++

标签 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
...
...

在一个文件中有上千次重复。我尝试使用 vector ,但执行时间很长。所以,我决定使用指针。如果使用指针,我不担心会丢失多少时间步长信息。因为我能得到它。当我使用 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的问题在哪里,请帮我解决这个问题。

最佳答案

尝试了很多可能的方法后,问题是什么。我找到了解决方案。我忘记删除第一个初始 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;
}

关于c++ - 展开多维数组指针失败c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441800/

相关文章:

c++ - 即使从类中抛出异常,C++ 程序还能正常运行吗?

c++ - 在 C++ 中用整数填充固定大小的 char 数组

c - 如何解释 C 中结构的成员访问(点)运算符?

c++ - 在 C++ 中创建对象

c - 指针操作

c++ - 使用 Null C++ 初始化结构数组

c++ - 无效使用不完整类型 'PGconn {aka struct pg_conn}'

c++ - 有没有一种方法可以通过 #define 列出在 c/c++ 中定义的符号

c++ - 数组类的构造函数

javascript - (Number | undefined)[] 不可分配给 Number[]