c++ - Parse a file and store it in a class -


while(!myfilename.eof()){     getline( myfilename , linelist ); linelist2 = linelist.substr(0,linelist.find(";")); //skips line after ';'      if(linelist2.find_first_not_of("#")){} //removes lines starts #     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 file , store in vector. actual work parse file , store in class. example:

class test{     string name;     string address; }; 

now text file can have

name = tom address = verger 

how should store in class members. thats vector class?

vector can have

name=> tom address=> verger 

if have

std::vector<test> tests; 

you can replace line

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

with

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

btw class must written lowercase: class.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -