c++ - what class type should s be in after s has been resolve by for(auto &s :text)? -
string text("asdffffffffff.f"); (const auto &s : text){ cout << s ; if(s.empty() || s[s.size()-1] == '.') cout << endl; else cout << "" ; } return 0; }; my book doesn't state text should be, made string text up, type should text be? const string, const char, else?
- after resolve, type s becomes?
- what s[s.size()-1] ? or function type s attempting call both empty , size? -after compiling error: 'empty , size in 's,' of non-class type 'const char.
- when though string , char function in book, couldn't find empty() or size() function. have make function empty() , size()?
in case auto deducted type char, s const char&.
to fix code can write :
(const auto& s : text){ cout << s ; if(s == '\0' || s == '.') cout << endl; else cout << "" ; } '\0' represents empty char. don't have write function. '\0' acts string terminator, every string , char* contains '\0' @ end appended automatically you.
empty() , size() member functions of std::string class. text std::string can use text.empty() or text.size() .size() returns total size of string.
however s not array here (it's single reference char, not char*) can't use operator [] single char.
Comments
Post a Comment