c++ - How do I fill this vector? Stroustrup example -
c++ - How do I fill this vector? Stroustrup example -
i reading stroustrup's programming principles , practice using c++. on page 287 gives illustration under heading of order of evaluation. not understand example. constructed, "stringy" inputs created , destroyed every time until types "quit".
how alter code vector v contains every string inputted? written, think v.size() never more 1. want iterate through loop , insert looped on vector rather insert, delete , insert loops.
(code book)
vector <string> v; //this defined globally void function() { string s; while(cin>>s && s!="quit") { string stripped; string non_letters; (int i=0; i<s.size(); ++i) if (isalpha(s[i])) stripped +=i; else not_letters += s[i]; v.push_back(stripped); } }
for every inputted s loop fills stripped letters of s. before end of while loop pushed @ end of v1. v1.size() number of non-quit inputs.
mind, push_back string copy, , @ } destructor of stripped called. (in fact compiler optimize these 2 operations away.)
c++ loops vector
Comments
Post a Comment