0
while(!myfilename.eof()){
    getline( myfilename , linelist );
linelist2 = linelist.substr(0,linelist.find(";")); //skips line after ';' 
    if(linelist2.find_first_not_of("#")){} //removes lines starts with #
    else{
        size_t pos = linelist2.find("=");
        test = linelist2.substr(0,pos);
        test2 = linelist2.substr(pos+1);
        getlist.push_back(make_pair(test,test2));
        }   
    }
//iterator
    /*for(std::vector <std::pair<std::string,std::string>>::iterator it=getlist.begin(); it!=getlist.end();it++)
    {
        std::cout << it->first << "=>" << it->second << "\n";
    }*/
    myfilename.close();

I parsed the file and store in a vector. But my actual work is parse the file and store it in a class. for example:

class test{
    string Name;
    string address;
};

Now the text file can have

Name = tom
address = verger

how should i store it in a class members. Thats from vector to a class?

vector can have

Name=> tom
address=> verger
Tobias Wollgam
  • 671
  • 2
  • 7
  • 23
  • 2
    Please take some time to read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Jul 26 '17 at 04:32
  • 1
    Why not just assign the values? E.g. in the iteration you commented out `test person; person.Name = it->first; person.address = it->second;`. – Steeve Jul 26 '17 at 07:07
  • Class is not the best solution for that. Consider using a `map`. It would fit much better. – K. Kirsz Jul 26 '17 at 12:23
  • `map` will only work if the keys are unique. – Tobias Wollgam Jul 26 '17 at 12:58

1 Answers1

-1

If you have

std::vector<test> tests;

you can replace the line

getlist.push_back(make_pair(test,test2));

with

tests.emplace_back(test, test2); // since c++11

BTW Class must be written lowercase: class.

Tobias Wollgam
  • 671
  • 2
  • 7
  • 23