c – 通过头文件功能将遍历搜索树中的遍历数据加载到Vector中

我目前有一个Vector类,它是模板类,用于存储一些库存对象.例如.矢量

    if (p != NULL)
    {
        inorder(p->lLink);
        cout << (p->info) << endl; //changed to vecA.Push_back(p->info);
        inorder(p->rLink);
    }

但是,它并没有按照我希望的方式将项目从节点推送到我的向量中.它在技术上有效,我可以一个接一个地打印()整齐排列所有数据行,但是当我执行Vector.getLength()时,它显示只有1行.

这里的问题是,当Vector只有1行(但奇怪地包含我拥有的所有项目并逐行显示)时,我无法使用此Vector,因为大多数进程都涉及for循环.

请指教,我怀疑我的inorder()方法有问题.也许这是BST输出数据等的方式.我对BST很新,我没有太多时间完成这项任务.

这是我的inorder()函数的代码

    template <class elemType>
    void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) const
    {
    Vector<Stock> bstData;
    ofstream of("output.csv"); 
    of << fixed << showpoint << setprecision(2); 

    if (p != NULL)
    {
        inorder(p->lLink);
        bstData.Push_back(p->info);
        inorder(p->rLink);
    }

    //Below is a for-loop that I was planning to use to get the traversed data 
    //from the Vector into an output file so I can access the traversed data 
    //through reading an output file from my Main() function

    for(int i = 0; i < bstData.getLength(); i++)
    {
        cout << "bstData data at " << i << ": " << bstData.at(i) << endl;
        //above statement is to check if i did an increment
        cout << "bstData length is: " << bstData.getLength() << endl;
        //above statement is to check my vector's length
        //the following statement is to output data from vector into a .csv file
        of << bstData.at(i).d1.getDay() << "/" << bstData.at(i).d1.getMonth() << "/" << bstData.at(i).d1.getYear() << "," << setw(2) << setfill('0') << bstData.at(i).t1.getHour() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getMin() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getSec() << "," << bstData.at(i).getPrice() << "," << bstData.at(i).getVolume() << "," << bstData.at(i).getValue() << endl;
        cout << "i is now at: " << i << endl; //check i again
    }
    of.close();
    } //close inorder()

以下是我运行程序时的输出:

P.S:我无法发布图片,所以请参考此链接获取图片!!

http://i132.photobucket.com/albums/q28/LoveSHE911/Screen%20Shot%202015-11-17%20at%205.05.48%20am_zpstldxxags.png

以下是运行bstData.Print()以检查向量内容时的输出.

http://i132.photobucket.com/albums/q28/LoveSHE911/Screen%20Shot%202015-11-17%20at%205.07.27%20am_zpsrqqo8kzb.png

请劝告和帮助,我迷路了!

编辑:感谢@Mykola,我已经解决了上述问题,但发生了一个密切相关的问题.

在我的main()中执行inorderTraversal()后,我想做一个ifstream inFile(“output.csv”)和一段时间(inFile>> dd>> c>> mm>> c >> yy>> …)读取文件的数据,用数据创建一个stock对象,并将push_back写入现有的向量.代码如下.

    ifstream inputfile("output.csv"); //open user chosen data file
    //load traversed data from output file output.csv into vAll 
    while (inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp) 
    {   //check if there's remaining data in input file
        Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
        //if there's still remaining data, create new stock object
        vAll.Push_back(stk2); //Insert stock object into vector 
    }
        cout << vAll.getLength() << endl; //check vector length

但是,vAll.getLength()返回0.是否有一个明显的错误,我没有看到?

最佳答案 您必须重建函数以使用节点指针传递存储目标.它的意思是

void binaryTreeType<elemType>::inorder(nodeType<elemType> *p, Vector<Stock>& storage) const

所以整个代码还有一个功能

template <class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) const
{
    Vector<Stock> bstData;
    ofstream of("output.csv"); 
    of << fixed << showpoint << setprecision(2); 

    inorder(p, bstData); // fill bstData recursively

    for(int i = 0; i < bstData.getLength(); i++)
    {
        cout << "bstData data at " << i << ": " << bstData.at(i) << endl;
        //above statement is to check if i did an increment
        cout << "bstData length is: " << bstData.getLength() << endl;
        //above statement is to check my vector's length
        //the following statement is to output data from vector into a .csv file
        of << bstData.at(i).d1.getDay() << "/" << bstData.at(i).d1.getMonth() << "/" << bstData.at(i).d1.getYear() << "," << setw(2) << setfill('0') << bstData.at(i).t1.getHour() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getMin() << ":" << setw(2) << setfill('0') << bstData.at(i).t1.getSec() << "," << bstData.at(i).getPrice() << "," << bstData.at(i).getVolume() << "," << bstData.at(i).getValue() << endl;
        cout << "i is now at: " << i << endl; //check i again
    }
    of.close();
} //close inorder()

和主要的递归功能

template <class elemType>
void binaryTreeType<elemType>::inorder(nodeType<elemType> *p, Vector<Stock>& storage) const
{
    if (p != NULL)
    {
        inorder(p->lLink, storage); // Fill storage with left values 
        storage.Push_back(p->info); // Add current value to storage (actualy bstData).
        inorder(p->rLink, storage); // Fill storage with right values
    }
} //close inorder()

要从文件加载数据,请尝试执行此操作

while (inputFile.good()) // if stream is good
{   
    inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp;
    //check if there's remaining data in input file
    Stock stk2(dd, mm, yy, hh, mn, ss, ch1, ch2, pr, vl, tp); 
    //if there's still remaining data, create new stock object
    vAll.Push_back(stk2); //Insert stock object into vector 
}
cout << vAll.getLength() << endl; //check vector length

我想你也必须改写你的阅读操作

inputfile >> dd >> c >> mm >> c >> yy >> hh >> c >> mn >> c >> ss >> ch1 >> ch2 >> pr >> vl >> tp;

istream不支持格式化输入,因此您必须以不同方式解析数据或使用支持此功能的fscanf.

点赞