After excellent advice I defined a struct, filled it with data read from a file and pushed it on a vector. Nice!!! Could somebody tell me now how I can call back the content of a member of the i-th struct I pushed on the vector?
The Structure definition:
struct Config_Data
{
int Surf_Index;
std::string Surf_Mnemo;
double Surf_Param[5];
std::string Surf_Comm;
};
Instantiation of the Struct and the definition of the vector:
Config_Data SURF;
std::vector<Config_Data> CDATA_SURF;
For filling the struct:
for (i=1;i<=10;i++)
{
getline(INP_Stream, Line, delim_Config);
SURF.Surf_Index=atoi(Line.c_str());
getline(INP_Stream,SURF.Surf_Mnemo,delim_Config);
SURF.Surf_Param[0]=Mnemo_list[SURF.Surf_Mnemo];
for (j=1;j<=Mnemo_list[SURF.Surf_Mnemo];j++)
{
getline(INP_Stream,Line,delim_Config);
SURF.Surf_Param[j]=strtod(Line.c_str(),NULL);
}
getline(INP_Stream, SURF.Surf_Comm,delim_Line);
CDATA_SURF.push_back(SURF);
cout<<CDATA_SURF.size()<<endl;
string aaa=CDATA_SURF[i].Surf_Comm;
}
As you can see I read strings from a csv file. I am not so happy with the way I convert strings in the int or double types, but it seem to work.
The way I wanted to call the i-th struct in the vector and its member Surf_Comm:
std::string aaa=CDATA_SURF[i].Surf_Comm;
I compiled without problems but at runtime I get segment violation signals. Being a beginner, I have no clue what is going on.
All help is very welcome!!!
Thanks in advance,
Regards,
Stefan
Element numeration in
std::vectorstarts with zero value. Thus if vector holds one element, index of this value is 0. In case of two values – 0 and 1 and so on. Do not be confused by thesize()vector method. It returns a number of holding elements.So index of the newly pushed value equals to
size()-1. Access to i-th element (that does not exist) results into an error.This code should work if you change range of
ivalue in cycle tofor( i = 0; i < 10; i++ ).