向向量添加元素

时间:2009-03-12 05:06:30

标签: c++

我想知道为什么我会丢失信息。

到目前为止我有三个功能。

选项处理函数,以及两个向量函数,一个用于读取文本文件,另一个用于将用户指定添加到同一向量。

发生的事情是我读取文件并将其内容保存到矢量中,然后选择下一个选项来添加规范。要做到这一点,我使用push_back。我输出(或调试)看看我是否成功。是。所以我选择再次读取文件的选项,然后回到我开始的地方。用户规格丢失了。我认为这是因为我每次输入该选项时都会分配它。

我是一个初学者,所以我的代码不符合大多数编程标准。

这是我的第一个函数,它隔离了用逗号分隔的字段,保存到结构成员变量中,然后保存到向量中作为文件中每行的元素。

vector<sStruct> * loadFile(char *myTextFile)
{
    myStruct 
        sStruct; 
    vector<myStruct> 
        vectorAddress,
        *vectorData = new vector<myStruct>
    string 
        feild1, feild2, feild3, feild4;

    ifstream 
        *inFile = new ifstream;

    inFile->open( myTextFile, ios::in );


    if ( !inFile->good() )
    {
         cout << "? File Doesnt Exist! " << endl;
    }

    while ( !inFile->eof() )
    {

         getline( *inFile, feild1, ',' );
         sStruct.m_1 = field1;
         getline( *inFile, feild2, ',' );
         sStruct.m_2 = field2;
         getline( *inFile, field3, ',' );
         sStruct.m_3; = feild3
         getline( *inFile, feild4 );
         sStruct.m_4 = feield4;

    vectorData->push_back( sStruct );

    }

    inFile->clear();
    inFile->close();
    cout << vectorData->size();
    delete inFile;   // allocated obj delete to fast why bother?
    return vectorData;
}

此功能成功地将另一个元素添加到矢量中。

vector<sStruct> *  addElement(vector<sStruct> *vAddElement)
{    
    sStruct addElement;  // referring to the same struct.
    cout << "Enter a String: ";
    cin  >> addElement.feild1

    vAddElement->push_back( addElement );
    cout << vAddElement->size() << endl;
    return vAddElement;
}

当我在第一个函数中,我调试我的矢量对象,并保存文件中的数据。好。所以我转到下一个函数并向具有第一个feild的struct成员添加一个字符串。希望不要覆盖任何东西。我调试以确保和不,它一切都很好,push_back工作得很好。但是,当我去我的第一个功能。 Everythingn回来了,就像它开始时一样。

我知道它,因为我在那里读取文件,并在每次进入该功能时分配。有办法防止这种情况吗?

4 个答案:

答案 0 :(得分:1)

你的函数addElement()得到一个参数vAddElement,但你正在推进vectorData ...?!?

答案 1 :(得分:1)

这是因为每次输入loadFile()方法时都要创建向量的新实例。如果要保留向量的内容,则不要在loadFile中创建向量对象。在此函数之外创建它(可能来自调用loadFile()的函数)并将其传递给此函数。

答案 2 :(得分:1)

  1. 对loadFile()函数进行更改以通过引用获取向量对象,并在loadfile()中更新它。
  2. 这样的事情:

    bool loadFile(char *myTextFile, vector<sStruct>& vectorData_out)
    {
          //Read the file
          //push_back the element into vectorData_out
          //vectorData_out.push_back() ...code to push
    }
    

    2.然后通过引用更改addElement以接受vectorData_out:

    bool addElement(vector<sStruct>& vAddElement_out)
    {
          //initilization code for addElement 
         vAddElement_out.push_back( addElement );
         cout << vAddElement->size() << endl;
    }
    

    现在你的调用代码看起来是这样的:

    std::vector<sStruct> aVectorData;
    loadFile("filename.txt", aVectorData);
    addElement(aVectorData);
    

    编辑:另外,除非绝对必要,否则请尽量避免在堆上分配

答案 3 :(得分:0)

用户是否在一行中指定字段或分布在多行中?

     getline( *inFile, feild1, ',' );
     sStruct.m_1 = field1;
     getline( *inFile, feild2, ',' );
     sStruct.m_2 = field2;
     getline( *inFile, field3, ',' );
     sStruct.m_3; = feild3
     getline( *inFile, feild4 );
     sStruct.m_4 = feield4;

上面的代码片段分为四行。你能粘贴用户输入文件的前几行吗?

相关问题