C ++创建结构向量的麻烦

时间:2019-06-30 18:00:56

标签: c++ vector struct

我正在尝试编写一个程序,该程序读取如下CSV文件(用户ID,fname,lname,保险,版本),然后按保险公司对患者进行排序。这就是CSV输入文件的样子。

nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
kb93,Kyle,Borris,Aetna,2

现在,我能够成功读取输入文件并将其存储在结构数组(注册者)中。我还能够确定唯一保险公司的名称。现在,我想创建一个结构向量(uniqueInsurance),该向量还将容纳一个结构向量(被登记者)作为存储所有患者的存储桶,以便“ Aetna”存储桶中将有第一个和最后一个患者它。

唯一的问题是我的程序似乎在显示该行的那一行崩溃

// assign the name of the insurance
uniqueInsVector[i].name = uniqueInsNames[i];

我在开始中断的地方注释掉了代码,以便您可以看到它之前的代码正确地按预期产生了此输出(我知道空集很乱,但现在我至少担心了)< / p>

insurance is: Aetna
version is: 1
userid is: sc91
fname is: Steve
lname is: Combs
insurance is: Cigna
version is: 2
userid is: ml94
fname is: Morgan
lname is: Lands
insurance is: BCBS
version is: 3
userid is: kb93
fname is: Kyle
lname is: Borris
insurance is: Aetna
version is: 2
userid is:
fname is:
lname is:
insurance is:
version is:
Unique Insurances:
Aetna
Cigna
BCBS

这应该是一个相对简单的任务,但是我不知道为什么当我尝试为向量中的第一个结构赋值时,代码会正确地崩溃。有人可以帮我吗?

#include <iostream>
#include <string>                               // for strings
#include <cstring>                              // for strtok()
#include <fstream>                              // for file streams
#include <vector>

using namespace std;

struct enrollee
{
    string userid = "";
    string fname = "";
    string lname = "";
    string insurance = "";
    string version = "";
};

struct uniqueInsurance
{
    string name = "";
    vector <enrollee> enrollVector;
};

int main()
{
    const int ENROLL_SIZE = 1000;              
    const int numCols = 5;                    

    ifstream inputFile;                         // create input file stream for reading only
    struct enrollee enrollArray[ENROLL_SIZE];   // array of structs to store each enrollee and their respective data
    int arrayPos = 0;

    // open the input file to read
    inputFile.open("input.csv");
    // read the file until we reach the end
    while(!inputFile.eof())
    {
        string tokensArray[numCols];
        string userid = "";
        string fname = "";
        string lname = "";
        string insurance = "";
        string sversion = "";
        //int version = -1;

        getline(inputFile,userid,',');
        getline(inputFile,fname,',');
        getline(inputFile,lname,',');
        getline(inputFile,insurance,',');
        getline(inputFile,sversion);

        enrollArray[arrayPos].userid = userid;
        enrollArray[arrayPos].fname = fname;
        enrollArray[arrayPos].lname = lname;
        enrollArray[arrayPos].insurance = insurance;
        enrollArray[arrayPos].version = sversion;

        arrayPos++;
    }

    for (int i = 0; i < arrayPos; i++)
    {
        cout << "userid is: " << enrollArray[i].userid << endl;
        cout << "fname is: " << enrollArray[i].fname << endl;
        cout << "lname is: " << enrollArray[i].lname << endl;
        cout << "insurance is: " << enrollArray[i].insurance << endl;
        cout << "version is: " << enrollArray[i].version << endl;
    }

    // now that we have all of the enrollees and their data read in and stored, we need to determine all of the unique insurances
    // these unique insurances will now serve as "buckets" that the patients will go into, so they can be written to the respective insurance file

    vector <string> uniqueInsNames;     // create an array of unique insurance names
    bool isUnique = true;               // default is true, if we find a match then it will switch to false

    // first loop thru each patient in the enrollArray
    for (int i = 0; i < arrayPos; i++)
    {
        // then loop thru uniqueInsNames to see if that patient's insurance is already in the uniqueInsNames
        for (int j = 0; j < uniqueInsNames.size(); j++)
        {
            // if at any point we find that the patient's insurance is = to any of the insurances in uniqueInsNames, then we flip isUnique flag to false
            if (enrollArray[i].insurance == uniqueInsNames[j])
            {
                isUnique = false;
            }
        }

        // after looping thru the uniqueInsNames, if the insurance is still unique, then add it to the uniqueInsNames
        if (isUnique == true)
        {
            uniqueInsNames.push_back(enrollArray[i].insurance);
        }
    }

    cout << "Unique Insurances: " << endl;
    for (int i = 0; i < uniqueInsNames.size(); i++)
    {
        cout << uniqueInsNames[i] << endl;
    }

    /*// now we know how many and what unique insurances there are,
    // so now we will create a vector of these unique insurance "buckets" to store patients in
    vector <uniqueInsurance> uniqueInsVector;
    uniqueInsVector.push_back(enrollee());

    for (int i = 0; i < uniqueInsNames.size(); i++)
    {
        // assign the name of the insurance
        uniqueInsVector[i].name = uniqueInsNames[i];

        // now cycle thru all patients in enrollArray, and if their insurance name matches, add the patient to the uniqueInsVector
        for (int j = 0; j < arrayPos; j++)
        {
            if (enrollArray[j].insurance == uniqueInsNames[i])
            {
                uniqueInsVector[i].enrollVector.push_back(enrollArray[j]);
            }
        }
    }*/
}

0 个答案:

没有答案