Convert string to int array C++ -
how convert string int array in c++?
giving input
21 100 30
convert int array
21,100,30
i did similar thing topcoder contest, i'll show how did won't guaranty it's best method.
to convert string int used function (you'll need #include sstream belive)
int nts ( string mys ) { istringstream buffer(mys); int value=0; buffer >> value; return value; } the above function return int string. need split initial string smaller string. used function
int getx(int x,string st) { int nr=0; string ret=""; for(int i=0;i<st.size();++i) { if(st[i]==' '){ ++nr; continue; } if(nr==x) ret = ret + st[i]; } return nts(ret); } where x number want string. if want first 1 type 0. example: if want 100 string write getx(1,"21 100 30"); , return 100 (int).
now have simple convert string vector.
for(int i=0;i<3;++i) myvector.push_back(getx(i,my_string)); hope helps.
Comments
Post a Comment